Skip to content

Commit

Permalink
Add support for pattern properties
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Aug 13, 2024
1 parent 8b0fb29 commit 6d32cc1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ public class OpenApiSchema : IOpenApiExtensible, IOpenApiReferenceable, IOpenApi
/// </summary>
public IDictionary<string, OpenApiSchema> Properties { get; set; } = new Dictionary<string, OpenApiSchema>();

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// PatternProperty definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced)
/// Each property name of this object SHOULD be a valid regular expression according to the ECMA 262 r
/// egular expression dialect. Each property value of this object MUST be an object, and each object MUST
/// be a valid Schema Object not a standard JSON Schema.
/// </summary>
public IDictionary<string, OpenApiSchema> PatternProperties { get; set; } = new Dictionary<string, OpenApiSchema>();

/// <summary>
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
/// </summary>
Expand Down Expand Up @@ -363,11 +372,12 @@ public OpenApiSchema(OpenApiSchema schema)
MinItems = schema?.MinItems ?? MinItems;
UniqueItems = schema?.UniqueItems ?? UniqueItems;
Properties = schema?.Properties != null ? new Dictionary<string, OpenApiSchema>(schema.Properties) : null;
PatternProperties = schema?.PatternProperties != null ? new Dictionary<string, OpenApiSchema>(schema.PatternProperties) : null;
MaxProperties = schema?.MaxProperties ?? MaxProperties;
MinProperties = schema?.MinProperties ?? MinProperties;
AdditionalPropertiesAllowed = schema?.AdditionalPropertiesAllowed ?? AdditionalPropertiesAllowed;
AdditionalProperties = schema?.AdditionalProperties != null ? new(schema?.AdditionalProperties) : null;
Discriminator = schema?.Discriminator != null ? new(schema?.Discriminator) : 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;
Expand Down Expand Up @@ -596,6 +606,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
writer.WriteProperty(OpenApiConstants.V31ExclusiveMinimum, V31ExclusiveMinimum);
writer.WriteProperty(OpenApiConstants.UnevaluatedProperties, UnevaluatedProperties, false);
writer.WriteOptionalCollection(OpenApiConstants.Examples, Examples, (nodeWriter, s) => nodeWriter.WriteAny(new OpenApiAny(s)));
writer.WriteOptionalMap(OpenApiConstants.PatternProperties, PatternProperties, (w, s) => s.SerializeAsV31(w));
}

/// <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 @@ -150,6 +150,10 @@ internal static partial class OpenApiV31Deserializer
"properties",
(o, n, t) => o.Properties = n.CreateMap(LoadOpenApiSchema, t)
},
{
"patternProperties",
(o, n, t) => o.PatternProperties = n.CreateMap(LoadOpenApiSchema, t)
},
{
"additionalProperties", (o, n, _) =>
{
Expand Down

0 comments on commit 6d32cc1

Please sign in to comment.