-
Notifications
You must be signed in to change notification settings - Fork 251
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
Changes from all commits
efe2135
fc03ad3
a521169
3a70681
8f1de8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||
|
@@ -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)) | ||||||||||||
{ | ||||||||||||
|
@@ -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) | ||||||||||||
|
@@ -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(':'); | ||||||||||||
|
@@ -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; | ||||||||||||
} | ||||||||||||
|
@@ -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) | ||||||||||||
{ | ||||||||||||
|
@@ -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; | ||||||||||||
|
||||||||||||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
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); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set the uri kind |
||
SchemaRegistry.Global.Register(refUri, schema.Value); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
var refPath = basePath ?? "https://registry"; | ||||||||||||||||||
|
||||||||||||||||||
var refUri = $"{refPath}{reference.OriginalString.Split('#').LastOrDefault()}"; | ||||||||||||||||||
var resolvedSchema = (JsonSchema)SchemaRegistry.Global.Get(new Uri(refUri)); | ||||||||||||||||||
|
||||||||||||||||||
if (resolvedSchema != null) | ||||||||||||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. set the uri kind |
||
SchemaRegistry.Global.Register(refUri, jsonSchema.Value); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.