Skip to content

Commit

Permalink
Fix RequestBodyFilterAnnotation for Minimal API (#2963)
Browse files Browse the repository at this point in the history
Fix RequestBodyFilterAnnotation and multiple `[FromForm]` attributes for Minimal APIs.
  • Loading branch information
jgarciadelanoceda authored Jun 27, 2024
1 parent 9b7c0dd commit 7f7f46f
Show file tree
Hide file tree
Showing 26 changed files with 3,673 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ private void ApplyPropertyAnnotations(OpenApiRequestBody parameter, PropertyInfo

private void ApplyParamAnnotations(OpenApiRequestBody requestBody, ParameterInfo parameterInfo)
{
var swaggerRequestBodyAttribute = parameterInfo.GetCustomAttributes<SwaggerRequestBodyAttribute>()
.FirstOrDefault();
var swaggerRequestBodyAttribute = parameterInfo.GetCustomAttribute<SwaggerRequestBodyAttribute>();

if (swaggerRequestBodyAttribute != null)
ApplySwaggerRequestBodyAttribute(requestBody, swaggerRequestBodyAttribute);
Expand All @@ -55,4 +54,4 @@ private void ApplySwaggerRequestBodyAttribute(OpenApiRequestBody parameter, Swag
parameter.Required = swaggerRequestBodyAttribute.RequiredFlag.Value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,32 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
{
foreach (var content in requestContentTypes)
{
var requestParameter = apiDescription.ParameterDescriptions.SingleOrDefault(desc => desc.IsFromBody() || desc.IsFromForm());
if (requestParameter is not null)
var requestParameters = apiDescription.ParameterDescriptions.Where(desc => desc.IsFromBody() || desc.IsFromForm());
var countOfParameters = requestParameters.Count();
if (countOfParameters > 0)
{
content.Schema = GenerateSchema(
requestParameter.ModelMetadata.ModelType,
schemaRepository,
requestParameter.PropertyInfo(),
requestParameter.ParameterInfo());
if (countOfParameters == 1)
{
var requestParameter = requestParameters.First();
content.Schema = GenerateSchema(
requestParameter.ModelMetadata.ModelType,
schemaRepository,
requestParameter.PropertyInfo(),
requestParameter.ParameterInfo());
}
else
{
content.Schema = new OpenApiSchema()
{
AllOf = requestParameters.Select(s =>
GenerateSchema(
s.ModelMetadata.ModelType,
schemaRepository,
s.PropertyInfo(),
s.ParameterInfo()))
.ToList()
};
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"openapi": "3.0.1",
"info": {
"title": "MvcWithNullable",
"version": "1.0"
},
"paths": {
"/api/Enum": {
"get": {
"tags": [
"Enum"
],
"parameters": [
{
"name": "logLevel",
"in": "query",
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/LogLevel"
}
],
"default": 4
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
"schemas": {
"LogLevel": {
"enum": [
0,
1,
2,
3,
4,
5,
6
],
"type": "integer",
"format": "int32"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
{
"openapi": "3.0.1",
"info": {
"title": "WebApi",
"version": "v1"
},
"paths": {
"/annotations/fruit/{id}": {
"post": {
"tags": [
"Annotations"
],
"summary": "CreateFruit",
"description": "Create a fruit",
"parameters": [
{
"name": "id",
"in": "path",
"description": "The id of the fruit that will be created",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"description": "Description for Body",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Fruit"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Description for response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Fruit"
}
}
}
}
}
}
},
"/WithOpenApi/weatherforecast": {
"get": {
"tags": [
"WithOpenApi"
],
"operationId": "GetWeatherForecast",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
}
}
}
}
}
},
"/WithOpenApi/multipleForms": {
"post": {
"tags": [
"WithOpenApi"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Person"
},
{
"$ref": "#/components/schemas/Address"
}
]
}
},
"application/x-www-form-urlencoded": {
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/Person"
},
{
"$ref": "#/components/schemas/Address"
}
]
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/XmlComments/Car/{id}": {
"get": {
"tags": [
"Xml"
],
"summary": "Returns a specific product",
"parameters": [
{
"name": "id",
"in": "path",
"description": "The product id",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
},
"example": 111
}
],
"responses": {
"200": {
"description": "A Product Id",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Address": {
"type": "object",
"properties": {
"street": {
"type": "string",
"nullable": true
},
"city": {
"type": "string",
"nullable": true
},
"state": {
"type": "string",
"nullable": true
},
"zipCode": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
"Fruit": {
"type": "object",
"properties": {
"name": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false,
"description": "Description for Schema"
},
"Person": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"nullable": true
},
"lastName": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
},
"Product": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"description": "Uniquely identifies the product",
"format": "int32"
},
"description": {
"type": "string",
"description": "Describes the product",
"nullable": true
}
},
"additionalProperties": false,
"description": "Represents a product"
},
"WeatherForecast": {
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date"
},
"temperatureC": {
"type": "integer",
"format": "int32"
},
"summary": {
"type": "string",
"nullable": true
},
"temperatureF": {
"type": "integer",
"format": "int32",
"readOnly": true
}
},
"additionalProperties": false
}
}
}
}
Loading

0 comments on commit 7f7f46f

Please sign in to comment.