Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix $ref with $id serialization #1851

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("https"))
MaggieKimani1 marked this conversation as resolved.
Show resolved Hide resolved
{
return Id;
}

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

if (Id.StartsWith("https"))
MaggieKimani1 marked this conversation as resolved.
Show resolved Hide resolved
{
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());
}
}
}
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
Loading