-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathRelativeReferenceTests.cs
More file actions
322 lines (268 loc) · 12.4 KB
/
RelativeReferenceTests.cs
File metadata and controls
322 lines (268 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.OpenApi.Reader;
using Xunit;
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
{
public class RelativeReferenceTests
{
private const string SampleFolderPath = "V31Tests/ReferenceSamples";
[Fact]
public async Task ParseInlineLocalReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "inlineLocalReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schemaType = actual.Paths["/item"].Operations[HttpMethod.Get].Parameters[0].Schema.Type;
// Assert
Assert.Equal(JsonSchemaType.Number, schemaType);
}
[Fact]
public async Task ParseInlineExternalReferenceWorks()
{
// Arrange
var expected = new JsonArray
{
new JsonObject
{
["name"] = "thing",
["description"] = "a thing"
}
};
var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath);
var settings = new OpenApiReaderSettings
{
LoadExternalRefs = true,
BaseUrl = new(path),
};
settings.AddYamlReader();
// Act
var actual = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "inlineExternalReference.yaml"), settings)).Document;
var exampleValue = actual.Paths["/items"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Examples["item-list"].Value;
// Assert
Assert.NotNull(exampleValue);
Assert.IsType<JsonArray>(exampleValue);
Assert.Equal(expected.ToJsonString(), exampleValue.ToJsonString());
}
[Fact]
public async Task ParseComponentExternalReferenceWorks()
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath);
var settings = new OpenApiReaderSettings
{
LoadExternalRefs = true,
BaseUrl = new(path),
};
settings.AddYamlReader();
// Act
var actual = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "componentExternalReference.yaml"), settings)).Document;
var securitySchemeValue = actual.Components.SecuritySchemes["customapikey"];
// Assert
Assert.Equal("x-api-key", securitySchemeValue.Name);
}
[Fact]
public async Task ParseRootInlineJsonSchemaReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "rootInlineSchemaReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Paths["/item"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
}
[Fact]
public async Task ParseSubschemaInlineJsonSchemaReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "subschemaInlineSchemaReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Paths["/items"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema.Items;
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
}
[Fact]
public async Task ParseRootComponentJsonSchemaReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "rootComponentSchemaReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Components.Schemas["specialitem"];
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
Assert.Equal("Item", schema.Title);
}
[Fact]
public async Task ParseSubschemaComponentJsonSchemaReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "subschemaComponentSchemaReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Components.Schemas["items"].Items;
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
}
[Fact]
public async Task ParseInternalComponentSubschemaJsonSchemaReferenceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "internalComponentsSubschemaReference.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var addressSchema = actual.Paths["/person/{id}/address"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
var itemsSchema = actual.Paths["/human"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
// Assert
Assert.Equal(JsonSchemaType.Object, addressSchema.Type);
Assert.Equal(JsonSchemaType.Integer, itemsSchema.Type);
}
[Fact]
public async Task ParseExternalComponentSubschemaJsonSchemaReferenceWorks()
{
// Arrange
var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath);
var settings = new OpenApiReaderSettings
{
LoadExternalRefs = true,
BaseUrl = new(path),
};
settings.AddYamlReader();
// Act
var actual = (await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalComponentSubschemaReference.yaml"), settings)).Document;
var schema = actual.Paths["/person/{id}"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
}
[Fact]
public async Task ParseReferenceToInternalComponentUsingDollarIdWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "internalComponentReferenceUsingId.yaml");
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Paths["/person/{id}"].Operations[HttpMethod.Get].Responses["200"].Content["application/json"].Schema;
// Assert
Assert.Equal(JsonSchemaType.Object, schema.Type);
}
[Fact]
public async Task ParseLocalReferenceToJsonSchemaResourceWorks()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "localReferenceToJsonSchemaResource.yaml");
var stringWriter = new StringWriter();
var writer = new OpenApiYamlWriter(stringWriter);
// Act
var actual = (await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings)).Document;
var schema = actual.Components.Schemas["a"].Properties["b"].Properties["c"].Properties["b"];
schema.SerializeAsV31(writer);
// Assert
Assert.Equal(JsonSchemaType.Object | JsonSchemaType.Null, schema.Type);
}
[Fact]
public void ResolveSubSchema_ShouldTraverseKnownKeywords()
{
var schema = new OpenApiSchema
{
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>
{
["a"] = new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["b"] = new OpenApiSchema { Type = JsonSchemaType.String }
}
}
}
};
var path = new[] { "properties", "a", "properties", "b" };
var result = OpenApiWorkspace.ResolveSubSchema(schema, path);
Assert.NotNull(result);
Assert.Equal(JsonSchemaType.String, result!.Type);
}
public static IEnumerable<object[]> SubSchemaKeywordPropertyPaths =>
[
[new[] { "properties", "properties" }],
[new[] { "properties", "allOf" }]
];
[Theory]
[MemberData(nameof(SubSchemaKeywordPropertyPaths))]
public void ResolveSubSchema_ShouldHandleUserDefinedKeywordNamedProperty(string[] pathSegments)
{
var schema = new OpenApiSchema
{
Type = JsonSchemaType.Object,
Properties = new Dictionary<string, IOpenApiSchema>
{
["properties"] = new OpenApiSchema { Type = JsonSchemaType.String },
["allOf"] = new OpenApiSchema { Type = JsonSchemaType.String }
}
};
var result = OpenApiWorkspace.ResolveSubSchema(schema, pathSegments);
Assert.NotNull(result);
Assert.Equal(JsonSchemaType.String, result!.Type);
}
[Fact]
public void ResolveSubSchema_ShouldRecurseIntoAllOfComposition()
{
var schema = new OpenApiSchema
{
AllOf =
[
new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["x"] = new OpenApiSchema { Type = JsonSchemaType.Integer }
}
}
]
};
var path = new[] { "allOf", "0", "properties", "x" };
var result = OpenApiWorkspace.ResolveSubSchema(schema, path);
Assert.NotNull(result);
Assert.Equal(JsonSchemaType.Integer, result!.Type);
}
[Fact]
public async Task ShouldResolveRelativeSubReference()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "relativeSubschemaReference.json");
// Act
var (actual, _) = await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings);
var fooComponentSchema = actual.Components.Schemas["Foo"];
var seq1Property = fooComponentSchema.Properties["seq1"];
Assert.NotNull(seq1Property);
var seq2Property = fooComponentSchema.Properties["seq2"];
Assert.NotNull(seq2Property);
Assert.Equal(JsonSchemaType.Array, seq2Property.Items.Type);
Assert.Equal(JsonSchemaType.String, seq2Property.Items.Items.Type);
}
[Fact]
public async Task ShouldResolveRecursiveRelativeSubReference()
{
// Arrange
var filePath = Path.Combine(SampleFolderPath, "recursiveRelativeSubschemaReference.json");
// Act
var (actual, _) = await OpenApiDocument.LoadAsync(filePath, SettingsFixture.ReaderSettings);
var fooComponentSchema = actual.Components.Schemas["Foo"];
var fooSchemaParentProperty = fooComponentSchema.Properties["parent"];
Assert.NotNull(fooSchemaParentProperty);
var fooSchemaParentPropertyTagsProperty = fooSchemaParentProperty.Properties["tags"];
Assert.NotNull(fooSchemaParentPropertyTagsProperty);
Assert.Equal(JsonSchemaType.Array | JsonSchemaType.Null, fooSchemaParentPropertyTagsProperty.Type);
Assert.Equal(JsonSchemaType.Object, fooSchemaParentPropertyTagsProperty.Items.Type);
var fooSchemaTagsProperty = fooComponentSchema.Properties["tags"];
Assert.NotNull(fooSchemaTagsProperty);
Assert.Equal(JsonSchemaType.Array | JsonSchemaType.Null, fooSchemaTagsProperty.Type);
Assert.Equal(JsonSchemaType.Object, fooSchemaTagsProperty.Items.Type);
}
}
}