diff --git a/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs b/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs index b8b75bd13..bdd885951 100644 --- a/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs +++ b/src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs @@ -196,9 +196,13 @@ public override void Visit(IDictionary links) /// public override void Visit(ref JsonSchema schema) { - if (schema.GetRef() != null) + var reference = schema.GetRef(); + var description = schema.GetDescription(); + var summary = schema.GetSummary(); + + if (reference != null) { - schema = ResolveJsonSchemaReference(schema); + schema = ResolveJsonSchemaReference(reference, description, summary); } var builder = new JsonSchemaBuilder(); @@ -230,17 +234,8 @@ private Dictionary ResolveJsonSchemas(IDictionary tags) private void ResolveJsonSchema(JsonSchema schema, Action assign) { if (schema == null) return; + var reference = schema.GetRef(); - if (schema.GetRef() != null) + if (reference != null) { - assign(ResolveJsonSchemaReference(schema)); + assign(ResolveJsonSchemaReference(reference)); } } @@ -338,9 +334,10 @@ private void ResolveJsonSchemaList(IList list, Action map, Action + /// Returns the target of a JSON schema reference from within the workspace + /// + /// + /// + public JsonSchema ResolveJsonSchemaReference(Uri reference) + { + var doc = _documents.Values.First(); + if (doc != null) + { + foreach (var jsonSchema in doc.Components.Schemas) + { + var refUri = new Uri($"http://everything.json/components/schemas/{jsonSchema.Key}"); + SchemaRegistry.Global.Register(refUri, jsonSchema.Value); + } + + var resolver = new OpenApiReferenceResolver(doc); + return resolver.ResolveJsonSchemaReference(reference); + } + return null; + } + /// /// /// diff --git a/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs b/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs index 4afdedbd1..564e893a4 100644 --- a/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs +++ b/test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.cs @@ -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; @@ -75,14 +75,13 @@ public void OpenApiWorkspacesAllowDocumentsToReferenceEachOther() public void OpenApiWorkspacesCanResolveExternalReferences() { var workspace = new OpenApiWorkspace(); - workspace.AddDocument("common", CreateCommonDocument()); - var schema = workspace.ResolveReference(new OpenApiReference() - { - Id = "test", - Type = ReferenceType.Schema, - ExternalResource = "common" - }) as JsonSchema; + var doc = CreateCommonDocument(); + var location = "common"; + + workspace.AddDocument(location, doc); + var schema = workspace.ResolveJsonSchemaReference(new Uri("http://everything.json/common#/components/schemas/test")); + Assert.NotNull(schema); Assert.Equal("The referenced one", schema.GetDescription()); }