Skip to content

Commit 2a6af62

Browse files
committed
fix for #3166
add concrete schema for discriminated types into 'oneOf' for base type
1 parent 76f8bc6 commit 2a6af62

3 files changed

+37
-2
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private bool IsBaseTypeWithKnownTypesDefined(DataContract dataContract, out IEnu
211211
? new[] { dataContract.UnderlyingType }.Union(subTypes)
212212
: subTypes;
213213

214-
knownTypesDataContracts = knownTypes.Select(knownType => GetDataContractFor(knownType));
214+
knownTypesDataContracts = knownTypes.Select(GetDataContractFor);
215215
return true;
216216
}
217217

@@ -415,7 +415,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
415415
foreach (var knownTypeDataContract in knownTypesDataContracts)
416416
{
417417
// Ensure schema is generated for all known types
418-
GenerateConcreteSchema(knownTypeDataContract, schemaRepository);
418+
schema.OneOf.Add(GenerateConcreteSchema(knownTypeDataContract, schemaRepository));
419419
}
420420

421421
if (TryGetDiscriminatorFor(dataContract, schemaRepository, knownTypesDataContracts, out var discriminator))

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt

+8
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,14 @@
15501550
"TypeName"
15511551
],
15521552
"type": "object",
1553+
"oneOf": [
1554+
{
1555+
"$ref": "#/components/schemas/Rectangle"
1556+
},
1557+
{
1558+
"$ref": "#/components/schemas/Circle"
1559+
}
1560+
],
15531561
"properties": {
15541562
"TypeName": {
15551563
"type": "string"

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=NSwagClientExample.Startup_swaggerRequestUri=v1.verified.txt

+27
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@
188188
"animalType"
189189
],
190190
"type": "object",
191+
"oneOf": [
192+
{
193+
"$ref": "#/components/schemas/Animal"
194+
},
195+
{
196+
"$ref": "#/components/schemas/Cat"
197+
},
198+
{
199+
"$ref": "#/components/schemas/Dog"
200+
}
201+
],
191202
"properties": {
192203
"animalType": {
193204
"$ref": "#/components/schemas/AnimalType"
@@ -214,6 +225,11 @@
214225
"discriminator"
215226
],
216227
"type": "object",
228+
"oneOf": [
229+
{
230+
"$ref": "#/components/schemas/SubSubType"
231+
}
232+
],
217233
"properties": {
218234
"discriminator": {
219235
"type": "string"
@@ -287,6 +303,17 @@
287303
"animalType"
288304
],
289305
"type": "object",
306+
"oneOf": [
307+
{
308+
"$ref": "#/components/schemas/SystemTextJsonAnimal"
309+
},
310+
{
311+
"$ref": "#/components/schemas/SystemTextJsonCat"
312+
},
313+
{
314+
"$ref": "#/components/schemas/SystemTextJsonDog"
315+
}
316+
],
290317
"properties": {
291318
"animalType": {
292319
"type": "string",

0 commit comments

Comments
 (0)