Skip to content

Commit aa3895f

Browse files
committed
Refactor vocabulary object to represent a dictionary; implement serialization and deserialization
1 parent 3178fc3 commit aa3895f

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 5 additions & 5 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;
@@ -43,7 +43,7 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe
4343
/// <summary>
4444
/// $vocabulary- used in meta-schemas to identify the vocabularies available for use in schemas described by that meta-schema.
4545
/// </summary>
46-
public virtual string Vocabulary { get; set; }
46+
public virtual IDictionary<string, bool> Vocabulary { get; set; }
4747

4848
/// <summary>
4949
/// $dynamicRef - an applicator that allows for deferring the full resolution until runtime, at which point it is resolved each time it is encountered while evaluating an instance
@@ -348,7 +348,7 @@ public OpenApiSchema(OpenApiSchema schema)
348348
Id = schema?.Id ?? Id;
349349
Schema = schema?.Schema ?? Schema;
350350
Comment = schema?.Comment ?? Comment;
351-
Vocabulary = schema?.Vocabulary ?? Vocabulary;
351+
Vocabulary = schema?.Vocabulary != null ? new Dictionary<string, bool>(schema.Vocabulary) : null;
352352
DynamicAnchor = schema?.DynamicAnchor ?? DynamicAnchor;
353353
DynamicRef = schema?.DynamicRef ?? DynamicRef;
354354
Definitions = schema?.Definitions != null ? new Dictionary<string, OpenApiSchema>(schema.Definitions) : null;
@@ -562,8 +562,8 @@ internal void WriteV31Properties(IOpenApiWriter writer)
562562
writer.WriteProperty(OpenApiConstants.Id, Id);
563563
writer.WriteProperty(OpenApiConstants.DollarSchema, Schema);
564564
writer.WriteProperty(OpenApiConstants.Comment, Comment);
565-
writer.WriteProperty(OpenApiConstants.Vocabulary, Vocabulary);
566-
writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, (w, s) => s.SerializeAsV3(w));
565+
writer.WriteOptionalMap(OpenApiConstants.Vocabulary, Vocabulary, (w, s) => w.WriteValue(s));
566+
writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, (w, s) => s.SerializeAsV31(w));
567567
writer.WriteProperty(OpenApiConstants.DynamicRef, DynamicRef);
568568
writer.WriteProperty(OpenApiConstants.DynamicAnchor, DynamicAnchor);
569569
writer.WriteProperty(OpenApiConstants.V31ExclusiveMaximum, V31ExclusiveMaximum);

src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal OpenApiSchemaReference(OpenApiSchema target, string referenceId)
7474
/// <inheritdoc/>
7575
public override string Comment { get => Target.Comment; set => Target.Comment = value; }
7676
/// <inheritdoc/>
77-
public override string Vocabulary { get => Target.Vocabulary; set => Target.Vocabulary = value; }
77+
public override IDictionary<string, bool> Vocabulary { get => Target.Vocabulary; set => Target.Vocabulary = value; }
7878
/// <inheritdoc/>
7979
public override string DynamicRef { get => Target.DynamicRef; set => Target.DynamicRef = value; }
8080
/// <inheritdoc/>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static partial class OpenApiV31Deserializer
3232
},
3333
{
3434
"$vocabulary",
35-
(o, n, _) => o.Vocabulary = n.GetScalarValue()
35+
(o, n, _) => o.Vocabulary = n.CreateSimpleMap(LoadBool)
3636
},
3737
{
3838
"$dynamicRef",

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ private static string LoadString(ParseNode node)
145145
return node.GetScalarValue();
146146
}
147147

148+
private static bool LoadBool(ParseNode node)
149+
{
150+
return bool.Parse(node.GetScalarValue());
151+
}
152+
148153
private static (string, string) GetReferenceIdAndExternalResource(string pointer)
149154
{
150155
/* Check whether the reference pointer is a URL

src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ public static void WriteOptionalMap(
272272
}
273273
}
274274

275+
/// <summary>
276+
/// Write the optional Open API element map (string to string mapping).
277+
/// </summary>
278+
/// <param name="writer">The Open API writer.</param>
279+
/// <param name="name">The property name.</param>
280+
/// <param name="elements">The map values.</param>
281+
/// <param name="action">The map element writer action.</param>
282+
public static void WriteOptionalMap(
283+
this IOpenApiWriter writer,
284+
string name,
285+
IDictionary<string, bool> elements,
286+
Action<IOpenApiWriter, bool> action)
287+
{
288+
if (elements != null && elements.Any())
289+
{
290+
writer.WriteMapInternal(name, elements, action);
291+
}
292+
}
293+
275294
/// <summary>
276295
/// Write the optional Open API element map.
277296
/// </summary>

0 commit comments

Comments
 (0)