From e727e8d53cd9f7edebe5faa6b8bf183e19afb0c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Garc=C3=ADa=20de=20la=20Noceda=20Arg=C3=BCelles?= Date: Fri, 22 Nov 2024 08:43:18 +0100 Subject: [PATCH] Fix issue with FromForm enums (#3164) Fix issue with .NET 7+. --- .../SwaggerGenerator/SwaggerGenerator.cs | 2 +- ...lidSwaggerJson_Basic_DotNet_8.verified.txt | 57 +++++++++++-------- ....Startup_swaggerRequestUri=v1.verified.txt | 55 +++++++++--------- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 08587282dd..b709141643 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -851,7 +851,7 @@ private OpenApiSchema GenerateSchemaFromFormParameters( : new OpenApiSchema { Type = "string" }; if (schema.Reference is null - || (formParameter.Type is not null && (Nullable.GetUnderlyingType(formParameter.Type) ?? formParameter.Type).IsEnum)) + || (formParameter.ModelMetadata?.ModelType is not null && (Nullable.GetUnderlyingType(formParameter.ModelMetadata.ModelType) ?? formParameter.ModelMetadata.ModelType).IsEnum)) { var name = _options.DescribeAllParametersInCamelCase ? formParameter.Name.ToCamelCase() diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_8.verified.txt b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_8.verified.txt index 275069b624..80bafac961 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_8.verified.txt +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_Basic_DotNet_8.verified.txt @@ -675,32 +675,33 @@ content: { multipart/form-data: { schema: { - allOf: [ - null, - null, - { - type: object, - properties: { - name: { - type: string, - description: Summary for Name, - example: MyName - }, - phoneNumbers: { - type: array, - items: { - type: integer, - format: int32 - }, - description: Sumary for PhoneNumbers - }, - formFile: { - type: string, - format: binary - } - } + type: object, + properties: { + name: { + type: string, + description: Summary for Name, + example: MyName + }, + phoneNumbers: { + type: array, + items: { + type: integer, + format: int32 + }, + description: Sumary for PhoneNumbers + }, + logLevel: { + $ref: #/components/schemas/LogLevel + }, + formFile: { + type: string, + description: Description for file, + format: binary + }, + dateTimeKind: { + $ref: #/components/schemas/DateTimeKind } - ] + } }, encoding: { name: { @@ -709,8 +710,14 @@ phoneNumbers: { style: form }, + logLevel: { + style: form + }, formFile: { style: form + }, + dateTimeKind: { + style: form } } } diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt index 00b253f2e0..afe5520fc4 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt @@ -684,36 +684,33 @@ "content": { "multipart/form-data": { "schema": { - "allOf": [ - { + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Summary for Name", + "example": "MyName" + }, + "phoneNumbers": { + "type": "array", + "items": { + "type": "integer", + "format": "int32" + }, + "description": "Sumary for PhoneNumbers" + }, + "logLevel": { "$ref": "#/components/schemas/LogLevel" }, - { - "$ref": "#/components/schemas/DateTimeKind" + "formFile": { + "type": "string", + "description": "Description for file", + "format": "binary" }, - { - "type": "object", - "properties": { - "name": { - "type": "string", - "description": "Summary for Name", - "example": "MyName" - }, - "phoneNumbers": { - "type": "array", - "items": { - "type": "integer", - "format": "int32" - }, - "description": "Sumary for PhoneNumbers" - }, - "formFile": { - "type": "string", - "format": "binary" - } - } + "dateTimeKind": { + "$ref": "#/components/schemas/DateTimeKind" } - ] + } }, "encoding": { "name": { @@ -722,8 +719,14 @@ "phoneNumbers": { "style": "form" }, + "logLevel": { + "style": "form" + }, "formFile": { "style": "form" + }, + "dateTimeKind": { + "style": "form" } } }