Skip to content

Commit fb74312

Browse files
authored
Merge pull request #2133 from dldl-cmd/fix_referenceWithFolderLoading
Fix: reference with folder loading
2 parents e23a86a + 012440b commit fb74312

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

src/Microsoft.OpenApi/Reader/V3/OpenApiV3Deserializer.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ private static (string, string) GetReferenceIdAndExternalResource(string pointer
193193
var refId = refSegments.Last();
194194
var isExternalResource = !refSegments.First().StartsWith("#", StringComparison.OrdinalIgnoreCase);
195195

196-
string externalResource = isExternalResource ? $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}" : null;
196+
string externalResource = null;
197+
if (isExternalResource)
198+
{
199+
externalResource = pointer.Split('#')[0].TrimEnd('#');
200+
}
197201

198202
return (refId, externalResource);
199203
}

src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ private static (string, string) GetReferenceIdAndExternalResource(string pointer
165165
string externalResource = null;
166166
if (isExternalResource && pointer.Contains('#'))
167167
{
168-
externalResource = $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}";
168+
externalResource = pointer.Split('#')[0].TrimEnd('#');
169169
}
170170

171171
return (refId, externalResource);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiSchemaTests.cs

+60
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,65 @@ public async Task ParseAdvancedSchemaWithReferenceShouldSucceed()
388388
// Assert
389389
Assert.Equal(expected, actual);
390390
}
391+
392+
[Fact]
393+
public async Task ParseExternalReferenceSchemaShouldSucceed()
394+
{
395+
// Act
396+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalReferencesSchema.yaml"));
397+
398+
// Assert
399+
var components = result.Document.Components;
400+
401+
Assert.Equivalent(
402+
new OpenApiDiagnostic()
403+
{
404+
SpecificationVersion = OpenApiSpecVersion.OpenApi3_0
405+
}, result.Diagnostic);
406+
407+
var expectedComponents = new OpenApiComponents
408+
{
409+
Schemas =
410+
{
411+
["RelativePathModel"] = new OpenApiSchema()
412+
{
413+
AllOf =
414+
{
415+
new OpenApiSchemaReference("ExternalRelativePathModel", result.Document, "./FirstLevel/SecondLevel/ThridLevel/File.json")
416+
}
417+
},
418+
["SimpleRelativePathModel"] = new OpenApiSchema()
419+
{
420+
AllOf =
421+
{
422+
new OpenApiSchemaReference("ExternalSimpleRelativePathModel", result.Document, "File.json")
423+
}
424+
},
425+
["AbsoluteWindowsPathModel"] = new OpenApiSchema()
426+
{
427+
AllOf =
428+
{
429+
new OpenApiSchemaReference("ExternalAbsWindowsPathModel", result.Document, @"A:\Dir\File.json")
430+
}
431+
},
432+
["AbsoluteUnixPathModel"] = new OpenApiSchema()
433+
{
434+
AllOf =
435+
{
436+
new OpenApiSchemaReference("ExternalAbsUnixPathModel", result.Document, "/Dir/File.json")
437+
}
438+
},
439+
["HttpsUrlModel"] = new OpenApiSchema()
440+
{
441+
AllOf =
442+
{
443+
new OpenApiSchemaReference("ExternalHttpsModel", result.Document, "https://host.lan:1234/path/to/file/resource.json")
444+
}
445+
}
446+
}
447+
};
448+
449+
Assert.Equivalent(expectedComponents, components);
450+
}
391451
}
392452
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject
2+
openapi: 3.0.0
3+
info:
4+
title: Simple Document
5+
version: 0.9.1
6+
paths: { }
7+
components:
8+
schemas:
9+
RelativePathModel:
10+
allOf:
11+
- $ref: './FirstLevel/SecondLevel/ThridLevel/File.json#/components/schemas/ExternalRelativePathModel'
12+
SimpleRelativePathModel:
13+
allOf:
14+
- $ref: 'File.json#/components/schemas/ExternalSimpleRelativePathModel'
15+
AbsoluteWindowsPathModel:
16+
allOf:
17+
- $ref: 'A:\Dir\File.json#/components/schemas/ExternalAbsWindowsPathModel'
18+
AbsoluteUnixPathModel:
19+
allOf:
20+
- $ref: '/Dir/File.json#/components/schemas/ExternalAbsUnixPathModel'
21+
HttpsUrlModel:
22+
allOf:
23+
- $ref: 'https://host.lan:1234/path/to/file/resource.json#/components/schemas/ExternalHttpsModel'

0 commit comments

Comments
 (0)