From 816d30ffbf0b81371a7c61972e28d2b3ef958cc5 Mon Sep 17 00:00:00 2001 From: Millicent Achieng Date: Mon, 30 May 2022 07:59:18 +0300 Subject: [PATCH] Include correct list of discriminator information (#222) * Retrieve list of derived types for structured types * Update tests for creating structured type schema with discriminator * Simplify how mapping property of dicriminator object is computed * Update release notes Co-authored-by: Irvine Sunday --- .../Generator/OpenApiSchemaGenerator.cs | 22 ++++++++-------- .../Microsoft.OpenAPI.OData.Reader.csproj | 25 +++---------------- .../Generator/OpenApiSchemaGeneratorTests.cs | 18 ++++++++++++- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs index a60818c3..b4a72ed0 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs @@ -436,23 +436,21 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex // The discriminator object is added to structured types which have derived types. OpenApiDiscriminator discriminator = null; if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any() && structuredType.BaseType != null) - { - string v3RefIdentifier = new OpenApiSchema - { - Reference = new OpenApiReference + { + Dictionary mapping = derivedTypes + .ToDictionary(x => $"#{x.FullTypeName()}", x => new OpenApiSchema { - Type = ReferenceType.Schema, - Id = structuredType.FullTypeName() - } - }.Reference.ReferenceV3; + Reference = new OpenApiReference + { + Type = ReferenceType.Schema, + Id = x.FullTypeName() + } + }.Reference.ReferenceV3); discriminator = new OpenApiDiscriminator { PropertyName = "@odata.type", - Mapping = new Dictionary - { - {"#" + structuredType.FullTypeName(), v3RefIdentifier } - } + Mapping = mapping }; } 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 c3107b24..36f76183 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj +++ b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj @@ -15,33 +15,14 @@ netstandard2.0 Microsoft.OpenApi.OData true - 1.0.10 + 1.0.11-preview1 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 https://github.com/Microsoft/OpenAPI.NET.OData -- Adds support for reading custom attributes #192 -- Adds support for OpenApiLinks for actions and functions #74 -- Maps operation summaries to operation descriptions and CSDL description annotations to operation LongDescription #206 -- Fixes the response type of functions returning Edm.Stream to use application/octet-stream #200 -- Fixes the generation of ODataTypeCast paths #199 -- Adds path items for properties of complex type. #176, #15 -- Describes errors on error response codes instead of default. #172, #165, #193 -- Fixes a bug where reference objects are not created correctly. #171 -- Adds support for enum values descriptions. #164 -- Declares path parameters on path item instead of operation. #159 -- Multiple fixes on navigation properties path items expansion. #151, #123 -- Multiple fixes for descriptions. #154 -- Fixes a bug where URL templates would be missing quotes for string parameters. #140 -- Fixes description of operations of primitive types. #126 -- Adds support for OData cast segments in path items generation. #123 -- Fixes the response descriptions for functions that return a collection. #122 -- Fixes structured and collection-valued parameters of functions. #203 -- Adds support for discriminator property in inheritance. #118 -- Sets deprecated for operations that are generated from deprecated types/singletons/entitysets/properties in the model. #113 -- Aligns target version to netframework2.0. #139 - +- Adds list of all derived types for discriminator mapping #219 + Microsoft.OpenApi.OData.Reader ..\..\tool\Microsoft.OpenApi.OData.snk ..\..\bin\Debug\ diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs index 98f86e49..241299ec 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs @@ -118,7 +118,23 @@ public void CreateStructuredTypeSchemaWithDiscriminatorValueEnabledReturnsCorrec ""discriminator"": { ""propertyName"": ""@odata.type"", ""mapping"": { - ""#microsoft.graph.directoryObject"": ""#/components/schemas/microsoft.graph.directoryObject"" + ""#microsoft.graph.user"": ""#/components/schemas/microsoft.graph.user"", + ""#microsoft.graph.group"": ""#/components/schemas/microsoft.graph.group"", + ""#microsoft.graph.device"": ""#/components/schemas/microsoft.graph.device"", + ""#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.extensionProperty"": ""#/components/schemas/microsoft.graph.extensionProperty"", + ""#microsoft.graph.endpoint"": ""#/components/schemas/microsoft.graph.endpoint"", + ""#microsoft.graph.resourceSpecificPermissionGrant"": ""#/components/schemas/microsoft.graph.resourceSpecificPermissionGrant"", + ""#microsoft.graph.administrativeUnit"": ""#/components/schemas/microsoft.graph.administrativeUnit"", + ""#microsoft.graph.contract"": ""#/components/schemas/microsoft.graph.contract"", + ""#microsoft.graph.directoryObjectPartnerReference"": ""#/components/schemas/microsoft.graph.directoryObjectPartnerReference"", + ""#microsoft.graph.directoryRole"": ""#/components/schemas/microsoft.graph.directoryRole"", + ""#microsoft.graph.directoryRoleTemplate"": ""#/components/schemas/microsoft.graph.directoryRoleTemplate"", + ""#microsoft.graph.directorySettingTemplate"": ""#/components/schemas/microsoft.graph.directorySettingTemplate"", + ""#microsoft.graph.organization"": ""#/components/schemas/microsoft.graph.organization"", + ""#microsoft.graph.orgContact"": ""#/components/schemas/microsoft.graph.orgContact"" } } }