Skip to content

Commit f45de4c

Browse files
committed
Add test to validate parsing of 3.1 JSON schema keywords and update public API
1 parent e1feafe commit f45de4c

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs

Lines changed: 47 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;
@@ -404,5 +404,51 @@ public void LoadSchemaWithNullableExtensionAsV31Works(string filePath)
404404
// Assert
405405
schema.Type.Should().BeEquivalentTo(new string[] { "string", "null" });
406406
}
407+
408+
[Fact]
409+
public void SerializeSchemaWithJsonSchemaKeywordsWorks()
410+
{
411+
// Arrange
412+
var expected = @"$id: https://example.com/schemas/person.schema.yaml
413+
$schema: https://json-schema.org/draft/2020-12/schema
414+
$comment: A schema defining a person object with optional references to dynamic components.
415+
$vocabulary:
416+
https://json-schema.org/draft/2020-12/vocab/core: true
417+
https://json-schema.org/draft/2020-12/vocab/applicator: true
418+
https://json-schema.org/draft/2020-12/vocab/validation: true
419+
https://json-schema.org/draft/2020-12/vocab/meta-data: false
420+
https://json-schema.org/draft/2020-12/vocab/format-annotation: false
421+
$dynamicAnchor: addressDef
422+
title: Person
423+
required:
424+
- name
425+
type: object
426+
properties:
427+
name:
428+
$comment: The person's full name
429+
type: string
430+
age:
431+
$comment: Age must be a non-negative integer
432+
minimum: 0
433+
type: integer
434+
address:
435+
$comment: Reference to an address definition which can change dynamically
436+
$dynamicRef: '#addressDef'
437+
description: Schema for a person object
438+
";
439+
var path = Path.Combine(SampleFolderPath, "schemaWithJsonSchemaKeywords.yaml");
440+
441+
// Act
442+
var schema = OpenApiModelFactory.Load<OpenApiSchema>(path, OpenApiSpecVersion.OpenApi3_1, out _);
443+
444+
// serialization
445+
var writer = new StringWriter();
446+
schema.SerializeAsV31(new OpenApiYamlWriter(writer));
447+
var schemaString = writer.ToString();
448+
449+
// Assert
450+
schema.Vocabulary.Keys.Count.Should().Be(5);
451+
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
452+
}
407453
}
408454
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
$schema: "https://json-schema.org/draft/2020-12/schema"
2+
$id: "https://example.com/schemas/person.schema.yaml"
3+
$comment: "A schema defining a person object with optional references to dynamic components."
4+
$vocabulary:
5+
"https://json-schema.org/draft/2020-12/vocab/core": true
6+
"https://json-schema.org/draft/2020-12/vocab/applicator": true
7+
"https://json-schema.org/draft/2020-12/vocab/validation": true
8+
"https://json-schema.org/draft/2020-12/vocab/meta-data": false
9+
"https://json-schema.org/draft/2020-12/vocab/format-annotation": false
10+
11+
title: "Person"
12+
description: "Schema for a person object"
13+
type: "object"
14+
15+
properties:
16+
name:
17+
type: "string"
18+
$comment: "The person's full name"
19+
age:
20+
type: "integer"
21+
minimum: 0
22+
$comment: "Age must be a non-negative integer"
23+
address:
24+
$dynamicRef: "#addressDef"
25+
$comment: "Reference to an address definition which can change dynamically"
26+
27+
required:
28+
- name
29+
30+
$dynamicAnchor: "addressDef"
31+
definitions:
32+
address:
33+
$dynamicAnchor: "addressDef"
34+
type: "object"
35+
properties:
36+
street:
37+
type: "string"
38+
city:
39+
type: "string"
40+
postalCode:
41+
type: "string"

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ namespace Microsoft.OpenApi.Models
912912
public virtual bool UnresolvedReference { get; set; }
913913
public virtual decimal? V31ExclusiveMaximum { get; set; }
914914
public virtual decimal? V31ExclusiveMinimum { get; set; }
915-
public virtual string Vocabulary { get; set; }
915+
public virtual System.Collections.Generic.IDictionary<string, bool> Vocabulary { get; set; }
916916
public virtual bool WriteOnly { get; set; }
917917
public virtual Microsoft.OpenApi.Models.OpenApiXml Xml { get; set; }
918918
public virtual void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
@@ -1250,7 +1250,7 @@ namespace Microsoft.OpenApi.Models.References
12501250
public override bool? UniqueItems { get; set; }
12511251
public override decimal? V31ExclusiveMaximum { get; set; }
12521252
public override decimal? V31ExclusiveMinimum { get; set; }
1253-
public override string Vocabulary { get; set; }
1253+
public override System.Collections.Generic.IDictionary<string, bool> Vocabulary { get; set; }
12541254
public override bool WriteOnly { get; set; }
12551255
public override Microsoft.OpenApi.Models.OpenApiXml Xml { get; set; }
12561256
public override void SerializeAsV2(Microsoft.OpenApi.Writers.IOpenApiWriter writer) { }
@@ -1845,6 +1845,7 @@ namespace Microsoft.OpenApi.Writers
18451845
{
18461846
public static void WriteOptionalCollection(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IEnumerable<string> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, string> action) { }
18471847
public static void WriteOptionalCollection<T>(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IEnumerable<T> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, T> action) { }
1848+
public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, bool> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, bool> action) { }
18481849
public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, string> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, string> action) { }
18491850
public static void WriteOptionalMap<T>(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, T> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, T> action)
18501851
where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { }

0 commit comments

Comments
 (0)