Skip to content

Commit

Permalink
Merge pull request #1082 from microsoft/mk/reader-changes-external-re…
Browse files Browse the repository at this point in the history
…ference

Fixes reader changing external reference bug
  • Loading branch information
MaggieKimani1 authored Nov 18, 2022
2 parents ed00216 + 4428e67 commit 482175d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/Microsoft.OpenApi.Readers/V3/OpenApiV3VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public OpenApiReference ConvertToOpenApiReference(
}
// Where fragments point into a non-OpenAPI document, the id will be the complete fragment identifier
string id = segments[1];
var openApiReference = new OpenApiReference();

// $ref: externalSource.yaml#/Pet
if (id.StartsWith("/components/"))
{
Expand All @@ -127,13 +129,16 @@ public OpenApiReference ConvertToOpenApiReference(
}
id = localSegments[3];
}

return new OpenApiReference
else
{
ExternalResource = segments[0],
Type = type,
Id = id
};
openApiReference.IsFragrament = true;
}

openApiReference.ExternalResource = segments[0];
openApiReference.Type = type;
openApiReference.Id = id;

return openApiReference;
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public class OpenApiReference : IOpenApiSerializable
/// </summary>
public bool IsLocal => ExternalResource == null;

/// <summary>
/// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment
/// </summary>
public bool IsFragrament = false;

/// <summary>
/// The OpenApiDocument that is hosting the OpenApiReference instance. This is used to enable dereferencing the reference.
/// </summary>
Expand Down Expand Up @@ -196,7 +201,12 @@ private string GetExternalReferenceV3()
{
if (Id != null)
{
return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
if (IsFragrament)
{
return ExternalResource + "#" + Id;
}

return ExternalResource + "#/components/" + Type.GetDisplayName() + "/"+ Id;
}

return ExternalResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\petStoreWithTagAndSecurity.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\documentWithExternalRefs.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\securedApi.yaml" />
<EmbeddedResource Include="V3Tests\Samples\OpenApiEncoding\advancedEncoding.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using FluentAssertions;
using Microsoft.OpenApi.Any;
Expand Down Expand Up @@ -1327,5 +1328,24 @@ public void HeaderParameterShouldAllowExample()
});
}
}

[Fact]
public void DoesNotChangeExternalReferences()
{
// Arrange
using var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "documentWithExternalRefs.yaml"));

// Act
var doc = new OpenApiStreamReader(
new OpenApiReaderSettings { ReferenceResolution = ReferenceResolutionSetting.DoNotResolveReferences})
.Read(stream, out var diagnostic);

var externalRef = doc.Components.Schemas["Nested"].Properties["AnyOf"].AnyOf.First().Reference.ReferenceV3;
var externalRef2 = doc.Components.Schemas["Nested"].Properties["AnyOf"].AnyOf.Last().Reference.ReferenceV3;

// Assert
Assert.Equal("file:///C:/MySchemas.json#/definitions/ArrayObject", externalRef);
Assert.Equal("../foo/schemas.yaml#/components/schemas/Number", externalRef2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.1
info:
title: anyOf-oneOf
license:
name: MIT
version: 1.0.0
paths: { }
components:
schemas:
Nested:
type: object
properties:
AnyOf:
anyOf:
- $ref: file:///C:/MySchemas.json#/definitions/ArrayObject
- $ref: ../foo/schemas.yaml#/components/schemas/Number
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ namespace Microsoft.OpenApi.Models
}
public class OpenApiReference : Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiSerializable
{
public bool IsFragrament;
public OpenApiReference() { }
public OpenApiReference(Microsoft.OpenApi.Models.OpenApiReference reference) { }
public string ExternalResource { get; set; }
Expand Down

0 comments on commit 482175d

Please sign in to comment.