<PackageReference Include="NJsonSchema" Version="10.1.24" />

JsonSchemaReferenceUtilities

public static class JsonSchemaReferenceUtilities
Provides utilities to resolve and set JSON schema references.
using Newtonsoft.Json.Serialization; using NJsonSchema.References; using NJsonSchema.Visitors; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NJsonSchema { public static class JsonSchemaReferenceUtilities { private class JsonReferenceUpdater : AsyncJsonReferenceVisitorBase { private readonly object _rootObject; private readonly JsonReferenceResolver _referenceResolver; private bool _replaceRefsRound; public JsonReferenceUpdater(object rootObject, JsonReferenceResolver referenceResolver, IContractResolver contractResolver) : base(contractResolver) { _rootObject = rootObject; _referenceResolver = referenceResolver; } public override async Task VisitAsync(object obj) { _replaceRefsRound = true; await base.VisitAsync(obj).ConfigureAwait(false); _replaceRefsRound = false; await base.VisitAsync(obj).ConfigureAwait(false); } protected override async Task<IJsonReference> VisitJsonReferenceAsync(IJsonReference reference, string path, string typeNameHint) { if (reference.ReferencePath != null && reference.Reference == null) { if (_replaceRefsRound) { if (path.EndsWith("/definitions/" + typeNameHint) || path.EndsWith("/schemas/" + typeNameHint)) return await _referenceResolver.ResolveReferenceWithoutAppendAsync(_rootObject, reference.ReferencePath).ConfigureAwait(false); } else reference.Reference = await _referenceResolver.ResolveReferenceAsync(_rootObject, reference.ReferencePath).ConfigureAwait(false); } return reference; } } private class JsonReferencePathUpdater : JsonReferenceVisitorBase { private readonly object _rootObject; private readonly Dictionary<IJsonReference, IJsonReference> _schemaReferences; private readonly bool _removeExternalReferences; private readonly IContractResolver _contractResolver; public JsonReferencePathUpdater(object rootObject, Dictionary<IJsonReference, IJsonReference> schemaReferences, bool removeExternalReferences, IContractResolver contractResolver) : base(contractResolver) { _rootObject = rootObject; _schemaReferences = schemaReferences; _removeExternalReferences = removeExternalReferences; _contractResolver = contractResolver; } protected override IJsonReference VisitJsonReference(IJsonReference reference, string path, string typeNameHint) { if (reference.Reference != null) { if (!_removeExternalReferences || reference.Reference.DocumentPath == null) _schemaReferences[reference] = reference.Reference.ActualObject; else { IJsonReference reference2 = reference.Reference; object rootObject = reference2.FindParentDocument(); reference.ReferencePath = reference2.DocumentPath + JsonPathUtilities.GetJsonPath(rootObject, reference2, _contractResolver).TrimEnd(new char[1] { '#' }); } } else if (_removeExternalReferences && _rootObject != reference && reference.DocumentPath != null) { throw new NotSupportedException("removeExternalReferences not supported"); } return reference; } } public static Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver) { return UpdateSchemaReferencesAsync(rootObject, referenceResolver, new DefaultContractResolver()); } public static async Task UpdateSchemaReferencesAsync(object rootObject, JsonReferenceResolver referenceResolver, IContractResolver contractResolver) { await new JsonReferenceUpdater(rootObject, referenceResolver, contractResolver).VisitAsync(rootObject).ConfigureAwait(false); } public static string ConvertJsonReferences(string data) { return data.Replace("$ref", "__referencePath"); } public static string ConvertPropertyReferences(string data) { return data.Replace("__referencePath", "$ref"); } public static void UpdateSchemaReferencePaths(object rootObject) { UpdateSchemaReferencePaths(rootObject, false, new DefaultContractResolver()); } public static void UpdateSchemaReferencePaths(object rootObject, bool removeExternalReferences, IContractResolver contractResolver) { Dictionary<IJsonReference, IJsonReference> dictionary = new Dictionary<IJsonReference, IJsonReference>(); new JsonReferencePathUpdater(rootObject, dictionary, removeExternalReferences, contractResolver).Visit(rootObject); IEnumerable<IJsonReference> searchedObjects = (from p in dictionary select p.Value).Distinct(); IReadOnlyDictionary<object, string> jsonPaths = JsonPathUtilities.GetJsonPaths(rootObject, searchedObjects, contractResolver); foreach (KeyValuePair<IJsonReference, IJsonReference> item in dictionary) { item.Key.ReferencePath = jsonPaths[item.Value]; } } } }