Skip to content

Commit 8b0fb29

Browse files
committed
Add support for examples
1 parent f0233f8 commit 8b0fb29

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
262262
/// </summary>
263263
public OpenApiAny Example { get; set; }
264264

265+
/// <summary>
266+
/// A free-form property to include examples of an instance for this schema.
267+
/// To represent examples that cannot be naturally represented in JSON or YAML,
268+
/// a list of values can be used to contain the examples with escaping where necessary.
269+
/// </summary>
270+
public IList<JsonNode> Examples { get; set; }
271+
265272
/// <summary>
266273
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
267274
/// </summary>
@@ -362,6 +369,7 @@ public OpenApiSchema(OpenApiSchema schema)
362369
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
363370
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null;
364371
Example = schema?.Example != null ? new(schema?.Example.Node) : null;
372+
Examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;
365373
Enum = schema?.Enum != null ? new List<JsonNode>(schema.Enum) : null;
366374
Nullable = schema?.Nullable ?? Nullable;
367375
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
@@ -587,6 +595,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
587595
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);
588596
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
589597
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
598+
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
590599
}
591600

592601
/// <summary>

src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

Lines changed: 5 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 Microsoft.OpenApi.Extensions;
@@ -203,6 +203,10 @@ internal static partial class OpenApiV31Deserializer
203203
"example",
204204
(o, n, _) => o.Example = n.CreateAny()
205205
},
206+
{
207+
"examples",
208+
(o, n, _) => o.Examples = n.CreateListOfAny()
209+
},
206210
{
207211
"deprecated",
208212
(o, n, _) => o.Deprecated = bool.Parse(n.GetScalarValue())

0 commit comments

Comments
 (0)