diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs index b4a72ed0..2563f6df 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs @@ -374,7 +374,7 @@ internal static OpenApiSchema CreateSchemaTypeSchema(this ODataContext context, } private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample, - IEnumerable derivedTypes = null) + IEnumerable derivedTypes = null) { Debug.Assert(context != null); Debug.Assert(structuredType != null); @@ -387,7 +387,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex if (context.Settings.EnableDiscriminatorValue && derivedTypes == null) { - derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType).OfType(); + derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType); } if (processBase && structuredType.BaseType != null) @@ -435,8 +435,8 @@ 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) - { + if (context.Settings.EnableDiscriminatorValue && derivedTypes.Any()) + { Dictionary mapping = derivedTypes .ToDictionary(x => $"#{x.FullTypeName()}", x => 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 3b9f6adb..1ef43f4a 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-preview2 + 1.0.11-preview3 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 @@ -26,6 +26,7 @@ - Add error ranges for OData actions when ErrorResponsesAsDefault is set to false #218 - Fixes missing bound operations on some navigation property paths #201 - Provides support for using success status code range 2XX #153 +- Adds discriminator object to complex types which have derived types #233 Microsoft.OpenApi.OData.Reader ..\..\tool\Microsoft.OpenApi.OData.snk diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs index 241299ec..272a2647 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs @@ -81,7 +81,7 @@ public void CreateStructuredTypeSchemaThrowArgumentNullEnumType() } [Fact] - public void CreateStructuredTypeSchemaWithDiscriminatorValueEnabledReturnsCorrectSchema() + public void CreateStructuredTypeSchemaForEntityTypeWithDiscriminatorValueEnabledReturnsCorrectSchema() { // Arrange IEdmModel model = EdmModelHelper.GraphBetaModel; @@ -142,6 +142,48 @@ public void CreateStructuredTypeSchemaWithDiscriminatorValueEnabledReturnsCorrec }".ChangeLineBreaks(), json); } + [Fact] + public void CreateStructuredTypeSchemaForComplexTypeWithDiscriminatorValueEnabledReturnsCorrectSchema() + { + // Arrange + IEdmModel model = EdmModelHelper.GraphBetaModel; + ODataContext context = new(model, new OpenApiConvertSettings + { + EnableDiscriminatorValue = true, + }); + + IEdmComplexType complex = model.SchemaElements.OfType().First(t => t.Name == "userSet"); + Assert.NotNull(complex); // Guard + + // Act + var schema = context.CreateStructuredTypeSchema(complex); + string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0); + + // Assert + Assert.NotNull(json); + Assert.Equal(@"{ + ""title"": ""userSet"", + ""type"": ""object"", + ""properties"": { + ""isBackup"": { + ""type"": ""boolean"", + ""nullable"": true + } + }, + ""discriminator"": { + ""propertyName"": ""@odata.type"", + ""mapping"": { + ""#microsoft.graph.connectedOrganizationMembers"": ""#/components/schemas/microsoft.graph.connectedOrganizationMembers"", + ""#microsoft.graph.externalSponsors"": ""#/components/schemas/microsoft.graph.externalSponsors"", + ""#microsoft.graph.groupMembers"": ""#/components/schemas/microsoft.graph.groupMembers"", + ""#microsoft.graph.internalSponsors"": ""#/components/schemas/microsoft.graph.internalSponsors"", + ""#microsoft.graph.requestorManager"": ""#/components/schemas/microsoft.graph.requestorManager"", + ""#microsoft.graph.singleUser"": ""#/components/schemas/microsoft.graph.singleUser"" + } + } +}".ChangeLineBreaks(), json); + } + [Fact] public void CreateStructuredTypePropertiesSchemaWithCustomAttributeReturnsCorrectSchema() {