Skip to content

Commit 86c585a

Browse files
Merge pull request #994 from microsoft/mk/remove-root-format-property-V3
Retrieve format property during V2 serialization
2 parents 9c296e1 + 0818fb7 commit 86c585a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
@@ -630,6 +630,10 @@ internal void WriteAsSchemaProperties(
630630
}
631631

632632
// format
633+
Format ??= AllOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
634+
AnyOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
635+
OneOf?.FirstOrDefault(static x => x.Format != null)?.Format;
636+
633637
writer.WriteProperty(OpenApiConstants.Format, Format);
634638

635639
// title
@@ -695,7 +699,7 @@ internal void WriteAsSchemaProperties(
695699
// allOf
696700
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));
697701

698-
// If there isn't already an AllOf, and the schema contains a oneOf or anyOf write an allOf with the first
702+
// If there isn't already an allOf, and the schema contains a oneOf or anyOf write an allOf with the first
699703
// schema in the list as an attempt to guess at a graceful downgrade situation.
700704
if (AllOf == null || AllOf.Count == 0)
701705
{

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,5 +422,46 @@ public async Task SerializeSchemaWRequiredPropertiesAsV2JsonWorksAsync(bool prod
422422
// Assert
423423
await Verifier.Verify(actual).UseParameters(produceTerseOutput);
424424
}
425+
426+
[Fact]
427+
public void SerializeAsV2ShouldSetFormatPropertyInParentSchemaIfPresentInChildrenSchema()
428+
{
429+
// Arrange
430+
var schema = new OpenApiSchema()
431+
{
432+
OneOf = new List<OpenApiSchema>
433+
{
434+
new OpenApiSchema
435+
{
436+
Type = "number",
437+
Format = "decimal"
438+
},
439+
new OpenApiSchema { Type = "string" },
440+
}
441+
};
442+
443+
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
444+
var openApiJsonWriter = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });
445+
446+
// Act
447+
// Serialize as V2
448+
schema.SerializeAsV2(openApiJsonWriter);
449+
openApiJsonWriter.Flush();
450+
451+
var v2Schema = outputStringWriter.GetStringBuilder().ToString().MakeLineBreaksEnvironmentNeutral();
452+
453+
var expectedV2Schema = @"{
454+
""format"": ""decimal"",
455+
""allOf"": [
456+
{
457+
""format"": ""decimal"",
458+
""type"": ""number""
459+
}
460+
]
461+
}".MakeLineBreaksEnvironmentNeutral();
462+
463+
// Assert
464+
Assert.Equal(expectedV2Schema, v2Schema);
465+
}
425466
}
426467
}

0 commit comments

Comments
 (0)