|
| 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