|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO;
|
3 | 4 | using System.Net.Http;
|
4 | 5 | using System.Text.Json.Nodes;
|
@@ -297,6 +298,99 @@ public async Task ShouldResolveRelativeSubReference()
|
297 | 298 | Assert.Equal(JsonSchemaType.String, seq2Property.Items.Items.Type);
|
298 | 299 | }
|
299 | 300 | [Fact]
|
| 301 | + public async Task ShouldResolveRelativeSubReferenceUsingParsingContext() |
| 302 | + { |
| 303 | + // Arrange |
| 304 | + var filePath = Path.Combine(SampleFolderPath, "relativeSubschemaReference.json"); |
| 305 | + using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); |
| 306 | + var jsonNode = await JsonNode.ParseAsync(fs); |
| 307 | + var schemaJsonNode = jsonNode["components"]?["schemas"]?["Foo"]; |
| 308 | + Assert.NotNull(schemaJsonNode); |
| 309 | + var diagnostic = new OpenApiDiagnostic(); |
| 310 | + var parsingContext = new ParsingContext(diagnostic); |
| 311 | + parsingContext.StartObject("components"); |
| 312 | + parsingContext.StartObject("schemas"); |
| 313 | + parsingContext.StartObject("Foo"); |
| 314 | + var document = new OpenApiDocument(); |
| 315 | + |
| 316 | + // Act |
| 317 | + var fooComponentSchema = parsingContext.ParseFragment<OpenApiSchema>(schemaJsonNode, OpenApiSpecVersion.OpenApi3_1, document); |
| 318 | + document.AddComponent("Foo", fooComponentSchema); |
| 319 | + var seq1Property = fooComponentSchema.Properties["seq1"]; |
| 320 | + Assert.NotNull(seq1Property); |
| 321 | + var seq2Property = fooComponentSchema.Properties["seq2"]; |
| 322 | + Assert.NotNull(seq2Property); |
| 323 | + Assert.Equal(JsonSchemaType.Array, seq2Property.Items.Type); |
| 324 | + Assert.Equal(JsonSchemaType.String, seq2Property.Items.Items.Type); |
| 325 | + } |
| 326 | + [Fact] |
| 327 | + public void ShouldFailToResolveRelativeSubReferenceFromTheObjectModel() |
| 328 | + { |
| 329 | + var document = new OpenApiDocument |
| 330 | + { |
| 331 | + Info = new OpenApiInfo { Title = "Test API", Version = "1.0.0" }, |
| 332 | + }; |
| 333 | + document.Components = new OpenApiComponents |
| 334 | + { |
| 335 | + Schemas = new Dictionary<string, IOpenApiSchema> |
| 336 | + { |
| 337 | + ["Foo"] = new OpenApiSchema |
| 338 | + { |
| 339 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 340 | + { |
| 341 | + ["seq1"] = new OpenApiSchema { Type = JsonSchemaType.Array | JsonSchemaType.Null, Items = new OpenApiSchema { Type = JsonSchemaType.Array, Items = new OpenApiSchema { Type = JsonSchemaType.String } } }, |
| 342 | + ["seq2"] = new OpenApiSchema { Type = JsonSchemaType.Array | JsonSchemaType.Null, Items = new OpenApiSchemaReference("#/properties/seq1/items", document) } |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + }; |
| 347 | + document.RegisterComponents(); |
| 348 | + |
| 349 | + var fooComponentSchema = document.Components.Schemas["Foo"]; |
| 350 | + var seq1Property = fooComponentSchema.Properties["seq1"]; |
| 351 | + Assert.NotNull(seq1Property); |
| 352 | + var seq2Property = fooComponentSchema.Properties["seq2"]; |
| 353 | + Assert.NotNull(seq2Property); |
| 354 | + Assert.Throws<ArgumentException>(() => seq2Property.Items.Type); |
| 355 | + // it's impossible to resolve relative references from the object model only because we don't have a way to get to |
| 356 | + // the parent object to build the full path for the reference. |
| 357 | + |
| 358 | + |
| 359 | + // #/properties/seq1/items |
| 360 | + // #/components/schemas/Foo/properties/seq1/items |
| 361 | + } |
| 362 | + [Fact] |
| 363 | + public void ShouldResolveAbsoluteSubReferenceFromTheObjectModel() |
| 364 | + { |
| 365 | + var document = new OpenApiDocument |
| 366 | + { |
| 367 | + Info = new OpenApiInfo { Title = "Test API", Version = "1.0.0" }, |
| 368 | + }; |
| 369 | + document.Components = new OpenApiComponents |
| 370 | + { |
| 371 | + Schemas = new Dictionary<string, IOpenApiSchema> |
| 372 | + { |
| 373 | + ["Foo"] = new OpenApiSchema |
| 374 | + { |
| 375 | + Properties = new Dictionary<string, IOpenApiSchema> |
| 376 | + { |
| 377 | + ["seq1"] = new OpenApiSchema { Type = JsonSchemaType.Array | JsonSchemaType.Null, Items = new OpenApiSchema { Type = JsonSchemaType.Array, Items = new OpenApiSchema { Type = JsonSchemaType.String } } }, |
| 378 | + ["seq2"] = new OpenApiSchema { Type = JsonSchemaType.Array | JsonSchemaType.Null, Items = new OpenApiSchemaReference("#/components/schemas/Foo/properties/seq1/items", document) } |
| 379 | + } |
| 380 | + } |
| 381 | + } |
| 382 | + }; |
| 383 | + document.RegisterComponents(); |
| 384 | + |
| 385 | + var fooComponentSchema = document.Components.Schemas["Foo"]; |
| 386 | + var seq1Property = fooComponentSchema.Properties["seq1"]; |
| 387 | + Assert.NotNull(seq1Property); |
| 388 | + var seq2Property = fooComponentSchema.Properties["seq2"]; |
| 389 | + Assert.NotNull(seq2Property); |
| 390 | + Assert.Equal(JsonSchemaType.Array, seq2Property.Items.Type); |
| 391 | + Assert.Equal(JsonSchemaType.String, seq2Property.Items.Items.Type); |
| 392 | + } |
| 393 | + [Fact] |
300 | 394 | public async Task ShouldResolveRecursiveRelativeSubReference()
|
301 | 395 | {
|
302 | 396 | // Arrange
|
|
0 commit comments