Skip to content

Commit

Permalink
Add support for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Aug 13, 2024
1 parent f0233f8 commit 8b0fb29
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
/// </summary>
public OpenApiAny Example { get; set; }

/// <summary>
/// A free-form property to include examples of an instance for this schema.
/// To represent examples that cannot be naturally represented in JSON or YAML,
/// a list of values can be used to contain the examples with escaping where necessary.
/// </summary>
public IList<JsonNode> Examples { get; set; }

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
Expand Down Expand Up @@ -362,6 +369,7 @@ public OpenApiSchema(OpenApiSchema schema)
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : null;
Example = schema?.Example != null ? new(schema?.Example.Node) : null;
Examples = schema?.Examples != null ? new List<JsonNode>(schema.Examples) : null;
Enum = schema?.Enum != null ? new List<JsonNode>(schema.Enum) : null;
Nullable = schema?.Nullable ?? Nullable;
ExternalDocs = schema?.ExternalDocs != null ? new(schema?.ExternalDocs) : null;
Expand Down Expand Up @@ -587,6 +595,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Extensions;
Expand Down Expand Up @@ -203,6 +203,10 @@ internal static partial class OpenApiV31Deserializer
"example",
(o, n, _) => o.Example = n.CreateAny()
},
{
"examples",
(o, n, _) => o.Examples = n.CreateListOfAny()
},
{
"deprecated",
(o, n, _) => o.Deprecated = bool.Parse(n.GetScalarValue())
Expand Down

0 comments on commit 8b0fb29

Please sign in to comment.