Skip to content

Commit

Permalink
Resolve JSON schema references from within the workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Oct 2, 2023
1 parent 6a72c05 commit 4ed5251
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
35 changes: 17 additions & 18 deletions src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ public override void Visit(IDictionary<string, OpenApiLink> links)
/// <param name="schema"></param>
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();
Expand Down Expand Up @@ -230,17 +234,8 @@ private Dictionary<string, JsonSchema> ResolveJsonSchemas(IDictionary<string, Js
return resolvedSchemas;
}

private JsonSchema ResolveJsonSchemaReference(JsonSchema schema)
public JsonSchema ResolveJsonSchemaReference(Uri reference, string description = null, string summary = null)
{
var reference = schema.GetRef();
var description = schema.GetDescription();
var summary = schema.GetSummary();

if (reference == null)
{
return schema;
}

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

Expand Down Expand Up @@ -310,10 +305,11 @@ private void ResolveTags(IList<OpenApiTag> tags)
private void ResolveJsonSchema(JsonSchema schema, Action<JsonSchema> assign)
{
if (schema == null) return;
var reference = schema.GetRef();

if (schema.GetRef() != null)
if (reference != null)
{
assign(ResolveJsonSchemaReference(schema));
assign(ResolveJsonSchemaReference(reference));
}
}

Expand All @@ -338,9 +334,10 @@ private void ResolveJsonSchemaList(IList<JsonSchema> list, Action<List<JsonSchem
for (int i = 0; i < list.Count; i++)
{
var entity = list[i];
if (entity.GetRef() != null)
var reference = entity.GetRef();
if (reference != null)
{
list[i] = ResolveJsonSchemaReference(entity);
list[i] = ResolveJsonSchemaReference(reference);
}
}

Expand Down Expand Up @@ -368,9 +365,11 @@ private void ResolveJsonSchemaMap(IDictionary<string, JsonSchema> map, Action<ID
foreach (var key in map.Keys.ToList())
{
var entity = map[key];
if (entity.GetRef() != null)
var reference = entity.GetRef();

if (reference != null)
{
map[key] = ResolveJsonSchemaReference(entity);
map[key] = ResolveJsonSchemaReference(reference);
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Json.Schema;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
Expand Down Expand Up @@ -131,6 +133,28 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
return null;
}

/// <summary>
/// Returns the target of a JSON schema reference from within the workspace
/// </summary>
/// <param name="reference"></param>
/// <returns></returns>
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;
}

/// <summary>
///
/// </summary>
Expand Down
15 changes: 7 additions & 8 deletions test/Microsoft.OpenApi.Tests/Workspaces/OpenApiWorkspaceTests.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 @@ -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());
}
Expand Down

0 comments on commit 4ed5251

Please sign in to comment.