From 2a6af62d99a6a56f4d450eb62b5a75d04d53b0b0 Mon Sep 17 00:00:00 2001 From: Evgenii Zakharov Date: Wed, 5 Mar 2025 22:06:31 +0300 Subject: [PATCH] fix for #3166 add concrete schema for discriminated types into 'oneOf' for base type --- .../SchemaGenerator/SchemaGenerator.cs | 4 +-- ....Startup_swaggerRequestUri=v1.verified.txt | 8 ++++++ ....Startup_swaggerRequestUri=v1.verified.txt | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs index 8e5a39ad42..3344abd60c 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs @@ -211,7 +211,7 @@ private bool IsBaseTypeWithKnownTypesDefined(DataContract dataContract, out IEnu ? new[] { dataContract.UnderlyingType }.Union(subTypes) : subTypes; - knownTypesDataContracts = knownTypes.Select(knownType => GetDataContractFor(knownType)); + knownTypesDataContracts = knownTypes.Select(GetDataContractFor); return true; } @@ -415,7 +415,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi foreach (var knownTypeDataContract in knownTypesDataContracts) { // Ensure schema is generated for all known types - GenerateConcreteSchema(knownTypeDataContract, schemaRepository); + schema.OneOf.Add(GenerateConcreteSchema(knownTypeDataContract, schemaRepository)); } if (TryGetDiscriminatorFor(dataContract, schemaRepository, knownTypesDataContracts, out var discriminator)) 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 32e5af36f3..8e344f7f1c 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 @@ -1550,6 +1550,14 @@ "TypeName" ], "type": "object", + "oneOf": [ + { + "$ref": "#/components/schemas/Rectangle" + }, + { + "$ref": "#/components/schemas/Circle" + } + ], "properties": { "TypeName": { "type": "string" diff --git a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=NSwagClientExample.Startup_swaggerRequestUri=v1.verified.txt b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=NSwagClientExample.Startup_swaggerRequestUri=v1.verified.txt index 7671c6817e..58633a9973 100644 --- a/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=NSwagClientExample.Startup_swaggerRequestUri=v1.verified.txt +++ b/test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=NSwagClientExample.Startup_swaggerRequestUri=v1.verified.txt @@ -188,6 +188,17 @@ "animalType" ], "type": "object", + "oneOf": [ + { + "$ref": "#/components/schemas/Animal" + }, + { + "$ref": "#/components/schemas/Cat" + }, + { + "$ref": "#/components/schemas/Dog" + } + ], "properties": { "animalType": { "$ref": "#/components/schemas/AnimalType" @@ -214,6 +225,11 @@ "discriminator" ], "type": "object", + "oneOf": [ + { + "$ref": "#/components/schemas/SubSubType" + } + ], "properties": { "discriminator": { "type": "string" @@ -287,6 +303,17 @@ "animalType" ], "type": "object", + "oneOf": [ + { + "$ref": "#/components/schemas/SystemTextJsonAnimal" + }, + { + "$ref": "#/components/schemas/SystemTextJsonCat" + }, + { + "$ref": "#/components/schemas/SystemTextJsonDog" + } + ], "properties": { "animalType": { "type": "string",