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) } ) }); }
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( 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); }