Skip to content

Commit

Permalink
Improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Oct 16, 2017
1 parent 0f8e0f3 commit 36292c2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
21 changes: 4 additions & 17 deletions src/NJsonSchema/JsonSchemaReferenceUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public static void UpdateSchemaReferencePaths(object rootObject, bool removeExte

private class JsonReferenceUpdater : JsonSchemaVisitor
{
private bool _inlineDefinitionsRound;
private readonly object _rootObject;
private readonly JsonReferenceResolver _referenceResolver;

Expand All @@ -80,28 +79,16 @@ public JsonReferenceUpdater(object rootObject, JsonReferenceResolver referenceRe
_referenceResolver = referenceResolver;
}

public override async Task VisitAsync(object obj)
{
_inlineDefinitionsRound = true;
await base.VisitAsync(obj);

_inlineDefinitionsRound = false;
await base.VisitAsync(obj);
}

protected override async Task<IJsonReference> VisitJsonReferenceAsync(IJsonReference reference, string path, string typeNameHint)
{
if (reference.ReferencePath != null && reference.Reference == null)
{
if (_inlineDefinitionsRound)
if (path.EndsWith("/definitions/" + typeNameHint))
{
// inline $refs in "definitions"
if (path.EndsWith("/definitions/" + typeNameHint))
{
return await _referenceResolver
.ResolveReferenceWithoutAppendAsync(_rootObject, reference.ReferencePath)
.ConfigureAwait(false);
}
return await _referenceResolver
.ResolveReferenceWithoutAppendAsync(_rootObject, reference.ReferencePath)
.ConfigureAwait(false);
}
else
{
Expand Down
22 changes: 11 additions & 11 deletions src/NJsonSchema/JsonSchemaVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ protected virtual async Task VisitAsync(object obj, string path, string typeName
if (schema == null)
return;

foreach (var p in schema.Definitions.ToArray())
{
await VisitAsync(p.Value, path + "/definitions/" + p.Key, p.Key, checkedObjects, o =>
{
if (o != null)
schema.Definitions[p.Key] = (JsonSchema4)o;
else
schema.Definitions.Remove(p.Key);
});
}

if (schema.AdditionalItemsSchema != null)
await VisitAsync(schema.AdditionalItemsSchema, path + "/additionalItems", null, checkedObjects, o => schema.AdditionalItemsSchema = (JsonSchema4)o);

Expand Down Expand Up @@ -101,17 +112,6 @@ protected virtual async Task VisitAsync(object obj, string path, string typeName

foreach (var p in schema.PatternProperties.ToArray())
await VisitAsync(p.Value, path + "/patternProperties/" + p.Key, null, checkedObjects, o => schema.PatternProperties[p.Key] = (JsonProperty)o);

foreach (var p in schema.Definitions.ToArray())
{
await VisitAsync(p.Value, path + "/definitions/" + p.Key, p.Key, checkedObjects, o =>
{
if (o != null)
schema.Definitions[p.Key] = (JsonSchema4)o;
else
schema.Definitions.Remove(p.Key);
});
}
}
else
{
Expand Down

0 comments on commit 36292c2

Please sign in to comment.