/
JsonReferenceResolver.cs
258 lines (230 loc) · 12.4 KB
/
JsonReferenceResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//-----------------------------------------------------------------------
// <copyright file="JsonReferenceResolver.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NJsonSchema.Generation;
using NJsonSchema.Infrastructure;
using NJsonSchema.References;
namespace NJsonSchema
{
/// <summary>Resolves JSON Pointer references.</summary>
public class JsonReferenceResolver
{
private readonly JsonSchemaResolver _schemaResolver;
private readonly Dictionary<string, IJsonReference> _resolvedObjects = new Dictionary<string, IJsonReference>();
/// <summary>Initializes a new instance of the <see cref="JsonReferenceResolver"/> class.</summary>
/// <param name="schemaResolver">The schema resolver.</param>
public JsonReferenceResolver(JsonSchemaResolver schemaResolver)
{
_schemaResolver = schemaResolver;
}
/// <summary>Creates the factory to be used in the FromJsonAsync method.</summary>
/// <param name="settings">The generator settings.</param>
/// <returns>The factory.</returns>
public static Func<JsonSchema4, JsonReferenceResolver> CreateJsonReferenceResolverFactory(JsonSchemaGeneratorSettings settings)
{
JsonReferenceResolver ReferenceResolverFactory(JsonSchema4 schema) =>
new JsonReferenceResolver(new JsonSchemaResolver(schema, settings));
return ReferenceResolverFactory;
}
/// <summary>Adds a document reference.</summary>
/// <param name="documentPath">The document path.</param>
/// <param name="schema">The referenced schema.</param>
public void AddDocumentReference(string documentPath, IJsonReference schema)
{
_resolvedObjects[documentPath] = schema;
}
/// <summary>Gets the object from the given JSON path.</summary>
/// <param name="rootObject">The root object.</param>
/// <param name="jsonPath">The JSON path.</param>
/// <returns>The JSON Schema or <c>null</c> when the object could not be found.</returns>
/// <exception cref="InvalidOperationException">Could not resolve the JSON path.</exception>
/// <exception cref="NotSupportedException">Could not resolve the JSON path.</exception>
public async Task<IJsonReference> ResolveReferenceAsync(object rootObject, string jsonPath)
{
return await ResolveReferenceAsync(rootObject, jsonPath, true).ConfigureAwait(false);
}
/// <summary>Gets the object from the given JSON path.</summary>
/// <param name="rootObject">The root object.</param>
/// <param name="jsonPath">The JSON path.</param>
/// <returns>The JSON Schema or <c>null</c> when the object could not be found.</returns>
/// <exception cref="InvalidOperationException">Could not resolve the JSON path.</exception>
/// <exception cref="NotSupportedException">Could not resolve the JSON path.</exception>
public async Task<IJsonReference> ResolveReferenceWithoutAppendAsync(object rootObject, string jsonPath)
{
return await ResolveReferenceAsync(rootObject, jsonPath, false).ConfigureAwait(false);
}
/// <summary>Resolves a document reference.</summary>
/// <param name="rootObject">The root object.</param>
/// <param name="jsonPath">The JSON path to resolve.</param>
/// <returns>The resolved JSON Schema.</returns>
/// <exception cref="InvalidOperationException">Could not resolve the JSON path.</exception>
public virtual IJsonReference ResolveDocumentReference(object rootObject, string jsonPath)
{
var allSegments = jsonPath.Split('/').Skip(1).ToList();
var schema = ResolveDocumentReference(rootObject, allSegments, new HashSet<object>());
if (schema == null)
throw new InvalidOperationException("Could not resolve the path '" + jsonPath + "'.");
return schema;
}
/// <summary>Resolves a file reference.</summary>
/// <param name="filePath">The file path.</param>
/// <returns>The resolved JSON Schema.</returns>
/// <exception cref="NotSupportedException">The System.IO.File API is not available on this platform.</exception>
public virtual async Task<IJsonReference> ResolveFileReferenceAsync(string filePath)
{
return await JsonSchema4.FromFileAsync(filePath, schema => this).ConfigureAwait(false);
}
/// <summary>Resolves an URL reference.</summary>
/// <param name="url">The URL.</param>
/// <exception cref="NotSupportedException">The HttpClient.GetAsync API is not available on this platform.</exception>
public virtual async Task<IJsonReference> ResolveUrlReferenceAsync(string url)
{
return await JsonSchema4.FromUrlAsync(url, schema => this).ConfigureAwait(false);
}
private async Task<IJsonReference> ResolveReferenceAsync(object rootObject, string jsonPath, bool append)
{
if (jsonPath == "#")
{
if (rootObject is IJsonReference)
return (IJsonReference)rootObject;
throw new InvalidOperationException("Could not resolve the JSON path '#' because the root object is not a JsonSchema4.");
}
else if (jsonPath.StartsWith("#/"))
{
return ResolveDocumentReference(rootObject, jsonPath);
}
else if (jsonPath.StartsWith("http://") || jsonPath.StartsWith("https://"))
return await ResolveUrlReferenceWithAlreadyResolvedCheckAsync(jsonPath, jsonPath, append).ConfigureAwait(false);
else
{
var documentPathProvider = rootObject as IDocumentPathProvider;
var documentPath = documentPathProvider?.DocumentPath;
if (documentPath != null)
{
if (documentPath.StartsWith("http://") || documentPath.StartsWith("https://"))
{
var url = new Uri(new Uri(documentPath), jsonPath).ToString();
return await ResolveUrlReferenceWithAlreadyResolvedCheckAsync(url, jsonPath, append).ConfigureAwait(false);
}
else
{
var filePath = DynamicApis.PathCombine(DynamicApis.PathGetDirectoryName(documentPath), jsonPath);
return await ResolveFileReferenceWithAlreadyResolvedCheckAsync(filePath, jsonPath, append).ConfigureAwait(false);
}
}
else
throw new NotSupportedException("Could not resolve the JSON path '" + jsonPath + "' because no document path is available.");
}
}
private async Task<IJsonReference> ResolveFileReferenceWithAlreadyResolvedCheckAsync(string fullJsonPath, string jsonPath, bool append)
{
try
{
var arr = Regex.Split(fullJsonPath, @"(?=#)");
var filePath = arr[0];
if (!_resolvedObjects.ContainsKey(filePath))
{
var schema = await ResolveFileReferenceAsync(filePath).ConfigureAwait(false);
schema.DocumentPath = jsonPath;
if (schema is JsonSchema4 && append)
_schemaResolver.AppendSchema((JsonSchema4)schema, filePath.Split('/', '\\').Last().Split('.').First());
_resolvedObjects[filePath] = schema;
}
var result = _resolvedObjects[filePath];
return arr.Length == 1 ? result : await ResolveReferenceAsync(result, arr[1]).ConfigureAwait(false);
}
catch (Exception exception)
{
throw new InvalidOperationException("Could not resolve the JSON path '" + jsonPath + "' with the full JSON path '" + fullJsonPath + "'.", exception);
}
}
private async Task<IJsonReference> ResolveUrlReferenceWithAlreadyResolvedCheckAsync(string fullJsonPath, string jsonPath, bool append)
{
try
{
var arr = fullJsonPath.Split('#');
if (!_resolvedObjects.ContainsKey(arr[0]))
{
var schema = await ResolveUrlReferenceAsync(arr[0]).ConfigureAwait(false);
schema.DocumentPath = jsonPath;
if (schema is JsonSchema4 && append)
_schemaResolver.AppendSchema((JsonSchema4)schema, null);
_resolvedObjects[arr[0]] = schema;
}
var result = _resolvedObjects[arr[0]];
return arr.Length == 1 ? result : await ResolveReferenceAsync(result, "#" + arr[1]).ConfigureAwait(false);
}
catch (Exception exception)
{
throw new InvalidOperationException("Could not resolve the JSON path '" + jsonPath + "' with the full JSON path '" + fullJsonPath + "'.", exception);
}
}
private IJsonReference ResolveDocumentReference(object obj, List<string> segments, HashSet<object> checkedObjects)
{
if (obj == null || obj is string || checkedObjects.Contains(obj))
return null;
if (obj is IJsonReference reference && reference.Reference != null)
{
var result = ResolveDocumentReferenceWithoutDereferencing(reference.Reference, segments, checkedObjects);
if (result == null)
return ResolveDocumentReferenceWithoutDereferencing(obj, segments, checkedObjects);
else
return result;
}
return ResolveDocumentReferenceWithoutDereferencing(obj, segments, checkedObjects);
}
private IJsonReference ResolveDocumentReferenceWithoutDereferencing(object obj, List<string> segments, HashSet<object> checkedObjects)
{
if (segments.Count == 0)
return obj as IJsonReference;
checkedObjects.Add(obj);
var firstSegment = segments[0];
if (obj is IDictionary)
{
if (((IDictionary)obj).Contains(firstSegment))
return ResolveDocumentReference(((IDictionary)obj)[firstSegment], segments.Skip(1).ToList(),
checkedObjects);
}
else if (obj is IEnumerable)
{
int index;
if (int.TryParse(firstSegment, out index))
{
var enumerable = ((IEnumerable)obj).Cast<object>().ToArray();
if (enumerable.Length > index)
return ResolveDocumentReference(enumerable[index], segments.Skip(1).ToList(), checkedObjects);
}
}
else
{
var extensionObj = obj as IJsonExtensionObject;
if (extensionObj?.ExtensionData?.ContainsKey(firstSegment) == true)
{
return ResolveDocumentReference(extensionObj.ExtensionData[firstSegment], segments.Skip(1).ToList(),
checkedObjects);
}
foreach (var member in ReflectionCache.GetPropertiesAndFields(obj.GetType())
.Where(p => p.CustomAttributes.JsonIgnoreAttribute == null))
{
var pathSegment = member.GetName();
if (pathSegment == firstSegment)
{
var value = member.GetValue(obj);
return ResolveDocumentReference(value, segments.Skip(1).ToList(), checkedObjects);
}
}
}
return null;
}
}
}