示例#1
0
        public IReadOnlyCollection <SwaggerDefinition> Create(IReadOnlyCollection <IApiActionRegistration> registrations)
        {
            var typeQueue = new Queue <Type>(registrations
                                             .SelectMany(r => _responseInfoFactory.CreateResponses(r.ApiActionType)).Select(ri => ri.ResponseData)
                                             .Where(t => t != null).Select(t => _typeProvider.GetTypeToDocument(t)));

            // Add Requests to Queue


            var resultsLookup = new Dictionary <string, SwaggerDefinition>();

            while (typeQueue.Count > 0)
            {
                var type = typeQueue.Dequeue();
                var name = _definitionNameProvider.GetDefinitionName(type);

                if (!resultsLookup.ContainsKey(name))
                {
                    resultsLookup.Add(name, new SwaggerDefinition
                    {
                        Name   = name,
                        Schema = _schemaFactory.Create(type, type.GetTypeDetails().PropertyReaders, typeQueue)
                    });
                }
            }

            return(resultsLookup.Values.ToList());
        }
示例#2
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);
        }