Skip to content

Commit b23764a

Browse files
Merge branch 'master' into fix/requestBodyAndResponseRepresentationForRefOperations
2 parents 3f87afb + 6ac465d commit b23764a

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @xuzhg @irvinesunday @darrelmiller @peombwa @zengin
1+
* @xuzhg @irvinesunday @darrelmiller @peombwa @zengin @baywet @millicentachieng

src/Microsoft.OpenApi.OData.Reader/Common/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,10 @@ internal static class Constants
119119
/// Name used for reference request PUT body
120120
/// </summary>
121121
public static string ReferencePutRequestBodyName = "refPutBody";
122+
123+
/// <summary>
124+
/// The odata type name.
125+
/// </summary>
126+
public static string OdataType = "@odata.type";
122127
}
123128
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static IDictionary<string, OpenApiSchema> CreateSchemas(this ODataContext
102102
Properties = new Dictionary<string, OpenApiSchema>
103103
{
104104
{"@odata.id", new OpenApiSchema { Type = "string", Nullable = false }},
105-
{"@odata.type", new OpenApiSchema { Type = "string", Nullable = true }},
105+
{Constants.OdataType, new OpenApiSchema { Type = "string", Nullable = true }},
106106
}
107107
};
108108
}
@@ -459,13 +459,13 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
459459

460460
discriminator = new OpenApiDiscriminator
461461
{
462-
PropertyName = "@odata.type",
462+
PropertyName = Constants.OdataType,
463463
Mapping = mapping
464464
};
465465
}
466466

467467
// A structured type without a base type is represented as a Schema Object of type object
468-
OpenApiSchema schema = new OpenApiSchema
468+
OpenApiSchema schema = new()
469469
{
470470
Title = (structuredType as IEdmSchemaElement)?.Name,
471471

@@ -483,6 +483,20 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
483483
AnyOf = null
484484
};
485485

486+
if (schema.Discriminator != null)
487+
{
488+
if (!schema.Properties.TryAdd(Constants.OdataType, new OpenApiSchema()
489+
{
490+
Type = "string",
491+
Default = new OpenApiString("#" + structuredType.FullTypeName()),
492+
}))
493+
{
494+
throw new InvalidOperationException(
495+
$"Property {Constants.OdataType} is already present in schema {structuredType.FullTypeName()}; verify CSDL.");
496+
}
497+
schema.Required.Add(Constants.OdataType);
498+
}
499+
486500
// It optionally can contain the field description,
487501
// whose value is the value of the unqualified annotation Core.Description of the structured type.
488502
if (structuredType.TypeKind == EdmTypeKind.Complex)
@@ -569,7 +583,7 @@ private static IOpenApiAny GetTypeNameForExample(ODataContext context, IEdmTypeR
569583
case EdmTypeKind.Complex:
570584
case EdmTypeKind.Enum:
571585
OpenApiObject obj = new OpenApiObject();
572-
obj["@odata.type"] = new OpenApiString(edmTypeReference.FullName());
586+
obj[Constants.OdataType] = new OpenApiString(edmTypeReference.FullName());
573587
return obj;
574588

575589
case EdmTypeKind.Collection:

src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1616
<PackageId>Microsoft.OpenApi.OData</PackageId>
1717
<SignAssembly>true</SignAssembly>
18-
<Version>1.0.11-preview3</Version>
18+
<Version>1.0.11-preview4</Version>
1919
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
2020
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
2121
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
@@ -28,6 +28,7 @@
2828
- Provides support for using success status code range 2XX #153
2929
- Fixes request body and response representation for ref POST and PUT operations #228
3030
- Adds discriminator object to complex types which have derived types #233
31+
- Adds @odata.type property and makes this a required property in schemas that have discriminator objects #243
3132
</PackageReleaseNotes>
3233
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
3334
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,20 @@ public void CreateStructuredTypeSchemaForEntityTypeWithDiscriminatorValueEnabled
130130
},
131131
{
132132
""title"": ""directoryObject"",
133+
""required"": [
134+
""@odata.type""
135+
],
133136
""type"": ""object"",
134137
""properties"": {
135138
""deletedDateTime"": {
136139
""pattern"": ""^[0-9]{4,}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,12})?(Z|[+-][0-9][0-9]:[0-9][0-9])$"",
137140
""type"": ""string"",
138141
""format"": ""date-time"",
139142
""nullable"": true
143+
},
144+
""@odata.type"": {
145+
""type"": ""string"",
146+
""default"": ""#microsoft.graph.directoryObject""
140147
}
141148
},
142149
""discriminator"": {
@@ -187,11 +194,18 @@ public void CreateStructuredTypeSchemaForComplexTypeWithDiscriminatorValueEnable
187194
Assert.NotNull(json);
188195
Assert.Equal(@"{
189196
""title"": ""userSet"",
197+
""required"": [
198+
""@odata.type""
199+
],
190200
""type"": ""object"",
191201
""properties"": {
192202
""isBackup"": {
193203
""type"": ""boolean"",
194204
""nullable"": true
205+
},
206+
""@odata.type"": {
207+
""type"": ""string"",
208+
""default"": ""#microsoft.graph.userSet""
195209
}
196210
},
197211
""discriminator"": {

0 commit comments

Comments
 (0)