Skip to content

Commit

Permalink
Fix file resolve when relative file initial file is added (#887)
Browse files Browse the repository at this point in the history
* Add (failing) test

Using a custom resolver with relative paths fails because an attempt is
made to resolve the physical path for this (non-existant) file.

* Fix file document resolution
  • Loading branch information
JPAhnen authored and RicoSuter committed Jan 25, 2019
1 parent 7709863 commit 2f83661
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/NJsonSchema.Tests/References/LocalReferencesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace NJsonSchema.Tests.References
{
using NJsonSchema.Generation;

public class LocalReferencesTests
{
[Fact]
Expand All @@ -27,7 +29,7 @@ public async Task When_definitions_is_nested_then_refs_work()
}
}
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
var j = schema.ToJson();
Expand Down Expand Up @@ -81,6 +83,46 @@ public async Task When_document_has_indirect_external_ref_than_it_is_loaded()
Assert.Equal(1, schema.Definitions.Count);
}

[Fact]
public async Task When_reference_is_registered_in_custom_resolver_it_should_not_try_to_access_file()
{
//// Arrange
var externalSchema = await JsonSchema4.FromJsonAsync(
@"{
""type"": ""object"",
""properties"": {
""foo"": {
""type"": ""string""
}
}
}");

Func<JsonSchema4, JsonReferenceResolver> factory = schema4 =>
{
var schemaResolver = new JsonSchemaResolver(schema4, new JsonSchemaGeneratorSettings());
var resolver = new JsonReferenceResolver(schemaResolver);
resolver.AddDocumentReference("../dir/external.json", externalSchema);
return resolver;
};

string schemaJson =
@"{
""$schema"": ""http://json-schema.org/draft-07/schema#"",
""type"": ""object"",
""properties"": {
""title"": {
""$ref"": ""../dir/external.json#""
}
}
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(schemaJson, ".", factory);

//// Assert
Assert.NotNull(schema);
}

private string GetTestDirectory()
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ JsonReferenceResolver ReferenceResolverFactory(JsonSchema4 schema) =>
/// <param name="schema">The referenced schema.</param>
public void AddDocumentReference(string documentPath, IJsonReference schema)
{
_resolvedObjects[documentPath] = schema;
_resolvedObjects[documentPath.Contains("://") ? documentPath : DynamicApis.GetFullPath(documentPath)] = schema;
}

/// <summary>Gets the object from the given JSON path.</summary>
Expand Down

0 comments on commit 2f83661

Please sign in to comment.