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 handling for reference IDs with http prefix #2239

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

echo "Building Microsoft.OpenApi"

PROJ="$(dirname "$0")/src/Microsoft.OpenApi/Microsoft.OpenApi.csproj"
dotnet msbuild "$PROJ" /t:restore /p:Configuration=Release
dotnet msbuild "$PROJ" /t:build /p:Configuration=Release
dotnet msbuild "$PROJ" /t:pack "/p:Configuration=Release;PackageOutputPath=$(dirname "$0")/artifacts"

echo "Building Microsoft.OpenApi.Readers"

PROJ="$(dirname "$0")/src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj"
dotnet msbuild "$PROJ" /t:restore /p:Configuration=Release
dotnet msbuild "$PROJ" /t:build /p:Configuration=Release
dotnet msbuild "$PROJ" /t:pack "/p:Configuration=Release;PackageOutputPath=$(dirname "$0")/artifacts"

echo "Building Microsoft.OpenApi.Hidi"

PROJ="$(dirname "$0")/src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj"
dotnet msbuild "$PROJ" /t:restore /p:Configuration=Release
dotnet msbuild "$PROJ" /t:build /p:Configuration=Release
dotnet msbuild "$PROJ" /t:pack "/p:Configuration=Release;PackageOutputPath=$(dirname "$0")/artifacts"
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "8.0.406"
}
}
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public string ReferenceV3
{
return Id;
}
if (Id.StartsWith("http", StringComparison.OrdinalIgnoreCase))
if (Id.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
Id.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
Expand Down Expand Up @@ -241,7 +242,8 @@ private string GetExternalReferenceV3()
return ExternalResource + "#" + Id;
}

if (Id.StartsWith("http", StringComparison.OrdinalIgnoreCase))
if (Id.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
Id.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return Id;
}
Expand Down
26 changes: 26 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class OpenApiReferenceTests
[InlineData("#/components/schemas/Pet", ReferenceType.Schema, "Pet")]
[InlineData("#/components/parameters/name", ReferenceType.Parameter, "name")]
[InlineData("#/components/responses/200", ReferenceType.Response, "200")]
[InlineData("#/components/schemas/HttpValidationsProblem", ReferenceType.Schema, "HttpValidationsProblem")]
public void SettingInternalReferenceForComponentsStyleReferenceShouldSucceed(
string input,
ReferenceType type,
Expand Down Expand Up @@ -43,6 +44,7 @@ public void SettingInternalReferenceForComponentsStyleReferenceShouldSucceed(
[InlineData("Pet.json#/components/schemas/Pet", "Pet.json", "Pet", ReferenceType.Schema)]
[InlineData("Pet.yaml#/components/schemas/Pet", "Pet.yaml", "Pet", ReferenceType.Schema)]
[InlineData("abc#/components/schemas/Pet", "abc", "Pet", ReferenceType.Schema)]
[InlineData("abc#/components/schemas/HttpsValidationProblem", "abc", "HttpsValidationProblem", ReferenceType.Schema)]
public void SettingExternalReferenceV3ShouldSucceed(string expected, string externalResource, string id, ReferenceType? type)
{
// Arrange & Act
Expand Down Expand Up @@ -105,6 +107,30 @@ public async Task SerializeSchemaReferenceAsJsonV3Works()
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("HttpValidationProblemDetails", "#/components/schemas/HttpValidationProblemDetails")]
[InlineData("http://example.com", "http://example.com")]
[InlineData("https://example.com", "https://example.com")]
public async Task SerializeHttpSchemaReferenceAsJsonV31Works(string id, string referenceV3)
{
// Arrange
var reference = new OpenApiReference { Type = ReferenceType.Schema, Id = id };
var expected =
$$"""
{
"$ref": "{{referenceV3}}"
}
""";

// Act
var actual = await reference.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_1);
expected = expected.MakeLineBreaksEnvironmentNeutral();
actual = actual.MakeLineBreaksEnvironmentNeutral();

// Assert
Assert.Equal(expected, actual);
}

[Fact]
public async Task SerializeSchemaReferenceAsYamlV3Works()
{
Expand Down