|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Collections.Generic;
|
| 5 | +using System.Linq; |
5 | 6 | using Microsoft.OpenApi.Any;
|
6 | 7 | using Microsoft.OpenApi.Interfaces;
|
7 | 8 | using Microsoft.OpenApi.Writers;
|
@@ -260,7 +261,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
|
260 | 261 | {
|
261 | 262 | Reference.SerializeAsV3(writer);
|
262 | 263 | return;
|
263 |
| - } |
| 264 | + } |
264 | 265 | else
|
265 | 266 | {
|
266 | 267 | if (Reference.IsExternal) // Temporary until v2
|
@@ -644,6 +645,20 @@ internal void WriteAsSchemaProperties(
|
644 | 645 | // allOf
|
645 | 646 | writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));
|
646 | 647 |
|
| 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 | + |
647 | 662 | // properties
|
648 | 663 | writer.WriteOptionalMap(OpenApiConstants.Properties, Properties, (w, key, s) =>
|
649 | 664 | s.SerializeAsV2(w, Required, key));
|
|
0 commit comments