|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.OpenApi.Models; |
| 5 | +using Microsoft.OpenApi.Models.References; |
| 6 | +using Microsoft.OpenApi.Reader; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Microsoft.OpenApi.Readers.Tests.V31Tests |
| 10 | +{ |
| 11 | + public class OpenApiCompoentsTests |
| 12 | + { |
| 13 | + [Theory] |
| 14 | + [InlineData("./FirstLevel/SecondLevel/ThridLevel/File.json#/components/schemas/ExternalRelativePathModel", "ExternalRelativePathModel", "./FirstLevel/SecondLevel/ThridLevel/File.json")] |
| 15 | + [InlineData("File.json#/components/schemas/ExternalSimpleRelativePathModel", "ExternalSimpleRelativePathModel", "File.json")] |
| 16 | + [InlineData("A:\\Dir\\File.json#/components/schemas/ExternalAbsWindowsPathModel", "ExternalAbsWindowsPathModel", "A:\\Dir\\File.json")] |
| 17 | + [InlineData("/Dir/File.json#/components/schemas/ExternalAbsUnixPathModel", "ExternalAbsUnixPathModel", "/Dir/File.json")] |
| 18 | + [InlineData("https://host.lan:1234/path/to/file/resource.json#/components/schemas/ExternalHttpsModel", "ExternalHttpsModel", "https://host.lan:1234/path/to/file/resource.json")] |
| 19 | + [InlineData("File.json", "File.json", null)] |
| 20 | + public void ParseExternalSchemaReferenceShouldSucceed(string reference, string referenceId, string externalResource) |
| 21 | + { |
| 22 | + var input = $@"{{ |
| 23 | + ""schemas"": {{ |
| 24 | + ""Model"": {{ |
| 25 | + ""$ref"": ""{reference.Replace("\\", "\\\\")}"" |
| 26 | + }} |
| 27 | + }} |
| 28 | +}} |
| 29 | +"; |
| 30 | + var openApiDocument = new OpenApiDocument(); |
| 31 | + |
| 32 | + // Act |
| 33 | + var components = OpenApiModelFactory.Parse<OpenApiComponents>(input, OpenApiSpecVersion.OpenApi3_1, openApiDocument, out _, "json"); |
| 34 | + |
| 35 | + // Assert |
| 36 | + var schema = components.Schemas["Model"] as OpenApiSchemaReference; |
| 37 | + var expected = new OpenApiSchemaReference(referenceId, openApiDocument, externalResource); |
| 38 | + Assert.Equivalent(expected, schema); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
0 commit comments