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",