Skip to content

Commit c8d4e3a

Browse files
Use anyOf in schema if ref is nullable
1 parent 36ddedc commit c8d4e3a

File tree

5 files changed

+26
-54
lines changed

5 files changed

+26
-54
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiEdmTypeSchemaGenerator.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ private static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEd
422422
OpenApiSchema schema = new OpenApiSchema();
423423
schema.Reference = null;
424424

425-
if (context.Settings.OpenApiSpecVersion >= OpenApiSpecVersion.OpenApi3_0)
425+
if (typeReference.IsNullable && context.Settings.OpenApiSpecVersion >= OpenApiSpecVersion.OpenApi3_0)
426426
{
427427
schema.AnyOf = new List<OpenApiSchema>
428428
{
@@ -434,17 +434,13 @@ private static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEd
434434
Type = ReferenceType.Schema,
435435
Id = typeReference.Definition.FullTypeName()
436436
}
437-
}
438-
};
439-
440-
if (typeReference.IsNullable)
441-
{
442-
schema.AnyOf.Add(new OpenApiSchema
437+
},
438+
new OpenApiSchema
443439
{
444440
Type = "object",
445441
Nullable = true
446-
});
447-
}
442+
}
443+
};
448444
}
449445
else
450446
{
@@ -518,7 +514,7 @@ private static OpenApiSchema CreateTypeDefinitionSchema(this ODataContext contex
518514
OpenApiSchema schema = new OpenApiSchema();
519515
schema.Reference = null;
520516

521-
if (context.Settings.OpenApiSpecVersion >= OpenApiSpecVersion.OpenApi3_0)
517+
if (reference.IsNullable && context.Settings.OpenApiSpecVersion >= OpenApiSpecVersion.OpenApi3_0)
522518
{
523519
schema.AnyOf = new List<OpenApiSchema>
524520
{
@@ -530,17 +526,13 @@ private static OpenApiSchema CreateTypeDefinitionSchema(this ODataContext contex
530526
Type = ReferenceType.Schema,
531527
Id = reference.Definition.FullTypeName()
532528
}
533-
}
534-
};
535-
536-
if (reference.IsNullable)
537-
{
538-
schema.AnyOf.Add(new OpenApiSchema
529+
},
530+
new OpenApiSchema
539531
{
540532
Type = "object",
541533
Nullable = true
542-
});
543-
}
534+
}
535+
};
544536
}
545537
else
546538
{

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiEdmTypeSchemaGeneratorTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,12 @@ public void CreateEdmTypeSchemaReturnSchemaForEnumType(bool isNullable, OpenApiS
199199
}
200200
else
201201
{
202-
Assert.Null(schema.Reference);
203-
Assert.NotNull(schema.AnyOf);
204-
Assert.NotEmpty(schema.AnyOf);
202+
205203
if (isNullable)
206204
{
205+
Assert.NotNull(schema.AnyOf);
206+
Assert.NotEmpty(schema.AnyOf);
207+
Assert.Null(schema.Reference);
207208
Assert.Equal(2, schema.AnyOf.Count);
208209
var anyOfRef = schema.AnyOf.FirstOrDefault();
209210
Assert.NotNull(anyOfRef.Reference);
@@ -216,11 +217,10 @@ public void CreateEdmTypeSchemaReturnSchemaForEnumType(bool isNullable, OpenApiS
216217
}
217218
else
218219
{
219-
Assert.Equal(1, schema.AnyOf.Count);
220-
var anyOf = Assert.Single(schema.AnyOf);
221-
Assert.NotNull(anyOf.Reference);
222-
Assert.Equal(ReferenceType.Schema, anyOf.Reference.Type);
223-
Assert.Equal(enumType.FullTypeName(), anyOf.Reference.Id);
220+
Assert.Null(schema.AnyOf);
221+
Assert.NotNull(schema.Reference);
222+
Assert.Equal(ReferenceType.Schema, schema.Reference.Type);
223+
Assert.Equal(enumType.FullTypeName(), schema.Reference.Id);
224224
}
225225
}
226226
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ public void CreatePropertySchemaForNonNullableEnumPropertyReturnSchema(OpenApiSp
672672

673673
IEdmEnumType enumType = model.SchemaElements.OfType<IEdmEnumType>().First(e => e.Name == "Color");
674674
EdmEntityType entitType = new EdmEntityType("NS", "Entity");
675-
IEdmProperty property = new EdmStructuralProperty(entitType, "ColorEnumValue", new EdmEnumTypeReference(enumType, false), "yellow");
675+
IEdmProperty property = new EdmStructuralProperty(entitType, "ColorEnumValue", new EdmEnumTypeReference(enumType, false));
676676

677677
// Act
678678
var schema = context.CreatePropertySchema(property);
@@ -690,12 +690,7 @@ public void CreatePropertySchemaForNonNullableEnumPropertyReturnSchema(OpenApiSp
690690
else
691691
{
692692
Assert.Equal(@"{
693-
""anyOf"": [
694-
{
695-
""$ref"": ""#/components/schemas/DefaultNs.Color""
696-
}
697-
],
698-
""default"": ""yellow""
693+
""$ref"": ""#/components/schemas/DefaultNs.Color""
699694
}".ChangeLineBreaks(), json);
700695
}
701696
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29242,11 +29242,7 @@
2924229242
"nullable": true
2924329243
},
2924429244
"Gender": {
29245-
"anyOf": [
29246-
{
29247-
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender"
29248-
}
29249-
]
29245+
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender"
2925029246
},
2925129247
"Age": {
2925229248
"anyOf": [
@@ -29293,20 +29289,12 @@
2929329289
]
2929429290
},
2929529291
"FavoriteFeature": {
29296-
"anyOf": [
29297-
{
29298-
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
29299-
}
29300-
]
29292+
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
2930129293
},
2930229294
"Features": {
2930329295
"type": "array",
2930429296
"items": {
29305-
"anyOf": [
29306-
{
29307-
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
29308-
}
29309-
]
29297+
"$ref": "#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
2931029298
}
2931129299
},
2931229300
"Friends": {

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20433,8 +20433,7 @@ components:
2043320433
type: string
2043420434
nullable: true
2043520435
Gender:
20436-
anyOf:
20437-
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender'
20436+
$ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender'
2043820437
Age:
2043920438
anyOf:
2044020439
- type: integer
@@ -20459,13 +20458,11 @@ components:
2045920458
- type: object
2046020459
nullable: true
2046120460
FavoriteFeature:
20462-
anyOf:
20463-
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature'
20461+
$ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature'
2046420462
Features:
2046520463
type: array
2046620464
items:
20467-
anyOf:
20468-
- $ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature'
20465+
$ref: '#/components/schemas/Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature'
2046920466
Friends:
2047020467
type: array
2047120468
items:

0 commit comments

Comments
 (0)