示例#1
0
        private IParameter CreateParameter(ApiParameterDescription paramDesc, ISchemaRegistry schemaRegistry)
        {
            var source = paramDesc.Source.Id.ToLower();
            var schema = (paramDesc.Type != null) ? schemaRegistry.GetOrRegister(paramDesc.Type) : null;

            if (source == "body")
            {
                return(new BodyParameter
                {
                    Name = paramDesc.Name,
                    In = source,
                    Required = paramDesc.IsRequired(),
                    Schema = schema
                });
            }
            else
            {
                var nonBodyParam = new NonBodyParameter
                {
                    Name     = paramDesc.Name,
                    In       = source,
                    Required = paramDesc.IsRequired(),
                };
                if (schema != null)
                {
                    nonBodyParam.PopulateFrom(schema);
                }
                return(nonBodyParam);
            }
        }
示例#2
0
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            if (location == "path" && !paramDescription.IsRequired())
            {
                throw new NotSupportedException(string.Format(
                                                    "Route contains optional parameters for action - {0}. " +
                                                    "Optional route parameters are not allowed in Swagger 2.0",
                                                    apiDescription.ActionDescriptor.DisplayName));
            }

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
示例#3
0
        private OpenApiRequestBody GenerateRequestBodyFromBodyParameter(
            ApiDescription apiDescription,
            SchemaRepository schemaRepository,
            ApiParameterDescription bodyParameter)
        {
            var contentTypes = InferRequestContentTypes(apiDescription);

            var isRequired = bodyParameter.IsRequired();

            var schema = _schemaGenerator.GenerateSchema(
                bodyParameter.ModelMetadata.ModelType,
                schemaRepository,
                bodyParameter.PropertyInfo(),
                bodyParameter.ParameterInfo());

            return(new OpenApiRequestBody
            {
                Content = contentTypes
                          .ToDictionary(
                    contentType => contentType,
                    contentType => new OpenApiMediaType
                {
                    Schema = schema
                }
                    ),
                Required = isRequired
            });
        }
示例#4
0
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription apiParameterDescription,
            ISchemaRegistry schemaRegistry)
        {
            // Try to retrieve additional metadata that's not directly provided by ApiExplorer
            ParameterInfo parameterInfo    = null;
            PropertyInfo  propertyInfo     = null;
            var           customAttributes = Enumerable.Empty <object>();

            if (apiParameterDescription.TryGetParameterInfo(apiDescription, out parameterInfo))
            {
                customAttributes = parameterInfo.GetCustomAttributes(true);
            }
            else if (apiParameterDescription.TryGetPropertyInfo(out propertyInfo))
            {
                customAttributes = propertyInfo.GetCustomAttributes(true);
            }

            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameterDescription.Name.ToCamelCase()
                : apiParameterDescription.Name;

            var isRequired = apiParameterDescription.IsRequired() ?? customAttributes.Any(attr =>
                                                                                          new[] { typeof(RequiredAttribute), typeof(BindRequiredAttribute) }.Contains(attr.GetType()));

            var parameter = (apiParameterDescription.Source == BindingSource.Body)
                ? CreateBodyParameter(
                apiParameterDescription,
                name,
                isRequired,
                schemaRegistry)
                : CreateNonBodyParameter(
                apiParameterDescription,
                parameterInfo,
                customAttributes,
                name,
                isRequired,
                schemaRegistry);

            var filterContext = new ParameterFilterContext(
                apiParameterDescription,
                schemaRegistry,
                parameterInfo,
                propertyInfo);

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") || paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                // In some cases (e.g. enum types), the schemaRegistry may return a reference instead of a
                // full schema. Retrieve the full schema before populating the parameter description
                var fullSchema = (schema.Ref != null)
                    ? schemaRegistry.Definitions[schema.Ref.Replace("#/definitions/", string.Empty)]
                    : schema;

                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
示例#6
0
        private OpenApiParameter GenerateParameter(
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location = (apiParameter.Source != null && ParameterLocationMap.ContainsKey(apiParameter.Source))
                ? ParameterLocationMap[apiParameter.Source]
                : ParameterLocation.Query;

            var isRequired = apiParameter.IsRequired();

            var schema = (apiParameter.ModelMetadata != null)
                ? _schemaGenerator.GenerateSchema(
                apiParameter.ModelMetadata.ModelType,
                schemaRepository,
                apiParameter.PropertyInfo(),
                apiParameter.ParameterInfo())
                : new OpenApiSchema {
                Type = "string"
            };

            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRepository,
                apiParameter.PropertyInfo(),
                apiParameter.ParameterInfo());

            foreach (var filter in _options.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }
示例#7
0
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription paramDescription,
            ISchemaRegistry schemaRegistry)
        {
            var location = GetParameterLocation(apiDescription, paramDescription);

            var name = _settings.DescribeAllParametersInCamelCase
                ? paramDescription.Name.ToCamelCase()
                : paramDescription.Name;

            var schema = (paramDescription.Type == null) ? null : schemaRegistry.GetOrRegister(paramDescription.Type);

            if (location == "body")
            {
                return(new BodyParameter
                {
                    Name = name,
                    Schema = schema
                });
            }

            var nonBodyParam = new NonBodyParameter
            {
                Name     = name,
                In       = location,
                Required = (location == "path") || paramDescription.IsRequired()
            };

            if (schema == null)
            {
                nonBodyParam.Type = "string";
            }
            else
            {
                nonBodyParam.PopulateFrom(schema);
            }

            if (nonBodyParam.Type == "array")
            {
                nonBodyParam.CollectionFormat = "multi";
            }

            return(nonBodyParam);
        }
        private IParameter CreateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription apiParameterDescription,
            ISchemaRegistry schemaRegistry)
        {
            var name = _settings.DescribeAllParametersInCamelCase
                ? apiParameterDescription.Name.ToCamelCase()
                : apiParameterDescription.Name;

            var location = ParameterLocationMap.ContainsKey(apiParameterDescription.Source)
                ? ParameterLocationMap[apiParameterDescription.Source]
                : "query";

            var schema = (apiParameterDescription.Type != null)
                ? schemaRegistry.GetOrRegister(apiParameterDescription.Type)
                : null;

            var isRequired = apiParameterDescription.IsRequired();

            var controllerParameterDescriptor = GetControllerParameterDescriptorOrNull(
                apiDescription, apiParameterDescription);

            var parameter = (location == "body")
                ? new BodyParameter {
                Name = name, Schema = schema, Required = isRequired
            }
                : CreateNonBodyParameter(name, location, schema, isRequired, schemaRegistry);

            var filterContext = new ParameterFilterContext(
                apiParameterDescription,
                controllerParameterDescriptor,
                schemaRegistry);

            foreach (var filter in _settings.ParameterFilters)
            {
                filter.Apply(parameter, filterContext);
            }

            return(parameter);
        }