private static IDictionary <string, object> GetRouteConstraints(IApiActionRegistration registration,
                                                                        IReadOnlyList <IGlobalRouteConstraintApplicationFactory> applicationFactories,
                                                                        IServiceProvider serviceProvider)
        {
            var constraints = registration.Constraints as IDictionary <string, object> ??
                              registration.Constraints.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            // ApplyAsync Global Route Constraints
            // ReSharper disable once ForCanBeConvertedToForeach - For loop is faster than foreach
            for (var x = 0; x < applicationFactories.Count; x++)
            {
                var factory = applicationFactories[x];
                if (!factory.Matches(registration.ApiActionType))
                {
                    continue;
                }

                var constraint = factory.Get(registration.ApiActionType, serviceProvider);
                if (constraint == null)
                {
                    continue;
                }

                var key = constraint.GetKey();
                if (!constraints.ContainsKey(key))
                {
                    constraints.Add(key, constraint);
                }
            }

            return(constraints);
        }
        private static RouteValueDictionary GetRouteDefaults(IApiActionRegistration registration,
                                                             IReadOnlyList <IGlobalRouteDefaultApplicationFactory> applicationFactories)
        {
            var routeDefaults = new RouteValueDictionary();

            if (registration.Defaults != null)
            {
                foreach (var kvp in registration.Defaults)
                {
                    routeDefaults.Add(kvp.Key, kvp.Value);
                }
            }

            // ApplyAsync Global Route Defaults
            // ReSharper disable once ForCanBeConvertedToForeach - For loop is faster than foreach
            for (var x = 0; x < applicationFactories.Count; x++)
            {
                var factory = applicationFactories[x];
                if (!factory.Matches(registration.ApiActionType))
                {
                    continue;
                }

                foreach (var kvp in factory.Get(registration.ApiActionType)
                         .Where(kvp => !routeDefaults.ContainsKey(kvp.Key)))
                {
                    routeDefaults.Add(kvp.Key, kvp.Value);
                }
            }

            return(routeDefaults);
        }
Пример #3
0
        protected virtual SwaggerPath GetPath(IApiActionRegistration registration)
        {
            // TODO: Move registration items to info provider
            var info = _infoProvider.GetInfo(registration.ApiActionType);

            var methods = info.Methods;

            if (methods == null || methods.Length == 0)
            {
                methods = _defaultMethods;
            }

            var path = new SwaggerPath
            {
                Path = "/" + RemoveTypesFromRoute(registration.Route),
                Item = new SwaggerPathItem
                {
                    Methods = new SwaggerObjectCollectionFacade <UnofficialPathItemMethod>(methods.Select(m =>
                                                                                                          new UnofficialPathItemMethod
                    {
                        Method    = m,
                        Operation = new SwaggerOperation
                        {
                            Description = info.Description,
                            Tags        = info.Categories,
                            Summary     = info.Summary,
                            Deprecated  = info.IsDeprecated,
                            Responses   = new SwaggerObjectCollectionFacade <SwaggerResponse>(
                                _responseFactory.Create(info.Responses))
                        }
                    })),
                    Parameters = GetParameters(registration, info)
                }
            };

            return(path);
        }
Пример #4
0
        private SwaggerParameter[] GetParameters(IApiActionRegistration registration, IApiActionInfo info)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (registration.Route == null)
            {
                throw new InvalidOperationException("Registration must have a route");
            }

            // TODO: Move this method to factory

            if (info.RequestType == null)
            {
                return(null);
            }

            var propertyDetails = info.RequestType.GetTypeDetails().PropertyWriters;

            var routeParameterNames = GetRouteParameterNames(registration.Route);

            var routeParameters    = new List <SwaggerParameter>();
            var nonRouteParameters = new List <SwaggerParameter>(propertyDetails.Count);

            var includesFile = false;

            foreach (var property in propertyDetails)
            {
                var parameter = new SwaggerParameter
                {
                    Name   = property.Name,
                    Schema = _schemaFactory.Create(property.PropertyType,
                                                   property.PropertyType.GetTypeDetails().PropertyWriters)
                             // TODO: Add Description
                             // TODO: Add Required
                };

                // Check for url
                if (routeParameterNames.Contains(property.Name.ToLowerInvariant()))
                {
                    parameter.In       = SwaggerRequestLocation.path;
                    parameter.Required = true; // All Route Parameters are Required
                    routeParameters.Add(parameter);
                    continue;
                }

                // Check for file
                if (!includesFile && (typeof(IFormFile).IsAssignableFrom(property.PropertyType) ||
                                      typeof(Stream).IsAssignableFrom(property.PropertyType)))
                {
                    includesFile = true;
                }

                nonRouteParameters.Add(parameter);
            }

            var supportsBody = !info.Methods.Any(m => "GET".Equals(m, StringComparison.OrdinalIgnoreCase));

            if (!includesFile && supportsBody)
            {
                return(new[]
                {
                    new SwaggerParameter
                    {
                        Name = "Body",
                        In = SwaggerRequestLocation.body,
                        Required = true,
                        SchemaLink = new SwaggerReferenceLink
                        {
                            Link = "#/definitions/" + _definitionNameProvider.GetDefinitionName(info.RequestType)
                        }
                    }
                });
            }

            var location = includesFile ? SwaggerRequestLocation.formData : SwaggerRequestLocation.query;

            foreach (var param in nonRouteParameters)
            {
                param.In = location;
            }

            var parameters = nonRouteParameters.Concat(routeParameters).ToArray();

            return(parameters.Length > 0 ? parameters : null);
        }