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

Use the server URL to uniquely ID schema models from different clouds during registration #1562

Closed
wants to merge 5 commits into from
Closed
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
29 changes: 22 additions & 7 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
using Json.Schema;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -156,9 +157,9 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
// Fill in missing information based on the defaultUrl
if (defaultUrl != null)
{
host = host ?? defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
basePath = basePath ?? defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
schemes = schemes ?? new List<string> { defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped) };
host ??= defaultUrl.GetComponents(UriComponents.NormalizedHost, UriFormat.SafeUnescaped);
basePath ??= defaultUrl.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
schemes ??= [defaultUrl.GetComponents(UriComponents.Scheme, UriFormat.SafeUnescaped)];
}
else if (String.IsNullOrEmpty(host) && String.IsNullOrEmpty(basePath))
{
Expand Down Expand Up @@ -197,6 +198,8 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
server.Url = server.Url.Substring(0, server.Url.Length - 1);
}
}

context.SetTempStorage(TempStorageKeys.Servers, servers);
}

private static string BuildUrl(string scheme, string host, string basePath)
Expand All @@ -211,7 +214,7 @@ private static string BuildUrl(string scheme, string host, string basePath)
#if NETSTANDARD2_1_OR_GREATER
if (!String.IsNullOrEmpty(host) && host.Contains(':', StringComparison.OrdinalIgnoreCase))
#else
if (!String.IsNullOrEmpty(host) && host.Contains(':'))
if (!string.IsNullOrEmpty(host) && host.Contains(':'))
#endif
{
var pieces = host.Split(':');
Expand Down Expand Up @@ -263,7 +266,7 @@ public static OpenApiDocument LoadOpenApi(RootNode rootNode)
MakeServers(openApidoc.Servers, openApiNode.Context, rootNode);

FixRequestBodyReferences(openApidoc);
RegisterComponentsSchemasInGlobalRegistry(openApidoc.Components?.Schemas);
RegisterComponentsSchemasInGlobalRegistry(openApiNode.Context, openApidoc.Components?.Schemas);

return openApidoc;
}
Expand Down Expand Up @@ -312,7 +315,7 @@ private static bool IsHostValid(string host)
return Uri.CheckHostName(hostPart) != UriHostNameType.Unknown;
}

private static void RegisterComponentsSchemasInGlobalRegistry(IDictionary<string, JsonSchema> schemas)
private static void RegisterComponentsSchemasInGlobalRegistry(ParsingContext context, IDictionary<string, JsonSchema> schemas)
{
if (schemas == null)
{
Expand All @@ -321,7 +324,19 @@ private static void RegisterComponentsSchemasInGlobalRegistry(IDictionary<string

foreach (var schema in schemas)
{
var refUri = new Uri(OpenApiConstants.V2ReferenceUri + schema.Key);
var servers = context.GetFromTempStorage<IList<OpenApiServer>>(TempStorageKeys.Servers);
var serverUrl = servers?.FirstOrDefault()?.Url;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var serverUrl = servers?.FirstOrDefault()?.Url;
var serverUrl = servers?.FirstOrDefault()?.Url?.TrimEnd('/');


// Remove any trailing slashes from the server URL
if (!string.IsNullOrEmpty(serverUrl) && serverUrl.EndsWith("/"))
{
serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
}

Comment on lines +330 to +335
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Remove any trailing slashes from the server URL
if (!string.IsNullOrEmpty(serverUrl) && serverUrl.EndsWith("/"))
{
serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
}

var refPath = !string.IsNullOrEmpty(serverUrl) ? string.Concat(serverUrl, OpenApiConstants.V2ReferencedSchemaPath)
: OpenApiConstants.V2ReferenceUri;

var refUri = new Uri(refPath + schema.Key);
SchemaRegistry.Global.Register(refUri, schema.Value);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OpenApi.Readers/V2/TempStorageKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ internal static class TempStorageKeys
public const string GlobalConsumes = "globalConsumes";
public const string GlobalProduces = "globalProduces";
public const string ParameterIsBodyOrFormData = "parameterIsBodyOrFormData";
public const string Servers = "servers";
}
}
15 changes: 13 additions & 2 deletions src/Microsoft.OpenApi.Readers/V3/OpenApiComponentsDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// Licensed under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.Schema;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;

namespace Microsoft.OpenApi.Readers.V3
{
Expand Down Expand Up @@ -44,10 +48,17 @@ public static OpenApiComponents LoadComponents(ParseNode node)
var components = new OpenApiComponents();

ParseMap(mapNode, components, _componentsFixedFields, _componentsPatternFields);


var servers = node.Context.GetFromTempStorage<IList<OpenApiServer>>(TempStorageKeys.Servers);
var serverUrl = servers?.FirstOrDefault()?.Url;

// Examples of server url ----> "https://graph.microsoft.com/v1.0", "https://graph.microsoft.com/v1.0-fairfax"
foreach (var schema in components.Schemas)
{
var refUri = new Uri(OpenApiConstants.V3ReferenceUri + schema.Key);
var refPath = !string.IsNullOrEmpty(serverUrl) ? string.Concat(serverUrl, OpenApiConstants.V3ReferencedSchemaPath)
: OpenApiConstants.V3ReferenceUri;

var refUri = new Uri(refPath + schema.Key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the uri kind

SchemaRegistry.Global.Register(refUri, schema.Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;

namespace Microsoft.OpenApi.Readers.V3
{
Expand All @@ -21,7 +22,7 @@ internal static partial class OpenApiV3Deserializer
} /* Version is valid field but we already parsed it */
},
{"info", (o, n) => o.Info = LoadInfo(n)},
{"servers", (o, n) => o.Servers = n.CreateList(LoadServer)},
{"servers", (o, n) => n.Context.SetTempStorage(TempStorageKeys.Servers, o.Servers = n.CreateList(LoadServer))},
{"paths", (o, n) => o.Paths = LoadPaths(n)},
{"components", (o, n) => o.Components = LoadComponents(n)},
{"tags", (o, n) => {o.Tags = n.CreateList(LoadTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using Json.Schema;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;

namespace Microsoft.OpenApi.Readers.V31
{
Expand Down Expand Up @@ -42,9 +45,15 @@ public static OpenApiComponents LoadComponents(ParseNode node)

ParseMap(mapNode, components, _componentsFixedFields, _componentsPatternFields);

var servers = node.Context.GetFromTempStorage<IList<OpenApiServer>>(TempStorageKeys.Servers);
var serverUrl = servers?.FirstOrDefault()?.Url;

foreach (var schema in components.Schemas)
{
var refUri = new Uri(OpenApiConstants.V3ReferenceUri + schema.Key);
var refPath = !string.IsNullOrEmpty(serverUrl) ? string.Concat(serverUrl, OpenApiConstants.V3ReferencedSchemaPath)
: OpenApiConstants.V3ReferenceUri;

var refUri = new Uri(refPath + schema.Key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the Uri kind to avoid exceptions

SchemaRegistry.Global.Register(refUri, schema.Value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;

namespace Microsoft.OpenApi.Readers.V31
{
Expand All @@ -19,7 +20,7 @@ internal static partial class OpenApiV31Deserializer
},
{"info", (o, n) => o.Info = LoadInfo(n)},
{"jsonSchemaDialect", (o, n) => o.JsonSchemaDialect = n.GetScalarValue() },
{"servers", (o, n) => o.Servers = n.CreateList(LoadServer)},
{"servers", (o, n) => n.Context.SetTempStorage(TempStorageKeys.Servers, o.Servers = n.CreateList(LoadServer))},
{"paths", (o, n) => o.Paths = LoadPaths(n)},
{"webhooks", (o, n) => o.Webhooks = LoadPaths(n)},
{"components", (o, n) => o.Components = LoadComponents(n)},
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,16 @@ public static class OpenApiConstants
/// </summary>
public static readonly Uri defaultUrl = new("http://localhost/");

/// <summary>
/// Field: V3 JsonSchema Reference Uri
/// </summary>
public const string V3ReferencedSchemaPath = "/components/schemas/";

/// <summary>
/// Field: V2 JsonSchema Reference Uri
/// </summary>
public const string V2ReferencedSchemaPath = "/definitions/";

/// <summary>
/// Field: V3 JsonSchema Reference Uri
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ private Dictionary<string, JsonSchema> ResolveJsonSchemas(IDictionary<string, Js
/// <returns></returns>
public JsonSchema ResolveJsonSchemaReference(Uri reference, string description = null, string summary = null)
{
var refUri = $"https://registry{reference.OriginalString.Split('#').LastOrDefault()}";
var servers = _currentDocument.Servers;
var basePath = servers?.FirstOrDefault()?.Url;

basePath = RemoveTrailingSlash(basePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
basePath = RemoveTrailingSlash(basePath);
basePath = basePath?.TrimEnd('/');

var refPath = basePath ?? "https://registry";

var refUri = $"{refPath}{reference.OriginalString.Split('#').LastOrDefault()}";
var resolvedSchema = (JsonSchema)SchemaRegistry.Global.Get(new Uri(refUri));

if (resolvedSchema != null)
Expand Down Expand Up @@ -443,5 +449,14 @@ private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference)
{
return possibleReference != null && possibleReference.UnresolvedReference;
}

private string RemoveTrailingSlash(string url)
{
if (!string.IsNullOrEmpty(url) && url.EndsWith("/"))
{
return url.Substring(0, url.Length - 1);
}
return url;
}
Comment on lines +453 to +460
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private string RemoveTrailingSlash(string url)
{
if (!string.IsNullOrEmpty(url) && url.EndsWith("/"))
{
return url.Substring(0, url.Length - 1);
}
return url;
}

}
}
6 changes: 5 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ public JsonSchema ResolveJsonSchemaReference(Uri reference)
{
foreach (var jsonSchema in doc.Components.Schemas)
{
var refUri = new Uri(OpenApiConstants.V3ReferenceUri + jsonSchema.Key);
var serverUrl = doc.Servers.FirstOrDefault()?.Url;
var refPath = serverUrl != null ? string.Concat(serverUrl, OpenApiConstants.V3ReferencedSchemaPath)
: OpenApiConstants.V3ReferenceUri;

var refUri = new Uri(refPath + jsonSchema.Key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the uri kind

SchemaRegistry.Global.Register(refUri, jsonSchema.Value);
}

Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ namespace Microsoft.OpenApi.Models
public const string UniqueItems = "uniqueItems";
public const string Url = "url";
public const string V2ReferenceUri = "https://registry/definitions/";
public const string V2ReferencedSchemaPath = "/definitions/";
public const string V3ReferenceUri = "https://registry/components/schemas/";
public const string V3ReferencedSchemaPath = "/components/schemas/";
public const string Value = "value";
public const string Variables = "variables";
public const string Version = "version";
Expand Down