Skip to content

Commit

Permalink
Removed special $ref handling as Newtonsoft $ref handling is turned o…
Browse files Browse the repository at this point in the history
…ff anyway, closes #1186
  • Loading branch information
RicoSuter committed Sep 28, 2020
1 parent f2f4b8b commit 1ae0fdd
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public void When_property_is_ignored_then_refs_ignore_it()
//// Act
JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(foo, false, contractResolver);
var json = JsonConvert.SerializeObject(foo, Formatting.Indented, new JsonSerializerSettings { ContractResolver = contractResolver });
json = JsonSchemaReferenceUtilities.ConvertPropertyReferences(json);

//// Assert
Assert.Contains("#/definitions2/Bar", json);
Expand Down
45 changes: 45 additions & 0 deletions src/NJsonSchema.Tests/Validation/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,50 @@ public async Task When_datetime_with_regex_validation_then_datetime_is_not_alter
//// Assert
Assert.Equal(0, errors.Count);
}

[Fact]
public async Task When_property_name_is_ref_then_validation_works()
{
//// Arrange
var jsonSchema = @"{
""$schema"": ""http://json-schema.org/draft-07/schema"",
""$ref"": ""#/definitions/reference_to_other_object"",
""definitions"": {
""reference_to_other_object"": {
""type"": ""object"",
""required"": [
""$ref""
],
""additionalProperties"": false,
""properties"": {
""$ref"": {
""type"": ""string"",
""allOf"": [
{
""format"": ""uri-reference""
},
{
""pattern"": ""^.*#/datatypes/.*$""
}
]
}
}
}
}
}";

//// Act
var jsonContent = @"{
""$ref"": ""#/datatypes/MyCustomDataType""
}";

//// Arrange
var validator = new JsonSchemaValidator();
var schema = await JsonSchema.FromJsonAsync(jsonSchema);
var result = validator.Validate(jsonContent, schema);

//// Assert
Assert.Empty(result);
}
}
}
2 changes: 0 additions & 2 deletions src/NJsonSchema/Converters/JsonReferenceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
_isWriting = true;

var json = JsonConvert.SerializeObject(value, serializer.Formatting);
json = JsonSchemaReferenceUtilities.ConvertPropertyReferences(json);

if (writer.WriteState == WriteState.Property)
{
writer.WriteRawValue(json);
Expand Down
4 changes: 1 addition & 3 deletions src/NJsonSchema/Infrastructure/JsonSchemaSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static string ToJson(object obj, SchemaType schemaType, IContractResolver
CurrentSerializerSettings = null;
CurrentSchemaType = SchemaType.JsonSchema;

return JsonSchemaReferenceUtilities.ConvertPropertyReferences(json);
return json;
}

/// <summary>Deserializes JSON data to a schema with reference handling.</summary>
Expand Down Expand Up @@ -124,8 +124,6 @@ public static T FromJson<T>(string json, IContractResolver contractResolver)
PreserveReferencesHandling = PreserveReferencesHandling.None
};

json = JsonSchemaReferenceUtilities.ConvertJsonReferences(json);

var obj = JsonConvert.DeserializeObject<T>(json, CurrentSerializerSettings);
CurrentSerializerSettings = null;

Expand Down
3 changes: 0 additions & 3 deletions src/NJsonSchema/JsonPathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace NJsonSchema
/// <summary>Utilities to work with JSON paths.</summary>
public static class JsonPathUtilities
{
/// <summary>Gets the $ref replacement string.</summary>
public const string ReferenceReplaceString = "__referencePath";

/// <summary>Gets the JSON path of the given object.</summary>
/// <param name="rootObject">The root object.</param>
/// <param name="searchedObject">The object to search.</param>
Expand Down
17 changes: 0 additions & 17 deletions src/NJsonSchema/JsonSchemaReferenceUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;
using NJsonSchema.Infrastructure;
using NJsonSchema.References;
using NJsonSchema.Visitors;

Expand All @@ -38,22 +37,6 @@ public static async Task UpdateSchemaReferencesAsync(object rootObject, JsonRefe
await updater.VisitAsync(rootObject).ConfigureAwait(false);
}

/// <summary>Converts JSON references ($ref) to property references.</summary>
/// <param name="data">The data.</param>
/// <returns>The data.</returns>
public static string ConvertJsonReferences(string data)
{
return data.Replace("$ref", JsonPathUtilities.ReferenceReplaceString);
}

/// <summary>Converts property references to JSON references ($ref).</summary>
/// <param name="data">The data.</param>
/// <returns></returns>
public static string ConvertPropertyReferences(string data)
{
return data.Replace(JsonPathUtilities.ReferenceReplaceString, "$ref");
}

/// <summary>Updates the <see cref="IJsonReferenceBase.Reference" /> properties
/// from the available <see cref="IJsonReferenceBase.Reference" /> properties with inlining external references.</summary>
/// <param name="rootObject">The root object.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/References/IJsonReferenceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace NJsonSchema.References
public interface IJsonReferenceBase : IDocumentPathProvider
{
/// <summary>Gets or sets the type reference path ($ref). </summary>
[JsonProperty(JsonPathUtilities.ReferenceReplaceString, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[JsonProperty("$ref", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
string ReferencePath { get; set; }

/// <summary>Gets or sets the referenced object.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/References/JsonReferenceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class JsonReferenceBase<T> : IJsonReferenceBase
public string DocumentPath { get; set; }

/// <summary>Gets or sets the type reference path ($ref). </summary>
[JsonProperty(JsonPathUtilities.ReferenceReplaceString, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[JsonProperty("$ref", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
string IJsonReferenceBase.ReferencePath { get; set; }

/// <summary>Gets or sets the referenced object.</summary>
Expand Down

0 comments on commit 1ae0fdd

Please sign in to comment.