Skip to content

Commit c11bf82

Browse files
committed
Use schema 'id' as a locator for schema registration and performing lookups in the component registry
1 parent 7fd7ca9 commit c11bf82

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -529,15 +529,22 @@ internal IOpenApiReferenceable ResolveReference(OpenApiReference reference, bool
529529
}
530530

531531
string uriLocation;
532-
string relativePath = OpenApiConstants.ComponentsSegment + reference.Type.GetDisplayName() + "/" + reference.Id;
532+
if (reference.Id.Contains("/")) // this means its a URL reference
533+
{
534+
uriLocation = reference.Id;
535+
}
536+
else
537+
{
538+
string relativePath = OpenApiConstants.ComponentsSegment + reference.Type.GetDisplayName() + "/" + reference.Id;
533539

534-
uriLocation = useExternal
535-
? Workspace.GetDocumentId(reference.ExternalResource)?.OriginalString + relativePath
536-
: BaseUri + relativePath;
540+
uriLocation = useExternal
541+
? Workspace.GetDocumentId(reference.ExternalResource)?.OriginalString + relativePath
542+
: BaseUri + relativePath;
543+
}
537544

538545
return Workspace.ResolveReference<IOpenApiReferenceable>(uriLocation);
539546
}
540-
547+
541548
/// <summary>
542549
/// Parses a local file path or Url into an Open API document.
543550
/// </summary>

src/Microsoft.OpenApi/Reader/V31/OpenApiV31Deserializer.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,19 @@ private static string LoadString(ParseNode node)
147147

148148
private static (string, string) GetReferenceIdAndExternalResource(string pointer)
149149
{
150+
/* Check whether the reference pointer is a URL
151+
* (id keyword allows you to supply a URL for the schema as a target for referencing)
152+
* E.g. $ref: 'https://example.com/schemas/resource.json'
153+
* or its a normal json pointer fragment syntax
154+
* E.g. $ref: '#/components/schemas/pet'
155+
*/
150156
var refSegments = pointer.Split('/');
151-
var refId = refSegments.Last();
152-
var isExternalResource = !refSegments.First().StartsWith("#");
157+
string refId = !pointer.Contains('#') ? pointer : refSegments.Last();
153158

154-
string externalResource = isExternalResource ? $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}" : null;
159+
var isExternalResource = !refSegments.First().StartsWith("#");
160+
string externalResource = isExternalResource
161+
? $"{refSegments.First()}/{refSegments[1].TrimEnd('#')}"
162+
: null;
155163

156164
return (refId, externalResource);
157165
}

src/Microsoft.OpenApi/Services/OpenApiComponentsRegistryExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void RegisterComponents(this OpenApiWorkspace workspace, OpenApiDo
2020
{
2121
if (item.Value.Id != null)
2222
{
23-
location = document.BaseUri + item.Value.Id;
23+
location = item.Value.Id;
2424
}
2525
else
2626
{

0 commit comments

Comments
 (0)