Skip to content

Commit

Permalink
Merge pull request #1851 from microsoft/mk/fix-$ref-with-$id-serializ…
Browse files Browse the repository at this point in the history
…ation

Fix $ref with $id serialization
  • Loading branch information
MaggieKimani1 authored Oct 2, 2024
2 parents e0a3bb2 + 66fb599 commit ed5c771
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public string ReferenceV3
{
return Id;
}
if (Id.StartsWith("http"))
{
return Id;
}

return "#/components/" + Type.GetDisplayName() + "/" + Id;
}
Expand Down Expand Up @@ -236,6 +240,11 @@ private string GetExternalReferenceV3()
return ExternalResource + "#" + Id;
}

if (Id.StartsWith("http"))
{
return Id;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Writers;
using System;
using System.Collections.Generic;
using System.Runtime;
using System.Text.Json.Nodes;

namespace Microsoft.OpenApi.Models.References
Expand Down
8 changes: 5 additions & 3 deletions src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ private static (string, string) GetReferenceIdAndExternalResource(string pointer
string refId = !pointer.Contains('#') ? pointer : refSegments.Last();

var isExternalResource = !refSegments.First().StartsWith("#");
string externalResource = isExternalResource
? $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}"
: null;
string externalResource = null;
if (isExternalResource && pointer.Contains('#'))
{
externalResource = $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}";
}

return (refId, externalResource);
}
Expand Down
8 changes: 4 additions & 4 deletions test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
<DependentUpon>OpenApiCallbackReferenceTests.cs</DependentUpon>
</None>

<None Update="Models\Samples\docWithDollarId.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<None Update="Models\Samples\docWithReusableWebhooks.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<None Update="PublicApi\PublicApi.approved.txt" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models\Samples\" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,5 +1642,54 @@ public void SerializeV31DocumentWithRefsInWebhooksWorks()
var actual = stringWriter.ToString();
actual.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral());
}

[Fact]
public void SerializeDocWithDollarIdInDollarRefSucceeds()
{
var expected = @"openapi: '3.1.0'
info:
title: Simple API
version: 1.0.0
paths:
/box:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: https://foo.bar/Box
/circle:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: https://foo.bar/Circle
components:
schemas:
Box:
$id: https://foo.bar/Box
type: object
properties:
width:
type: number
height:
type: number
Circle:
$id: https://foo.bar/Circle
type: object
properties:
radius:
type: number
";
var doc = OpenApiDocument.Load("Models/Samples/docWithDollarId.yaml").OpenApiDocument;

var actual = doc.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_1);
actual.MakeLineBreaksEnvironmentNeutral().Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral());
}
}
}
39 changes: 39 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/Samples/docWithDollarId.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
openapi: 3.1.0
info:
title: Simple API
version: 1.0.0
paths:
/box:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: https://foo.bar/Box
/circle:
get:
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: https://foo.bar/Circle
components:
schemas:
Box:
$id: https://foo.bar/Box
type: object
properties:
width:
type: number
height:
type: number
Circle:
$id: https://foo.bar/Circle
type: object
properties:
radius:
type: number

0 comments on commit ed5c771

Please sign in to comment.