Skip to content

Commit 4015f13

Browse files
committed
Preserve examples in v2 files and write them out as extensions
1 parent 99a038c commit 4015f13

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Text.Json;
6+
using System.Linq;
77
using Json.Schema;
88
using Microsoft.OpenApi.Any;
99
using Microsoft.OpenApi.Extensions;
@@ -433,6 +433,20 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
433433
}
434434
}
435435

436+
//examples
437+
if (Examples != null && Examples.Any())
438+
{
439+
writer.WritePropertyName("x-examples");
440+
writer.WriteStartObject();
441+
442+
foreach (var example in Examples)
443+
{
444+
writer.WritePropertyName(example.Key);
445+
writer.WriteV2Examples(writer, example.Value, OpenApiSpecVersion.OpenApi2_0);
446+
}
447+
writer.WriteEndObject();
448+
}
449+
436450
// extensions
437451
writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0);
438452

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ internal OpenApiBodyParameter ConvertToBodyParameter()
186186
// To allow round-tripping we use an extension to hold the name
187187
Name = "body",
188188
Schema = Content.Values.FirstOrDefault()?.Schema ?? new JsonSchemaBuilder().Build(),
189+
Examples = Content.Values.FirstOrDefault()?.Examples,
189190
Required = Required,
190191
Extensions = Extensions.ToDictionary(static k => k.Key, static v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
191192
};
@@ -219,6 +220,7 @@ internal IEnumerable<OpenApiFormDataParameter> ConvertToFormDataParameters()
219220
Description = property.Value.GetDescription(),
220221
Name = property.Key,
221222
Schema = property.Value,
223+
Examples = Content.Values.FirstOrDefault()?.Examples,
222224
Required = Content.First().Value.Schema.GetRequired()?.Contains(property.Key) ?? false
223225
};
224226
}

src/Microsoft.OpenApi/Models/OpenApiResponse.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using System.Linq;
77
using System.Text.Json;
88
using System.Text.Json.Nodes;
9+
using System.Xml.Linq;
910
using Json.More;
11+
using Microsoft.OpenApi.Any;
1012
using Microsoft.OpenApi.Helpers;
1113
using Microsoft.OpenApi.Interfaces;
1214
using Microsoft.OpenApi.Writers;
@@ -235,6 +237,27 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
235237
writer.WriteEndObject();
236238
}
237239

240+
if (Content.Values.Any(m => m.Examples != null && m.Examples.Any()))
241+
{
242+
writer.WritePropertyName("x-examples");
243+
writer.WriteStartObject();
244+
245+
foreach (var mediaTypePair in Content)
246+
{
247+
var examples = mediaTypePair.Value.Examples;
248+
if (examples != null && examples.Any())
249+
{
250+
foreach (var example in examples)
251+
{
252+
writer.WritePropertyName(example.Key);
253+
writer.WriteV2Examples(writer, example.Value, OpenApiSpecVersion.OpenApi2_0);
254+
}
255+
}
256+
}
257+
258+
writer.WriteEndObject();
259+
}
260+
238261
writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0);
239262

240263
foreach (var key in mediatype.Value.Extensions.Keys)

src/Microsoft.OpenApi/Writers/IOpenApiWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using Json.Schema;
7+
using Microsoft.OpenApi.Models;
78

89
namespace Microsoft.OpenApi.Writers
910
{
@@ -96,5 +97,13 @@ public interface IOpenApiWriter
9697
/// <param name="writer"></param>
9798
/// <param name="reference"></param>
9899
void WriteJsonSchemaReference(IOpenApiWriter writer, Uri reference);
100+
101+
/// <summary>
102+
/// Writes out existing examples in a mediatype object
103+
/// </summary>
104+
/// <param name="writer"></param>
105+
/// <param name="example"></param>
106+
/// <param name="version"></param>
107+
void WriteV2Examples(IOpenApiWriter writer, OpenApiExample example, OpenApiSpecVersion version);
99108
}
100109
}

src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,29 @@ public void WriteJsonSchemaReference(IOpenApiWriter writer, Uri reference)
594594
this.WriteProperty(OpenApiConstants.DollarRef, reference.OriginalString);
595595
WriteEndObject();
596596
}
597+
598+
/// <inheritdoc/>
599+
public void WriteV2Examples(IOpenApiWriter writer, OpenApiExample example, OpenApiSpecVersion version)
600+
{
601+
writer.WriteStartObject();
602+
603+
// summary
604+
writer.WriteProperty(OpenApiConstants.Summary, example.Summary);
605+
606+
// description
607+
writer.WriteProperty(OpenApiConstants.Description, example.Description);
608+
609+
// value
610+
writer.WriteOptionalObject(OpenApiConstants.Value, example.Value, (w, v) => w.WriteAny(v));
611+
612+
// externalValue
613+
writer.WriteProperty(OpenApiConstants.ExternalValue, example.ExternalValue);
614+
615+
// extensions
616+
writer.WriteExtensions(example.Extensions, version);
617+
618+
writer.WriteEndObject();
619+
}
597620
}
598621

599622
internal class FindJsonSchemaRefs : OpenApiVisitorBase

0 commit comments

Comments
 (0)