diff --git a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj index f07012b5d..fa8bee8bf 100644 --- a/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj +++ b/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj @@ -11,7 +11,7 @@ Microsoft Microsoft.OpenApi Microsoft.OpenApi - 1.4.0 + 1.4.1 .NET models with JSON and YAML writers for OpenAPI specification © Microsoft Corporation. All rights reserved. OpenAPI .NET diff --git a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs index 8734c19a2..6019d7362 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiSchema.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiSchema.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using System.Collections.Generic; @@ -566,6 +566,13 @@ internal void WriteAsItemsProperties(IOpenApiWriter writer) writer.WriteProperty(OpenApiConstants.Type, Type); // format + if (string.IsNullOrEmpty(Format)) + { + Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? + AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? + OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format; + } + writer.WriteProperty(OpenApiConstants.Format, Format); // items @@ -630,9 +637,12 @@ internal void WriteAsSchemaProperties( } // format - Format ??= AllOf?.FirstOrDefault(static x => x.Format != null)?.Format ?? - AnyOf?.FirstOrDefault(static x => x.Format != null)?.Format ?? - OneOf?.FirstOrDefault(static x => x.Format != null)?.Format; + if (string.IsNullOrEmpty(Format)) + { + Format = AllOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? + AnyOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? + OneOf?.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format; + } writer.WriteProperty(OpenApiConstants.Format, Format); diff --git a/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs b/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs index 6db019be7..cfcc56d15 100644 --- a/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs +++ b/test/Microsoft.OpenApi.Tests/Models/OpenApiParameterTests.cs @@ -50,7 +50,12 @@ public class OpenApiParameterTests Schema = new OpenApiSchema { Title = "title2", - Description = "description2" + Description = "description2", + OneOf = new List + { + new OpenApiSchema { Type = "number", Format = "double" }, + new OpenApiSchema { Type = "string" } + } }, Examples = new Dictionary { @@ -234,6 +239,15 @@ public void SerializeAdvancedParameterAsV3JsonWorks() ""explode"": true, ""schema"": { ""title"": ""title2"", + ""oneOf"": [ + { + ""type"": ""number"", + ""format"": ""double"" + }, + { + ""type"": ""string"" + } + ], ""description"": ""description2"" }, ""examples"": { @@ -253,6 +267,27 @@ public void SerializeAdvancedParameterAsV3JsonWorks() actual.Should().Be(expected); } + [Fact] + public void SerializeAdvancedParameterAsV2JsonWorks() + { + // Arrange + var expected = @"{ + ""in"": ""path"", + ""name"": ""name1"", + ""description"": ""description1"", + ""required"": true, + ""format"": ""double"" +}"; + + // Act + var actual = AdvancedPathParameterWithSchema.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0); + + // Assert + actual = actual.MakeLineBreaksEnvironmentNeutral(); + expected = expected.MakeLineBreaksEnvironmentNeutral(); + actual.Should().Be(expected); + } + [Theory] [InlineData(true)] [InlineData(false)]