Skip to content

Commit 0afb104

Browse files
authored
Merge pull request #817 from /issues/762
Convert anyOf/oneOf to allOf with first schema when writing v2
2 parents 5189855 + 0ecaed8 commit 0afb104

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Writers;
@@ -260,7 +261,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
260261
{
261262
Reference.SerializeAsV3(writer);
262263
return;
263-
}
264+
}
264265
else
265266
{
266267
if (Reference.IsExternal) // Temporary until v2
@@ -644,6 +645,20 @@ internal void WriteAsSchemaProperties(
644645
// allOf
645646
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));
646647

648+
// If there isn't already an AllOf, and the schema contains a oneOf or anyOf write an allOf with the first
649+
// schema in the list as an attempt to guess at a graceful downgrade situation.
650+
if (AllOf == null || AllOf.Count == 0)
651+
{
652+
// anyOf (Not Supported in V2) - Write the first schema only as an allOf.
653+
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AnyOf.Take(1), (w, s) => s.SerializeAsV2(w));
654+
655+
if (AnyOf == null || AnyOf.Count == 0)
656+
{
657+
// oneOf (Not Supported in V2) - Write the first schema only as an allOf.
658+
writer.WriteOptionalCollection(OpenApiConstants.AllOf, OneOf.Take(1), (w, s) => s.SerializeAsV2(w));
659+
}
660+
}
661+
647662
// properties
648663
writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, key, s) =>
649664
s.SerializeAsV2(w, Required, key));

0 commit comments

Comments
 (0)