Ejemplo n.º 1
0
        public static LinkBuilder Create(LinksFromAttribute attribute, ApiDescription apiDescription)
        {
            var modelTypeName = attribute.ModelType.Name;
            var getters       = new List <ILinkPropertyGetter>();
            Dictionary <string, Tuple <string, bool> > parameters =
                apiDescription.ParameterDescriptions.Where(p => p.Source == ApiParameterSource.FromUri)
                .ToDictionary(s => s.Name, s => Tuple.Create("{" + s.Name + "}", IsOptional(s, attribute)), StringComparer.OrdinalIgnoreCase);

            int questionMark = apiDescription.RelativePath.IndexOf('?');

            string path;
            string queryString;

            string[] queryStringSegments;

            if (questionMark > -1)
            {
                path                = apiDescription.RelativePath.Substring(0, questionMark);
                queryString         = apiDescription.RelativePath.Substring(questionMark + 1);
                queryStringSegments = queryString.Split('&');
            }
            else
            {
                path                = apiDescription.RelativePath;
                queryString         = string.Empty;
                queryStringSegments = new string[0];
            }

            foreach (var parameterDescription in apiDescription.ParameterDescriptions.Where(p => p.Source == ApiParameterSource.FromUri))
            {
                var property =
                    attribute.ModelType.GetProperties().FirstOrDefault(p => p.Name.Equals(parameterDescription.Name, StringComparison.OrdinalIgnoreCase));
                var regex = new Regex(@"\{\*?" + parameterDescription.Name + @"[:\?]?.*?\}");
                if (property != null)
                {
                    bool isOptional = IsOptional(parameterDescription, attribute);
                    if (regex.IsMatch(path))
                    {
                        getters.Add(new PathPropertyGetter(regex, property, isOptional));
                    }
                    else if (regex.IsMatch(queryString))
                    {
                        var segment = queryStringSegments.FirstOrDefault(regex.IsMatch);
                        getters.Add(new QueryStringPropertyGetter(regex, segment, property, isOptional));
                    }
                }
                else
                {
                    if (regex.IsMatch(path))
                    {
                        getters.Add(new NullPropertyGetter(parameterDescription.Name));
                    }
                }
            }

            if (getters.Count(g => !g.Optional) != parameters.Count(p => !p.Value.Item2))
            {
                return(null);
            }
            return(new LinkBuilder(attribute.ModelType, attribute.Rel, getters.ToArray(), apiDescription.Route.RouteTemplate));
        }
Ejemplo n.º 2
0
 private static bool IsOptional(ApiParameterDescription parameterDescription, LinksFromAttribute attribute)
 {
     return((parameterDescription.ParameterDescriptor != null && parameterDescription.ParameterDescriptor.IsOptional) &&
            !attribute.IsRequired(parameterDescription.Name));
 }