Skip to content

Commit 76763dd

Browse files
committed
Add tests to verify external reference resolution both by $id and $ref locator works
1 parent 06d499a commit 76763dd

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System.Collections.Generic;
22
using System.Globalization;
33
using System.IO;
4+
using System.Threading.Tasks;
45
using FluentAssertions;
56
using Microsoft.OpenApi.Extensions;
6-
using Microsoft.OpenApi.Interfaces;
77
using Microsoft.OpenApi.Models;
88
using Microsoft.OpenApi.Models.References;
99
using Microsoft.OpenApi.Reader;
1010
using Microsoft.OpenApi.Tests;
1111
using Microsoft.OpenApi.Writers;
12+
using Microsoft.OpenApi.Services;
1213
using Xunit;
14+
using System.Linq;
1315

1416
namespace Microsoft.OpenApi.Readers.Tests.V31Tests
1517
{
@@ -392,7 +394,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds()
392394
new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 });
393395

394396
var outputWriter = new StringWriter(CultureInfo.InvariantCulture);
395-
var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true } );
397+
var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true });
396398
actual.OpenApiDocument.SerializeAsV31(writer);
397399
var serialized = outputWriter.ToString();
398400
}
@@ -445,7 +447,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
445447
}
446448
}
447449
};
448-
450+
449451
// Serialization
450452
var mediaType = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"];
451453

@@ -461,7 +463,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks()
461463
type: string
462464
prop3:
463465
type: string";
464-
466+
465467
var actualMediaType = mediaType.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1);
466468

467469
// Assert
@@ -484,5 +486,48 @@ public void ParseDocumentWithReferenceByIdGetsResolved()
484486
Assert.Equal("object", requestBodySchema.Type);
485487
Assert.Equal("string", parameterSchema.Type);
486488
}
489+
490+
[Fact]
491+
public async Task ExternalDocumentDereferenceToOpenApiDocumentUsingJsonPointerWorks()
492+
{
493+
// Arrange
494+
var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath);
495+
496+
var settings = new OpenApiReaderSettings
497+
{
498+
LoadExternalRefs = true,
499+
BaseUrl = new(path),
500+
};
501+
502+
// Act
503+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings);
504+
var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema;
505+
506+
// Assert
507+
result.OpenApiDocument.Workspace.Contains("./externalResource.yaml");
508+
responseSchema.Properties.Count.Should().Be(2); // reference has been resolved
509+
}
510+
511+
[Fact]
512+
public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks()
513+
{
514+
// Arrange
515+
var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath);
516+
517+
var settings = new OpenApiReaderSettings
518+
{
519+
LoadExternalRefs = true,
520+
BaseUrl = new(path),
521+
};
522+
523+
// Act
524+
var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings);
525+
var externalDoc = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml"), settings);
526+
527+
var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema;
528+
529+
// Assert
530+
requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved
531+
}
487532
}
488533
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
openapi: 3.1.0
2+
info:
3+
title: ReferenceById
4+
version: 1.0.0
5+
paths:
6+
/resource:
7+
get:
8+
parameters:
9+
- name: id
10+
in: query
11+
required: true
12+
schema:
13+
$ref: 'https://example.com/schemas/user.json'
14+
responses:
15+
'200':
16+
description: OK
17+
content:
18+
application/json:
19+
schema:
20+
$ref: './externalResource.yaml#/components/schemas/todo'
21+
components: {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.1.0
2+
info:
3+
title: ReferencedById
4+
version: 1.0.0
5+
paths: {}
6+
components:
7+
schemas:
8+
todo:
9+
type: object
10+
properties:
11+
id:
12+
type: string
13+
name:
14+
type: string
15+
user:
16+
$id: 'https://example.com/schemas/user.json'
17+
type: object
18+
properties:
19+
id:
20+
type: string
21+
name:
22+
type: string

0 commit comments

Comments
 (0)