|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Text.Json.Nodes; |
| 4 | +using Microsoft.OpenApi.Reader; |
| 5 | +using Microsoft.OpenApi.Reader.V31; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.OpenApi.Readers.Tests.V31Tests; |
| 9 | + |
| 10 | +public class OpenApiTagReferenceDeserializerTests |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void ShouldDeserializeTagReferenceAnnotations() |
| 14 | + { |
| 15 | + var json = |
| 16 | + """ |
| 17 | + { |
| 18 | + "tags" : [ |
| 19 | + "MyTag" |
| 20 | + ] |
| 21 | + } |
| 22 | + """; |
| 23 | + |
| 24 | + var hostDocument = new OpenApiDocument(); |
| 25 | + hostDocument.Tags ??= new HashSet<OpenApiTag>(); |
| 26 | + hostDocument.Tags.Add(new OpenApiTag |
| 27 | + { |
| 28 | + Name = "MyTag", |
| 29 | + Description = "This is a tag description", |
| 30 | + }); |
| 31 | + var jsonNode = JsonNode.Parse(json); |
| 32 | + var parseNode = ParseNode.Create(new ParsingContext(new()), jsonNode); |
| 33 | + |
| 34 | + var result = OpenApiV31Deserializer.LoadOperation(parseNode, hostDocument); |
| 35 | + // this diverges from the other unit tests because Tag References are implemented |
| 36 | + // through the reference infrastructure for convenience, but the behave quite differently |
| 37 | + |
| 38 | + Assert.NotNull(result); |
| 39 | + Assert.NotNull(result.Tags); |
| 40 | + Assert.Single(result.Tags); |
| 41 | + var resultReference = Assert.IsType<OpenApiTagReference>(result.Tags.First()); |
| 42 | + |
| 43 | + Assert.Equal("MyTag", resultReference.Reference.Id); |
| 44 | + Assert.Equal("This is a tag description", resultReference.Description); |
| 45 | + Assert.NotNull(resultReference.Target); |
| 46 | + } |
| 47 | +} |
0 commit comments