Skip to content

Commit b004ba6

Browse files
committed
Preserve examples in v2 files and write them out as extensions
1 parent 17241ec commit b004ba6

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 15 additions & 1 deletion
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;
@@ -394,6 +394,20 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
394394
}
395395
}
396396

397+
//examples
398+
if (Examples != null && Examples.Any())
399+
{
400+
writer.WritePropertyName("x-examples");
401+
writer.WriteStartObject();
402+
403+
foreach (var example in Examples)
404+
{
405+
writer.WritePropertyName(example.Key);
406+
writer.WriteV2Examples(writer, example.Value, OpenApiSpecVersion.OpenApi2_0);
407+
}
408+
writer.WriteEndObject();
409+
}
410+
397411
// extensions
398412
writer.WriteExtensions(extensionsClone, OpenApiSpecVersion.OpenApi2_0);
399413

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 5 additions & 3 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;
@@ -152,7 +152,8 @@ internal OpenApiBodyParameter ConvertToBodyParameter()
152152
// V2 spec actually allows the body to have custom name.
153153
// To allow round-tripping we use an extension to hold the name
154154
Name = "body",
155-
Schema = Content.Values.FirstOrDefault()?.Schema ?? new OpenApiSchema(),
155+
Schema = Content.Values.FirstOrDefault()?.Schema ?? new JsonSchemaBuilder().Build(),
156+
Examples = Content.Values.FirstOrDefault()?.Examples,
156157
Required = Required,
157158
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.
158159
};
@@ -184,7 +185,8 @@ internal IEnumerable<OpenApiFormDataParameter> ConvertToFormDataParameters()
184185
Description = property.Value.Description,
185186
Name = property.Key,
186187
Schema = property.Value,
187-
Required = Content.First().Value.Schema.Required.Contains(property.Key)
188+
Examples = Content.Values.FirstOrDefault()?.Examples,
189+
Required = Content.First().Value.Schema.GetRequired()?.Contains(property.Key) ?? false
188190
};
189191
}
190192
}

src/Microsoft.OpenApi/Models/OpenApiResponse.cs

Lines changed: 22 additions & 1 deletion
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;
@@ -201,6 +201,27 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
201201
writer.WriteEndObject();
202202
}
203203

204+
if (Content.Values.Any(m => m.Examples != null && m.Examples.Any()))
205+
{
206+
writer.WritePropertyName("x-examples");
207+
writer.WriteStartObject();
208+
209+
foreach (var mediaTypePair in Content)
210+
{
211+
var examples = mediaTypePair.Value.Examples;
212+
if (examples != null && examples.Any())
213+
{
214+
foreach (var example in examples)
215+
{
216+
writer.WritePropertyName(example.Key);
217+
writer.WriteV2Examples(writer, example.Value, OpenApiSpecVersion.OpenApi2_0);
218+
}
219+
}
220+
}
221+
222+
writer.WriteEndObject();
223+
}
224+
204225
writer.WriteExtensions(mediatype.Value.Extensions, OpenApiSpecVersion.OpenApi2_0);
205226

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

src/Microsoft.OpenApi/Writers/IOpenApiWriter.cs

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

4+
using Microsoft.OpenApi.Models;
5+
46
namespace Microsoft.OpenApi.Writers
57
{
68
/// <summary>
@@ -72,5 +74,13 @@ public interface IOpenApiWriter
7274
/// Flush the writer.
7375
/// </summary>
7476
void Flush();
77+
78+
/// <summary>
79+
/// Writes out existing examples in a mediatype object
80+
/// </summary>
81+
/// <param name="writer"></param>
82+
/// <param name="example"></param>
83+
/// <param name="version"></param>
84+
void WriteV2Examples(IOpenApiWriter writer, OpenApiExample example, OpenApiSpecVersion version);
7585
}
7686
}

src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

Lines changed: 24 additions & 1 deletion
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;
@@ -405,5 +405,28 @@ protected void VerifyCanWritePropertyName(string name)
405405
string.Format(SRResource.ObjectScopeNeededForPropertyNameWriting, name));
406406
}
407407
}
408+
409+
/// <inheritdoc/>
410+
public void WriteV2Examples(IOpenApiWriter writer, OpenApiExample example, OpenApiSpecVersion version)
411+
{
412+
writer.WriteStartObject();
413+
414+
// summary
415+
writer.WriteProperty(OpenApiConstants.Summary, example.Summary);
416+
417+
// description
418+
writer.WriteProperty(OpenApiConstants.Description, example.Description);
419+
420+
// value
421+
writer.WriteOptionalObject(OpenApiConstants.Value, example.Value, (w, v) => w.WriteAny(v));
422+
423+
// externalValue
424+
writer.WriteProperty(OpenApiConstants.ExternalValue, example.ExternalValue);
425+
426+
// extensions
427+
writer.WriteExtensions(example.Extensions, version);
428+
429+
writer.WriteEndObject();
430+
}
408431
}
409432
}

0 commit comments

Comments
 (0)