Skip to content

Commit 348e8cf

Browse files
committed
Merge remote-tracking branch 'origin/is/patch-fix' into mk/remove-root-format-property-V3
2 parents 4ecd027 + 00fe8db commit 348e8cf

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 6 additions & 8 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(x => x.Format != null)?.Format ??
634+
AnyOf?.FirstOrDefault(x => x.Format != null)?.Format ??
635+
OneOf?.FirstOrDefault(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
{
@@ -707,12 +711,6 @@ internal void WriteAsSchemaProperties(
707711
// oneOf (Not Supported in V2) - Write the first schema only as an allOf.
708712
writer.WriteOptionalCollection(OpenApiConstants.AllOf, OneOf?.Take(1), (w, s) => s.SerializeAsV2(w));
709713
}
710-
if (OneOf?.Count > 0)
711-
{
712-
// Take the format and set it at the root
713-
var oneOfFormat = OneOf.Select<OpenApiSchema, string>(x => x.Format.ToString()).FirstOrDefault();
714-
this.Format = oneOfFormat;
715-
}
716714
}
717715

718716
// properties

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public void SerializeSchemaPrimitiveTypeShouldRemoveFormatInRootIfPresentInChild
448448
schema.SerializeAsV2(openApiJsonWriter);
449449
openApiJsonWriter.Flush();
450450

451-
var v2Schema = outputStringWriter.GetStringBuilder().ToString();//.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\n","");
451+
var v2Schema = outputStringWriter.GetStringBuilder().ToString().MakeLineBreaksEnvironmentNeutral();//.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\n","");
452452

453453
// Serialize as V3
454454
//schema.SerializeAsV3(openApiJsonWriter);
@@ -457,13 +457,14 @@ public void SerializeSchemaPrimitiveTypeShouldRemoveFormatInRootIfPresentInChild
457457
//var v3Schema = outputStringWriter.GetStringBuilder().ToString();//.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\n", "");
458458

459459
var expectedV2Schema = @"{
460-
""allOf"": [
461-
{
462-
""format"": ""decimal"",
463-
""type"": ""number""
464-
}],
465-
""format"": ""decimal""
466-
}".Replace(Environment.NewLine, "").Replace(" ", "").Replace("\n","");
460+
""format"": ""decimal"",
461+
""allOf"": [
462+
{
463+
""format"": ""decimal"",
464+
""type"": ""number""
465+
}
466+
]
467+
}".MakeLineBreaksEnvironmentNeutral();
467468

468469

469470
var expectedV3Schema = @"{

0 commit comments

Comments
 (0)