Skip to content

Commit 6e12152

Browse files
committed
chore: adds a unit test for json schema ref annotations parsing
Signed-off-by: Vincent Biret <[email protected]>
1 parent 8ed4512 commit 6e12152

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json.Nodes;
3+
using Microsoft.OpenApi.Reader;
4+
using Microsoft.OpenApi.Reader.V31;
5+
using Xunit;
6+
7+
namespace Microsoft.OpenApi.Readers.Tests.V31Tests;
8+
9+
public class OpenApiSchemaReferenceDeserializerTests
10+
{
11+
[Fact]
12+
public void ShouldDeserializeSchemaReferenceAnnotations()
13+
{
14+
var json =
15+
"""
16+
{
17+
"$ref": "#/components/schemas/MySchema",
18+
"description": "This is a schema reference",
19+
"default": "foo",
20+
"readOnly": true,
21+
"writeOnly": true,
22+
"deprecated": true,
23+
"title": "This is a schema reference",
24+
"examples": ["example reference value"]
25+
}
26+
""";
27+
28+
var hostDocument = new OpenApiDocument();
29+
hostDocument.AddComponent("MySchema", new OpenApiSchema
30+
{
31+
Title = "This is a schema",
32+
Description = "This is a schema description",
33+
Default = "bar",
34+
Type = JsonSchemaType.String,
35+
ReadOnly = false,
36+
WriteOnly = false,
37+
Deprecated = false,
38+
Examples = new List<JsonNode> { "example value" },
39+
});
40+
var jsonNode = JsonNode.Parse(json);
41+
var parseNode = ParseNode.Create(new ParsingContext(new()), jsonNode);
42+
43+
var result = OpenApiV31Deserializer.LoadSchema(parseNode, hostDocument);
44+
45+
Assert.NotNull(result);
46+
var resultReference = Assert.IsType<OpenApiSchemaReference>(result);
47+
48+
Assert.Equal("MySchema", resultReference.Reference.Id);
49+
Assert.Equal("This is a schema reference", resultReference.Description);
50+
Assert.Equal("foo", resultReference.Default?.ToString());
51+
Assert.True(resultReference.ReadOnly);
52+
Assert.True(resultReference.WriteOnly);
53+
Assert.True(resultReference.Deprecated);
54+
Assert.Equal("This is a schema reference", resultReference.Title);
55+
Assert.NotNull(resultReference.Examples);
56+
Assert.Single(resultReference.Examples);
57+
Assert.Equal("example reference value", resultReference.Examples[0]?.ToString());
58+
Assert.NotNull(resultReference.Target);
59+
}
60+
}

0 commit comments

Comments
 (0)