From bf2803dd12f8b22bfe32741c0178240fdf7f36b0 Mon Sep 17 00:00:00 2001 From: Irvine Sunday <40403681+irvinesunday@users.noreply.github.com> Date: Mon, 18 Jul 2022 12:23:09 +0300 Subject: [PATCH] List all derived types in discriminator mapping and append `@odata.type` property to all derived types (#256) * Find all derived types; append odata.type property to all derived types * Bump up package version and add release notes --- .../Generator/OpenApiSchemaGenerator.cs | 4 ++-- .../Microsoft.OpenAPI.OData.Reader.csproj | 4 +++- .../Generator/OpenApiSchemaGeneratorTests.cs | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs index a639eecd..279fb69e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs @@ -398,7 +398,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex if (context.Settings.EnableDiscriminatorValue && derivedTypes == null) { - derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType); + derivedTypes = context.Model.FindAllDerivedTypes(structuredType); } if (processBase && structuredType.BaseType != null) @@ -484,7 +484,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex AnyOf = null }; - if (schema.Discriminator != null) + if (context.Settings.EnableDiscriminatorValue) { if (!schema.Properties.TryAdd(Constants.OdataType, new OpenApiSchema() { diff --git a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj index 9d33b1c3..7effe22e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj +++ b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj @@ -15,7 +15,7 @@ netstandard2.0 Microsoft.OpenApi.OData true - 1.0.11-preview5 + 1.0.11 This package contains the codes you need to convert OData CSDL to Open API Document of Model. © Microsoft Corporation. All rights reserved. Microsoft OpenApi OData EDM @@ -28,7 +28,9 @@ - Provides support for using success status code range 2XX #153 - Fixes request body and response representation for ref POST and PUT operations #228 - Adds discriminator object to complex types which have derived types #233 +- Lists all the derived types of a type in discriminator mapping #240 - Adds @odata.type property and makes this a required property in schemas that have discriminator objects #243 +- Appends `@odata.type` property to all derived types schemas #248 - Represent nullable references within anyOf correctly #190 Microsoft.OpenApi.OData.Reader diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs index c1bd4f4e..c9879c1e 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs @@ -114,14 +114,19 @@ public void CreateStructuredTypeSchemaForEntityTypeWithDiscriminatorValueEnabled EnableDiscriminatorValue = true, }); + string derivedType = "user"; IEdmEntityType entity = model.SchemaElements.OfType().First(t => t.Name == "directoryObject"); - Assert.NotNull(entity); // Guard + IEdmEntityType derivedEntity = model.SchemaElements.OfType().First(t => t.Name == derivedType); + Assert.NotNull(entity); + Assert.NotNull(derivedEntity); // Act var schema = context.CreateStructuredTypeSchema(entity); + var derivedSchema = context.CreateStructuredTypeSchema(derivedEntity); string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); // Assert + Assert.True(derivedSchema.AllOf.FirstOrDefault(x => derivedType.Equals(x.Title))?.Properties.ContainsKey("@odata.type")); Assert.NotNull(json); Assert.Equal(@"{ ""allOf"": [ @@ -155,6 +160,18 @@ public void CreateStructuredTypeSchemaForEntityTypeWithDiscriminatorValueEnabled ""#microsoft.graph.application"": ""#/components/schemas/microsoft.graph.application"", ""#microsoft.graph.servicePrincipal"": ""#/components/schemas/microsoft.graph.servicePrincipal"", ""#microsoft.graph.policyBase"": ""#/components/schemas/microsoft.graph.policyBase"", + ""#microsoft.graph.appManagementPolicy"": ""#/components/schemas/microsoft.graph.appManagementPolicy"", + ""#microsoft.graph.stsPolicy"": ""#/components/schemas/microsoft.graph.stsPolicy"", + ""#microsoft.graph.homeRealmDiscoveryPolicy"": ""#/components/schemas/microsoft.graph.homeRealmDiscoveryPolicy"", + ""#microsoft.graph.tokenIssuancePolicy"": ""#/components/schemas/microsoft.graph.tokenIssuancePolicy"", + ""#microsoft.graph.tokenLifetimePolicy"": ""#/components/schemas/microsoft.graph.tokenLifetimePolicy"", + ""#microsoft.graph.claimsMappingPolicy"": ""#/components/schemas/microsoft.graph.claimsMappingPolicy"", + ""#microsoft.graph.activityBasedTimeoutPolicy"": ""#/components/schemas/microsoft.graph.activityBasedTimeoutPolicy"", + ""#microsoft.graph.authorizationPolicy"": ""#/components/schemas/microsoft.graph.authorizationPolicy"", + ""#microsoft.graph.tenantAppManagementPolicy"": ""#/components/schemas/microsoft.graph.tenantAppManagementPolicy"", + ""#microsoft.graph.permissionGrantPolicy"": ""#/components/schemas/microsoft.graph.permissionGrantPolicy"", + ""#microsoft.graph.servicePrincipalCreationPolicy"": ""#/components/schemas/microsoft.graph.servicePrincipalCreationPolicy"", + ""#microsoft.graph.identitySecurityDefaultsEnforcementPolicy"": ""#/components/schemas/microsoft.graph.identitySecurityDefaultsEnforcementPolicy"", ""#microsoft.graph.extensionProperty"": ""#/components/schemas/microsoft.graph.extensionProperty"", ""#microsoft.graph.endpoint"": ""#/components/schemas/microsoft.graph.endpoint"", ""#microsoft.graph.resourceSpecificPermissionGrant"": ""#/components/schemas/microsoft.graph.resourceSpecificPermissionGrant"",