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

Register and retrieve JsonSchema references with $id value pointers #1633

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 12 additions & 12 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -128,7 +128,7 @@

writer.WriteStartObject();

// openApi;

Check warning on line 131 in src/Microsoft.OpenApi/Models/OpenApiDocument.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
writer.WriteProperty(OpenApiConstants.OpenApi, "3.1.0");

// jsonSchemaDialect
Expand Down Expand Up @@ -485,22 +485,22 @@
/// <param name="referenceUri"></param>
/// <returns>A JsonSchema ref.</returns>
public JsonSchema ResolveJsonSchemaReference(Uri referenceUri)
{
{
const char pound = '#';
string uriLocation;
string id = referenceUri.OriginalString.Split('/')?.Last();
string relativePath = "/components/" + ReferenceType.Schema.GetDisplayName() + "/" + id;

if (referenceUri.OriginalString.StartsWith("#"))
int poundIndex = referenceUri.OriginalString.IndexOf(pound);

if (poundIndex > 0)

Check warning on line 493 in src/Microsoft.OpenApi/Models/OpenApiDocument.cs

View workflow job for this annotation

GitHub Actions / Build

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
{
// Local reference
uriLocation = BaseUri + relativePath;
// External reference, ex: ./TodoReference.yaml#/components/schemas/todo
string externalUri = referenceUri.OriginalString.Split(pound).First();
Uri externalDocId = Workspace.GetDocumentId(externalUri);
string relativePath = referenceUri.OriginalString.Split(pound).Last();
uriLocation = externalDocId + relativePath;
}
else
{
// External reference
var externalUri = referenceUri.OriginalString.Split('#').First();
var externalDocId = Workspace.GetDocumentId(externalUri);
uriLocation = externalDocId + relativePath;
uriLocation = BaseUri + referenceUri.ToString().TrimStart(pound);
}

return (JsonSchema)Workspace.ResolveReference<IBaseDocument>(uriLocation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.OpenApi.Interfaces;
Expand Down Expand Up @@ -27,15 +27,16 @@ internal async Task<OpenApiDiagnostic> LoadAsync(OpenApiReference reference,
CancellationToken cancellationToken = default)
{
_workspace.AddDocumentId(reference.ExternalResource, document.BaseUri);
_workspace.RegisterComponents(document);
var version = diagnostic?.SpecificationVersion ?? OpenApiSpecVersion.OpenApi3_0;
_workspace.RegisterComponents(document, version);
document.Workspace = _workspace;

// Collect remote references by walking document
var referenceCollector = new OpenApiRemoteReferenceCollector();
var collectorWalker = new OpenApiWalker(referenceCollector);
collectorWalker.Walk(document);

diagnostic ??= new();
diagnostic ??= new() { SpecificationVersion = version };

// Walk references
foreach (var item in referenceCollector.References)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
FixRequestBodyReferences(openApiDoc);

// Register components
openApiDoc.Workspace.RegisterComponents(openApiDoc);
openApiDoc.Workspace.RegisterComponents(openApiDoc, OpenApiSpecVersion.OpenApi2_0);

return openApiDoc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
ParseMap(openApiNode, openApiDoc, _openApiFixedFields, _openApiPatternFields);

// Register components
openApiDoc.Workspace.RegisterComponents(openApiDoc);
openApiDoc.Workspace.RegisterComponents(openApiDoc, OpenApiSpecVersion.OpenApi3_0);

return openApiDoc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
ParseMap(openApiNode, openApiDoc, _openApiFixedFields, _openApiPatternFields);

// Register components
openApiDoc.Workspace.RegisterComponents(openApiDoc);
openApiDoc.Workspace.RegisterComponents(openApiDoc, OpenApiSpecVersion.OpenApi3_1);

return openApiDoc;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Json.Schema;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Services
{
internal static class OpenApiComponentsRegistryExtensions
{
public static void RegisterComponents(this OpenApiWorkspace workspace, OpenApiDocument document)
public static void RegisterComponents(this OpenApiWorkspace workspace, OpenApiDocument document, OpenApiSpecVersion version = OpenApiSpecVersion.OpenApi3_0)
{
if (document?.Components == null) return;

Expand All @@ -17,7 +18,24 @@
// Register Schema
foreach (var item in document.Components.Schemas)
{
var location = baseUri + ReferenceType.Schema.GetDisplayName() + "/" + item.Key;
string location;

if (item.Value.GetId() != null)
{
location = document.BaseUri + item.Value.GetId().ToString();
}
else
{
if (version == OpenApiSpecVersion.OpenApi2_0)
{
location = document.BaseUri + "/" + OpenApiConstants.Definitions + "/" + item.Key;
}
else
{
location = baseUri + ReferenceType.Schema.GetDisplayName() + "/" + item.Key;
}
}

workspace.RegisterComponent(location, item.Value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
Expand All @@ -7,10 +7,10 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**\*.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="**\*.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public OpenApiPathItemReferenceTests()
_openApiDoc = OpenApiDocument.Parse(OpenApi, OpenApiConstants.Yaml).OpenApiDocument;
_openApiDoc_2 = OpenApiDocument.Parse(OpenApi_2, OpenApiConstants.Yaml).OpenApiDocument;
_openApiDoc.Workspace.AddDocumentId("https://myserver.com/beta", _openApiDoc_2.BaseUri);
_openApiDoc.Workspace.RegisterComponents(_openApiDoc_2);
_openApiDoc_2.Workspace.RegisterComponents(_openApiDoc_2);
_openApiDoc.Workspace.RegisterComponents(_openApiDoc_2, OpenApiSpecVersion.OpenApi3_1);
_openApiDoc_2.Workspace.RegisterComponents(_openApiDoc_2, OpenApiSpecVersion.OpenApi3_1);

_localPathItemReference = new OpenApiPathItemReference("userPathItem", _openApiDoc_2)
{
Expand Down
Loading