Skip to content

Retrieve format property during V2 serialization #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -630,6 +630,10 @@ 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;

writer.WriteProperty(OpenApiConstants.Format, Format);

// title
Expand Down Expand Up @@ -695,7 +699,7 @@ internal void WriteAsSchemaProperties(
// allOf
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));

// If there isn't already an AllOf, and the schema contains a oneOf or anyOf write an allOf with the first
// If there isn't already an allOf, and the schema contains a oneOf or anyOf write an allOf with the first
// schema in the list as an attempt to guess at a graceful downgrade situation.
if (AllOf == null || AllOf.Count == 0)
{
Expand Down
41 changes: 41 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,46 @@ public async Task SerializeSchemaWRequiredPropertiesAsV2JsonWorksAsync(bool prod
// Assert
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
}

[Fact]
public void SerializeAsV2ShouldSetFormatPropertyInParentSchemaIfPresentInChildrenSchema()
{
// Arrange
var schema = new OpenApiSchema()
{
OneOf = new List<OpenApiSchema>
{
new OpenApiSchema
{
Type = "number",
Format = "decimal"
},
new OpenApiSchema { Type = "string" },
}
};

var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
var openApiJsonWriter = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });

// Act
// Serialize as V2
schema.SerializeAsV2(openApiJsonWriter);
openApiJsonWriter.Flush();

var v2Schema = outputStringWriter.GetStringBuilder().ToString().MakeLineBreaksEnvironmentNeutral();

var expectedV2Schema = @"{
""format"": ""decimal"",
""allOf"": [
{
""format"": ""decimal"",
""type"": ""number""
}
]
}".MakeLineBreaksEnvironmentNeutral();

// Assert
Assert.Equal(expectedV2Schema, v2Schema);
}
}
}