From 76763ddabd550c62994244935407ef28dc0afd73 Mon Sep 17 00:00:00 2001 From: Maggiekimani1 Date: Fri, 6 Sep 2024 13:08:05 +0300 Subject: [PATCH] Add tests to verify external reference resolution both by $id and $ref locator works --- .../V31Tests/OpenApiDocumentTests.cs | 53 +++++++++++++++++-- .../OpenApiDocument/docWithExternalRef.yaml | 21 ++++++++ .../OpenApiDocument/externalResource.yaml | 22 ++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExternalRef.yaml create mode 100644 test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/externalResource.yaml diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs index 2ada8e4bd..bce1ffb68 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiDocumentTests.cs @@ -1,15 +1,17 @@ using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Threading.Tasks; using FluentAssertions; using Microsoft.OpenApi.Extensions; -using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models.References; using Microsoft.OpenApi.Reader; using Microsoft.OpenApi.Tests; using Microsoft.OpenApi.Writers; +using Microsoft.OpenApi.Services; using Xunit; +using System.Linq; namespace Microsoft.OpenApi.Readers.Tests.V31Tests { @@ -392,7 +394,7 @@ public void ParseDocumentsWithReusablePathItemInWebhooksSucceeds() new OpenApiDiagnostic() { SpecificationVersion = OpenApiSpecVersion.OpenApi3_1 }); var outputWriter = new StringWriter(CultureInfo.InvariantCulture); - var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true } ); + var writer = new OpenApiJsonWriter(outputWriter, new() { InlineLocalReferences = true }); actual.OpenApiDocument.SerializeAsV31(writer); var serialized = outputWriter.ToString(); } @@ -445,7 +447,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks() } } }; - + // Serialization var mediaType = result.OpenApiDocument.Paths["/example"].Operations[OperationType.Get].Responses["200"].Content["application/json"]; @@ -461,7 +463,7 @@ public void ParseDocumentWithPatternPropertiesInSchemaWorks() type: string prop3: type: string"; - + var actualMediaType = mediaType.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1); // Assert @@ -484,5 +486,48 @@ public void ParseDocumentWithReferenceByIdGetsResolved() Assert.Equal("object", requestBodySchema.Type); Assert.Equal("string", parameterSchema.Type); } + + [Fact] + public async Task ExternalDocumentDereferenceToOpenApiDocumentUsingJsonPointerWorks() + { + // Arrange + var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath); + + var settings = new OpenApiReaderSettings + { + LoadExternalRefs = true, + BaseUrl = new(path), + }; + + // Act + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings); + var responseSchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Responses["200"].Content["application/json"].Schema; + + // Assert + result.OpenApiDocument.Workspace.Contains("./externalResource.yaml"); + responseSchema.Properties.Count.Should().Be(2); // reference has been resolved + } + + [Fact] + public async Task ParseExternalDocumentDereferenceToOpenApiDocumentByIdWorks() + { + // Arrange + var path = Path.Combine(Directory.GetCurrentDirectory(), SampleFolderPath); + + var settings = new OpenApiReaderSettings + { + LoadExternalRefs = true, + BaseUrl = new(path), + }; + + // Act + var result = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "docWithExternalRef.yaml"), settings); + var externalDoc = await OpenApiDocument.LoadAsync(Path.Combine(SampleFolderPath, "externalResource.yaml"), settings); + + var requestBodySchema = result.OpenApiDocument.Paths["/resource"].Operations[OperationType.Get].Parameters.First().Schema; + + // Assert + requestBodySchema.Properties.Count.Should().Be(2); // reference has been resolved + } } } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExternalRef.yaml b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExternalRef.yaml new file mode 100644 index 000000000..7a4b7cd8c --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/docWithExternalRef.yaml @@ -0,0 +1,21 @@ +openapi: 3.1.0 +info: + title: ReferenceById + version: 1.0.0 +paths: + /resource: + get: + parameters: + - name: id + in: query + required: true + schema: + $ref: 'https://example.com/schemas/user.json' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: './externalResource.yaml#/components/schemas/todo' +components: {} \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/externalResource.yaml b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/externalResource.yaml new file mode 100644 index 000000000..78d6c0851 --- /dev/null +++ b/test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiDocument/externalResource.yaml @@ -0,0 +1,22 @@ +openapi: 3.1.0 +info: + title: ReferencedById + version: 1.0.0 +paths: {} +components: + schemas: + todo: + type: object + properties: + id: + type: string + name: + type: string + user: + $id: 'https://example.com/schemas/user.json' + type: object + properties: + id: + type: string + name: + type: string \ No newline at end of file