Skip to content

Commit 36a8583

Browse files
committed
fix: enum example values
Signed-off-by: Vincent Biret <[email protected]>
1 parent 37508b2 commit 36a8583

File tree

6 files changed

+90
-141
lines changed

6 files changed

+90
-141
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,12 @@ EdmTypeKind.Primitive when edmTypeReference.IsInt16() ||
687687
edmTypeReference.IsDouble() => 0,
688688
EdmTypeKind.Primitive => GetTypeNameForPrimitive(context, edmTypeReference, document),
689689

690-
EdmTypeKind.Entity or EdmTypeKind.Complex or EdmTypeKind.Enum => new JsonObject()
691-
{//TODO this is wrong for enums, and should instead use one of the enum members
690+
EdmTypeKind.Entity or EdmTypeKind.Complex => new JsonObject()
691+
{
692692
[Constants.OdataType] = edmTypeReference.FullName()
693693
},
694-
694+
EdmTypeKind.Enum when edmTypeReference.Definition is IEdmEnumType edmEnumType && edmEnumType.Members.FirstOrDefault()?.Name is string firstMemberName =>
695+
JsonValue.Create(firstMemberName),
695696
EdmTypeKind.Collection => new JsonArray(GetTypeNameForExample(context, edmTypeReference.AsCollection().ElementType(), document)),
696697
EdmTypeKind.TypeDefinition => GetTypeNameForExample(context, new EdmPrimitiveTypeReference(edmTypeReference.AsTypeDefinition().TypeDefinition().UnderlyingType, edmTypeReference.IsNullable), document),
697698
EdmTypeKind.Untyped => new JsonObject(),

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,8 @@ public async Task CreateComplexTypeWithBaseSchemaReturnCorrectSchema()
574574
}
575575
],
576576
""example"": {
577-
""Color"": {
578-
""@odata.type"": ""NS.Color""
579-
},
580-
""Continent"": {
581-
""@odata.type"": ""NS.Continent""
582-
},
577+
""Color"": ""Blue"",
578+
""Continent"": ""Asia"",
583579
""Name"": ""string"",
584580
""Price"": 0
585581
}

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34779,9 +34779,7 @@
3477934779
"FirstName": "string",
3478034780
"LastName": "string",
3478134781
"MiddleName": "string",
34782-
"Gender": {
34783-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender"
34784-
},
34782+
"Gender": "Male",
3478534783
"Age": 0,
3478634784
"Emails": [
3478734785
"string"
@@ -34794,13 +34792,9 @@
3479434792
"HomeAddress": {
3479534793
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location"
3479634794
},
34797-
"FavoriteFeature": {
34798-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
34799-
},
34795+
"FavoriteFeature": "Feature1",
3480034796
"Features": [
34801-
{
34802-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
34803-
}
34797+
"Feature1"
3480434798
],
3480534799
"Photo": "string",
3480634800
"Friends": [
@@ -34951,9 +34945,7 @@
3495134945
"FirstName": "string",
3495234946
"LastName": "string",
3495334947
"MiddleName": "string",
34954-
"Gender": {
34955-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender"
34956-
},
34948+
"Gender": "Male",
3495734949
"Age": 0,
3495834950
"Emails": [
3495934951
"string"
@@ -34966,13 +34958,9 @@
3496634958
"HomeAddress": {
3496734959
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location"
3496834960
},
34969-
"FavoriteFeature": {
34970-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
34971-
},
34961+
"FavoriteFeature": "Feature1",
3497234962
"Features": [
34973-
{
34974-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
34975-
}
34963+
"Feature1"
3497634964
],
3497734965
"Photo": "string",
3497834966
"Friends": [
@@ -35002,9 +34990,7 @@
3500234990
"FirstName": "string",
3500334991
"LastName": "string",
3500434992
"MiddleName": "string",
35005-
"Gender": {
35006-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender"
35007-
},
34993+
"Gender": "Male",
3500834994
"Age": 0,
3500934995
"Emails": [
3501034996
"string"
@@ -35017,13 +35003,9 @@
3501735003
"HomeAddress": {
3501835004
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location"
3501935005
},
35020-
"FavoriteFeature": {
35021-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
35022-
},
35006+
"FavoriteFeature": "Feature1",
3502335007
"Features": [
35024-
{
35025-
"@odata.type": "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature"
35026-
}
35008+
"Feature1"
3502735009
],
3502835010
"Photo": "string",
3502935011
"Friends": [

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23230,19 +23230,17 @@ components:
2323023230
FirstName: string
2323123231
LastName: string
2323223232
MiddleName: string
23233-
Gender:
23234-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender
23233+
Gender: Male
2323523234
Age: 0
2323623235
Emails:
2323723236
- string
2323823237
AddressInfo:
2323923238
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
2324023239
HomeAddress:
2324123240
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
23242-
FavoriteFeature:
23243-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23241+
FavoriteFeature: Feature1
2324423242
Features:
23245-
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23243+
- Feature1
2324623244
Photo: string
2324723245
Friends:
2324823246
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person
@@ -23347,19 +23345,17 @@ components:
2334723345
FirstName: string
2334823346
LastName: string
2334923347
MiddleName: string
23350-
Gender:
23351-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender
23348+
Gender: Male
2335223349
Age: 0
2335323350
Emails:
2335423351
- string
2335523352
AddressInfo:
2335623353
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
2335723354
HomeAddress:
2335823355
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
23359-
FavoriteFeature:
23360-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23356+
FavoriteFeature: Feature1
2336123357
Features:
23362-
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23358+
- Feature1
2336323359
Photo: string
2336423360
Friends:
2336523361
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person
@@ -23376,19 +23372,17 @@ components:
2337623372
FirstName: string
2337723373
LastName: string
2337823374
MiddleName: string
23379-
Gender:
23380-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender
23375+
Gender: Male
2338123376
Age: 0
2338223377
Emails:
2338323378
- string
2338423379
AddressInfo:
2338523380
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
2338623381
HomeAddress:
2338723382
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Location
23388-
FavoriteFeature:
23389-
'@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23383+
FavoriteFeature: Feature1
2339023384
Features:
23391-
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature
23385+
- Feature1
2339223386
Photo: string
2339323387
Friends:
2339423388
- '@odata.type': Microsoft.OData.Service.Sample.TrippinInMemory.Models.Person

0 commit comments

Comments
 (0)