Skip to content

Commit 6dac23b

Browse files
committed
draft: fix location key encoding
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
1 parent a861e47 commit 6dac23b

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace Microsoft.OpenApi.Tests.Services;
5+
6+
public class OpenApiVisitorBaseTests
7+
{
8+
[Fact]
9+
public void EncodesReservedCharacters()
10+
{
11+
// Given
12+
var openApiDocument = new OpenApiDocument
13+
{
14+
Info = new()
15+
{
16+
Title = "foo",
17+
Version = "1.2.2"
18+
},
19+
Paths = new()
20+
{
21+
},
22+
Components = new()
23+
{
24+
Schemas = new Dictionary<string, IOpenApiSchema>()
25+
{
26+
["Pet~"] = new OpenApiSchema()
27+
{
28+
Type = JsonSchemaType.Object
29+
},
30+
["Pet/"] = new OpenApiSchema()
31+
{
32+
Type = JsonSchemaType.Object
33+
},
34+
}
35+
}
36+
};
37+
var visitor = new LocatorVisitor();
38+
39+
// When
40+
visitor.Visit(openApiDocument);
41+
42+
// Then
43+
Assert.Equivalent(
44+
new List<string>
45+
{
46+
"#/components/schemas/Pet~0",
47+
"#/components/schemas/Pet~1"
48+
}, visitor.Locations);
49+
}
50+
51+
private class LocatorVisitor : OpenApiVisitorBase
52+
{
53+
public List<string> Locations { get; } = new List<string>();
54+
55+
public override void Visit(IOpenApiSchema openApiSchema)
56+
{
57+
Locations.Add(this.PathString);
58+
}
59+
public override void Visit(OpenApiComponents components)
60+
{
61+
Enter("schemas");
62+
if (components.Schemas != null)
63+
{
64+
foreach (var schemaKvp in components.Schemas)
65+
{
66+
Enter(schemaKvp.Key);
67+
this.Visit(schemaKvp.Value);
68+
Exit();
69+
}
70+
}
71+
Exit();
72+
}
73+
public override void Visit(OpenApiDocument doc)
74+
{
75+
Enter("components");
76+
Visit(doc.Components);
77+
Exit();
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)