示例#1
0
        private OpenApiRequestBody GenerateRequestBodyFromBodyParameter(ServiceEntry apiDescription,
                                                                        SchemaRepository schemaRepository, ParameterDescriptor bodyParameter)
        {
            var contentTypes = InferRequestContentTypes(apiDescription);
            var isRequired   =
                bodyParameter.Type.CustomAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));
            var schema = GenerateSchema(
                bodyParameter.Type,
                schemaRepository,
                null,
                bodyParameter.ParameterInfo);

            return(new OpenApiRequestBody
            {
                Content = contentTypes
                          .ToDictionary(
                    contentType => contentType,
                    contentType => new OpenApiMediaType
                {
                    Schema = schema
                }
                    ),
                Required = isRequired
            });
        }
示例#2
0
        private OpenApiRequestBody GenerateRequestBodyFromBodyParameter(
            ApiDescription apiDescription,
            SchemaRepository schemaRepository,
            ApiParameterDescription bodyParameter)
        {
            var contentTypes = InferRequestContentTypes(apiDescription);

            var isRequired = bodyParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            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
            });
        }
示例#3
0
        private OpenApiRequestBody GenerateRequestBodyFromBodyParameter(
            ApiDescription apiDescription,
            IEnumerable <object> methodAttributes,
            SchemaRepository schemaRepository,
            ApiParameterDescription bodyParameter)
        {
            var contentTypes = InferRequestContentTypes(apiDescription, methodAttributes);

            bodyParameter.GetAdditionalMetadata(apiDescription,
                                                out ParameterInfo parameterInfo,
                                                out PropertyInfo propertyInfo,
                                                out IEnumerable <object> parameterOrPropertyAttributes);

            var isRequired = parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            return(new OpenApiRequestBody
            {
                Required = isRequired,
                Content = contentTypes
                          .ToDictionary(
                    contentType => contentType,
                    contentType => new OpenApiMediaType
                {
                    Schema = _schemaGenerator.GenerateSchema(bodyParameter.Type, schemaRepository)
                }
                    )
            });
        }
示例#4
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.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

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

            // If it's NOT a reference schema, apply contextual metadata (i.e. from custom attributes)
            if (schema.Reference == null)
            {
                // Honor default value for optional action parameters
                var parameterInfo = apiParameter.ParameterInfo();
                if (parameterInfo != null && parameterInfo.HasDefaultValue)
                {
                    schema.Default = OpenApiAnyFactory.TryCreateFor(schema, parameterInfo.DefaultValue, out IOpenApiAny openApiAny)
                        ? openApiAny
                        : null;
                }

                schema.ApplyCustomAttributes(apiParameter.CustomAttributes());
            }

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

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

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

            return(parameter);
        }
        private OpenApiParameter GenerateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            apiParameter.GetAdditionalMetadata(
                apiDescription,
                out ParameterInfo parameterInfo,
                out PropertyInfo propertyInfo,
                out IEnumerable <object> parameterOrPropertyAttributes);

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

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

            var isRequired = (apiParameter.IsFromPath()) ||
                             parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var defaultValue = parameterInfo?.DefaultValue
                               ?? parameterOrPropertyAttributes.OfType <DefaultValueAttribute>().FirstOrDefault()?.Value;

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

            if (defaultValue != null && schema.Reference == null)
            {
                schema.Default = OpenApiAnyFactory.TryCreateFor(schema, defaultValue, out IOpenApiAny openApiAny)
                    ? openApiAny
                    : null;
            }

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

            var filterContext = new ParameterFilterContext(apiParameter, _schemaGenerator, schemaRepository, parameterInfo, propertyInfo);

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

            return(parameter);
        }
        private OpenApiParameter GenerateParameter(
            ApiParameterDescription apiParameter,
            SchemaRepository schemaRepository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

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

            var isRequired = (apiParameter.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

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

            var defaultValue = apiParameter.CustomAttributes().OfType <DefaultValueAttribute>().FirstOrDefault()?.Value
                               ?? apiParameter.ParameterInfo()?.DefaultValue;

            // NOTE: Oddly, ParameterInfo.DefaultValue returns DBNull if not optional, hence the additional check below
            if (schema.Reference == null && defaultValue != null && defaultValue != DBNull.Value)
            {
                schema.Default = OpenApiAnyFactory.TryCreateFor(schema, defaultValue, out IOpenApiAny openApiAny)
                    ? openApiAny
                    : null;
            }

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

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

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

            return(parameter);
        }
示例#7
0
        private OpenApiSchema GenerateSchemaFromFormParameters(IEnumerable <ParameterDescriptor> formParameters,
                                                               SchemaRepository schemaRepository)
        {
            var properties            = new Dictionary <string, OpenApiSchema>();
            var requiredPropertyNames = new List <string>();

            foreach (var formParameter in formParameters)
            {
                if (formParameter.IsSample)
                {
                    var name = _options.DescribeAllParametersInCamelCase
                        ? formParameter.Name.ToCamelCase()
                        : formParameter.Name;
                    var schema = GenerateSchema(
                        formParameter.Type,
                        schemaRepository,
                        null,
                        formParameter.ParameterInfo);
                    properties.Add(name, schema);
                    if (formParameter.Type.GetCustomAttributes()
                        .Any(attr => RequiredAttributeTypes.Contains(attr.GetType())))
                    {
                        requiredPropertyNames.Add(name);
                    }
                }
                else
                {
                    foreach (var propertyInfo in formParameter.Type.GetProperties())
                    {
                        var name = _options.DescribeAllParametersInCamelCase
                            ? propertyInfo.Name.ToCamelCase()
                            : propertyInfo.Name;
                        var schema = GenerateSchema(
                            formParameter.Type,
                            schemaRepository,
                            propertyInfo,
                            null);
                        properties.Add(name, schema);
                        if (propertyInfo.GetCustomAttributes()
                            .Any(attr => RequiredAttributeTypes.Contains(attr.GetType())))
                        {
                            requiredPropertyNames.Add(name);
                        }
                    }
                }
            }

            return(new OpenApiSchema
            {
                Type = "object",
                Properties = properties,
                Required = new SortedSet <string>(requiredPropertyNames)
            });
        }
示例#8
0
        private OpenApiParameter GenerateParameter(
            ApiDescription apiDescription,
            ApiParameterDescription apiParameter,
            ISchemaRegistry schemaRegistry)
        {
            apiParameter.GetAdditionalMetadata(
                apiDescription,
                out ParameterInfo parameterInfo,
                out PropertyInfo propertyInfo,
                out IEnumerable <object> parameterOrPropertyAttributes);

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

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

            var isRequired = (apiParameter.IsFromPath()) ||
                             parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = (apiParameter.Type != null)
                ? schemaRegistry.GetOrRegister(apiParameter.Type)
                : new OpenApiSchema {
                Type = "string"
            };

            // If it corresponds to an optional action parameter, assign the default value
            if (parameterInfo?.DefaultValue != null && schema.Reference == null)
            {
                schema.Default = OpenApiPrimitiveFactory.CreateFrom(parameterInfo.DefaultValue);
            }

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

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

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

            return(parameter);
        }
示例#9
0
        private IEnumerable <OpenApiParameter> GenerateComplexParameter(ParameterDescriptor apiParameter,
                                                                        SchemaRepository schemaRespository)
        {
            var parameters    = new List <OpenApiParameter>();
            var propertyInfos = apiParameter.Type.GetProperties();

            foreach (var propertyInfo in propertyInfos)
            {
                if (!propertyInfo.PropertyType.IsSample())
                {
                    throw new LmsException("指定QString 参数不允许指定复杂类型参数");
                }

                var name = apiParameter.From == ParameterFrom.Path ? apiParameter.Name : propertyInfo.Name;
                name = _options.DescribeAllParametersInCamelCase
                    ? name.ToCamelCase()
                    : name;
                var location   = ParameterLocationMap[apiParameter.From];
                var isRequired = (apiParameter.From == ParameterFrom.Path) ||
                                 apiParameter.Type.GetCustomAttributes().Any(attr =>
                                                                             RequiredAttributeTypes.Contains(attr.GetType()));

                var schema    = GenerateSchema(apiParameter.Type, schemaRespository, propertyInfo, null);
                var parameter = new OpenApiParameter
                {
                    Name     = name,
                    In       = location,
                    Required = isRequired,
                    Schema   = schema
                };

                var filterContext = new ParameterFilterContext(
                    apiParameter,
                    _schemaGenerator,
                    schemaRespository,
                    null,
                    apiParameter.ParameterInfo);

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

                parameters.Add(parameter);
            }

            return(parameters);
        }
示例#10
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.IsFromPath()) ||
                             apiParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            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);
        }
示例#11
0
        private OpenApiRequestBody GenerateRequestBody(
            ApiDescription apiDescription,
            IEnumerable <object> methodAttributes,
            SchemaRepository schemaRepository)
        {
            var requestContentTypes = InferRequestContentTypes(apiDescription, methodAttributes);

            if (!requestContentTypes.Any())
            {
                return(null);
            }

            var bodyParameter = apiDescription.ParameterDescriptions
                                .FirstOrDefault(paramDesc => paramDesc.IsFromBody());

            bool isRequired = false;

            if (bodyParameter != null)
            {
                bodyParameter.GetAdditionalMetadata(
                    apiDescription,
                    out ParameterInfo parameterInfo,
                    out PropertyInfo propertyInfo,
                    out IEnumerable <object> parameterOrPropertyAttributes);

                isRequired = parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));
            }

            return(new OpenApiRequestBody
            {
                Required = isRequired,
                Content = requestContentTypes
                          .ToDictionary(
                    contentType => contentType,
                    contentType => GenerateRequestMediaType(contentType, apiDescription, schemaRepository)
                    )
            });
        }
        private OpenApiSchema GenerateSchemaFromApiParameters(
            ApiDescription apiDescription,
            IEnumerable <ApiParameterDescription> apiParameters,
            SchemaRepository schemaRepository)
        {
            // First, map to a data structure that captures the pertinent metadata
            var parametersMetadata = apiParameters
                                     .Select(paramDesc =>
            {
                paramDesc.GetAdditionalMetadata(
                    apiDescription,
                    out ParameterInfo parameterInfo,
                    out PropertyInfo propertyInfo,
                    out IEnumerable <object> parameterOrPropertyAttributes);

                var name       = _options.DescribeAllParametersInCamelCase ? paramDesc.Name.ToCamelCase() : paramDesc.Name;
                var isRequired = parameterOrPropertyAttributes.Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));
                var schema     = _schemaGenerator.GenerateSchema(paramDesc.Type, schemaRepository);

                return(new
                {
                    Name = name,
                    IsRequired = isRequired,
                    Schema = schema
                });
            });

            return(new OpenApiSchema
            {
                Type = "object",
                Properties = parametersMetadata.ToDictionary(
                    metadata => metadata.Name,
                    metadata => metadata.Schema
                    ),
                Required = new SortedSet <string>(parametersMetadata.Where(m => m.IsRequired).Select(m => m.Name)),
            });
        }
示例#13
0
        private OpenApiParameter GenerateSampleParameter(ParameterDescriptor apiParameter,
                                                         SchemaRepository schemaRespository)
        {
            var name = _options.DescribeAllParametersInCamelCase
                ? apiParameter.Name.ToCamelCase()
                : apiParameter.Name;

            var location   = ParameterLocationMap[apiParameter.From];
            var isRequired = (apiParameter.From == ParameterFrom.Path) ||
                             apiParameter.Type.GetCustomAttributes()
                             .Any(attr => RequiredAttributeTypes.Contains(attr.GetType()));

            var schema = GenerateSchema(apiParameter.Type, schemaRespository, null,
                                        apiParameter.ParameterInfo);
            var parameter = new OpenApiParameter
            {
                Name     = name,
                In       = location,
                Required = isRequired,
                Schema   = schema
            };

            var filterContext = new ParameterFilterContext(
                apiParameter,
                _schemaGenerator,
                schemaRespository,
                null,
                apiParameter.ParameterInfo);

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

            return(parameter);
        }
示例#14
0
        private OpenApiSchema GenerateSchemaFromFormParameters(
            IEnumerable <ApiParameterDescription> formParameters,
            SchemaRepository schemaRepository)
        {
            var properties            = new Dictionary <string, OpenApiSchema>();
            var requiredPropertyNames = new List <string>();

            foreach (var formParameter in formParameters)
            {
                var name = _options.DescribeAllParametersInCamelCase
                    ? formParameter.Name.ToCamelCase()
                    : formParameter.Name;

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

                properties.Add(name, schema);

                if (formParameter.IsFromPath() || formParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType())))
                {
                    requiredPropertyNames.Add(name);
                }
            }

            return(new OpenApiSchema
            {
                Type = "object",
                Properties = properties,
                Required = new SortedSet <string>(requiredPropertyNames)
            });
        }
        private OpenApiSchema GenerateSchemaFromFormParameters(
            IEnumerable <ApiParameterDescription> formParameters,
            SchemaRepository schemaRepository)
        {
            var properties            = new Dictionary <string, OpenApiSchema>();
            var requiredPropertyNames = new List <string>();

            foreach (var formParameter in formParameters)
            {
                var name = _options.DescribeAllParametersInCamelCase
                    ? formParameter.Name.ToCamelCase()
                    : formParameter.Name;

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

                var defaultValue = formParameter.CustomAttributes().OfType <DefaultValueAttribute>().FirstOrDefault()?.Value
                                   ?? formParameter.ParameterInfo()?.DefaultValue;

                // NOTE: Oddly, ParameterInfo.DefaultValue returns DBNull if not optional, hence the additional check below
                if (schema.Reference == null && defaultValue != null && defaultValue != DBNull.Value)
                {
                    schema.Default = OpenApiAnyFactory.TryCreateFor(schema, defaultValue, out IOpenApiAny openApiAny)
                        ? openApiAny
                        : null;
                }

                properties.Add(name, schema);

                if (formParameter.IsFromPath() || formParameter.CustomAttributes().Any(attr => RequiredAttributeTypes.Contains(attr.GetType())))
                {
                    requiredPropertyNames.Add(name);
                }
            }

            return(new OpenApiSchema
            {
                Type = "object",
                Properties = properties,
                Required = new SortedSet <string>(requiredPropertyNames)
            });
        }