-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSchemaTestBase.cs
More file actions
451 lines (379 loc) · 18.2 KB
/
SchemaTestBase.cs
File metadata and controls
451 lines (379 loc) · 18.2 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json.Nodes;
using com.IvanMurzak.ReflectorNet.Utils;
using Xunit.Abstractions;
namespace com.IvanMurzak.ReflectorNet.Tests.SchemaTests
{
public abstract class SchemaTestBase : BaseTest
{
static readonly Type[] RestrictedDefineTypes = new Type[]
{
typeof(string),
typeof(object),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(Guid),
typeof(TimeSpan),
typeof(Uri)
};
protected SchemaTestBase(ITestOutputHelper output) : base(output)
{
}
protected JsonNode? JsonSchemaValidation(Type type, Reflector? reflector = null)
{
reflector ??= new Reflector();
var schema = reflector.GetSchema(type);
_output.WriteLine($"Schema for {type.GetTypeShortName()}");
_output.WriteLine($"{schema}");
Assert.NotNull(schema);
if (schema.AsObject().TryGetPropertyValue(JsonSchema.Error, out var errorValue))
{
Assert.Fail(errorValue!.ToString());
}
Assert.NotNull(schema.AsObject());
return schema;
}
protected void TestMethodInputs_PropertyRefs(Reflector? reflector, MethodInfo methodInfo, params string[] parameterNames)
{
reflector ??= new Reflector();
var schema = reflector.GetArgumentsSchema(methodInfo)!;
_output.WriteLine(schema.ToString());
Assert.NotNull(schema);
Assert.NotNull(schema[JsonSchema.Defs]);
var defines = schema[JsonSchema.Defs]?.AsObject();
Assert.NotNull(defines);
var properties = schema[JsonSchema.Properties]?.AsObject();
Assert.NotNull(properties);
foreach (var parameterName in parameterNames)
{
var methodParameter = methodInfo.GetParameters().FirstOrDefault(p => p.Name == parameterName);
Assert.NotNull(methodParameter);
var typeId = methodParameter.ParameterType.GetSchemaTypeId();
var refString = $"{JsonSchema.RefValue}{typeId}";
var targetDefine = defines[typeId];
Assert.NotNull(targetDefine);
var refStringValue = properties.FirstOrDefault(kvp
=> kvp.Value!.AsObject().TryGetPropertyValue(JsonSchema.Ref, out var refValue)
&& refString == refValue?.ToString())
.Value
?.ToString();
Assert.False(string.IsNullOrEmpty(refStringValue));
}
}
protected JsonNode? TestMethodInputs_Defines(Reflector? reflector, MethodInfo methodInfo, params Type[] expectedTypes)
{
reflector ??= new Reflector();
var schema = reflector.GetArgumentsSchema(methodInfo)!;
_output.WriteLine(schema.ToString());
Assert.NotNull(schema);
Assert.NotNull(schema[JsonSchema.Defs]);
var defines = schema[JsonSchema.Defs]?.AsObject();
Assert.NotNull(defines);
var properties = schema[JsonSchema.Properties]?.AsObject();
Assert.NotNull(properties);
foreach (var expectedType in expectedTypes)
{
var typeId = expectedType.GetSchemaTypeId();
var targetDefine = defines[typeId];
Assert.NotNull(targetDefine);
}
return schema;
}
#region Return Schema Helper Methods
/// <summary>
/// Gets the return schema for a method by name
/// </summary>
protected JsonNode? GetReturnSchemaForMethod(string methodName, bool justRef = false)
{
var reflector = new Reflector();
var methodInfo = GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance)!;
var schema = reflector.GetReturnSchema(methodInfo, justRef);
_output.WriteLine($"Return schema for {methodName}:");
_output.WriteLine(schema?.ToString() ?? "null");
_output.WriteLine("");
return schema;
}
/// <summary>
/// Asserts that a primitive return type has the correct schema structure
/// </summary>
protected void AssertPrimitiveReturnSchema(JsonNode schema, string expectedJsonType, bool shouldBeRequired = true)
{
Assert.NotNull(schema);
Assert.Equal(JsonSchema.Object, schema[JsonSchema.Type]?.ToString());
var properties = schema[JsonSchema.Properties]!.AsObject();
Assert.True(properties.ContainsKey(JsonSchema.Result));
Assert.Equal(expectedJsonType, properties[JsonSchema.Result]![JsonSchema.Type]?.ToString());
if (shouldBeRequired)
{
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Required));
var required = schema[JsonSchema.Required]!.AsArray();
Assert.Single(required);
Assert.Equal(JsonSchema.Result, required[0]?.ToString());
}
else
{
AssertResultNotRequired(schema);
}
}
/// <summary>
/// Asserts that "result" is NOT in the required array (for nullable types)
/// </summary>
protected void AssertResultNotRequired(JsonNode schema)
{
if (schema.AsObject().ContainsKey(JsonSchema.Required))
{
var required = schema[JsonSchema.Required]!.AsArray();
Assert.DoesNotContain(required, r => r?.ToString() == JsonSchema.Result);
}
}
/// <summary>
/// Asserts that "result" IS in the required array (for non-nullable types)
/// </summary>
protected void AssertResultRequired(JsonNode schema)
{
if (schema.AsObject().ContainsKey(JsonSchema.Required))
{
var required = schema[JsonSchema.Required]!.AsArray();
Assert.Contains(required, r => r?.ToString() == JsonSchema.Result);
}
}
/// <summary>
/// Asserts that all expected types are defined in the $defs section of the schema OR referenced within the schema.
/// This method recursively checks all $ref values in the schema to ensure nested types are properly referenced.
/// Only non-primitive and non-enum types should be included in $defs, as primitives and enums are inlined.
/// Verifies that at minimum the expected types are present (additional types may be included by the schema generator).
/// </summary>
protected void AssertResultDefines(JsonNode schema, params Type[] expectedTypes)
{
Assert.NotNull(schema);
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs), "Schema should contain $defs section");
var defines = schema[JsonSchema.Defs]!.AsObject();
Assert.NotNull(defines);
// Collect all $ref values throughout the schema (includes nested references)
var allReferences = new HashSet<string>();
CollectAllReferences(schema, allReferences);
// Filter expected types to only include non-primitive, non-enum types
var nonPrimitiveTypes = expectedTypes.Where(t => !TypeUtils.IsPrimitive(t) && !t.IsEnum).ToArray();
// Verify all expected types are present in $defs and referenced
foreach (var expectedType in nonPrimitiveTypes)
{
var expectedTypeId = expectedType.GetSchemaTypeId();
// Check if the type is either:
// 1. Directly defined in $defs (exact match)
var isDirectlyDefined = defines.ContainsKey(expectedTypeId);
// 2. Referenced somewhere in the schema (exact match in $ref)
var expectedRef = $"{JsonSchema.RefValue}{expectedTypeId}";
var isReferenced = allReferences.Contains(expectedRef);
Assert.True(isDirectlyDefined && isReferenced,
$"Expected type '{expectedType.GetTypeShortName()}' with ID '{expectedTypeId}' should be both defined in $defs and referenced in schema. " +
$"Expected $ref: '{expectedRef}'. " +
$"Defined types: {string.Join(", ", defines.Select(d => d.Key))}. " +
$"Referenced types: {string.Join(", ", allReferences)}");
}
// Verify that if any of the expected types appear in $defs, they are not primitive or enum
foreach (var expectedType in expectedTypes)
{
var expectedTypeId = expectedType.GetSchemaTypeId();
if (defines.ContainsKey(expectedTypeId))
{
Assert.False(TypeUtils.IsPrimitive(expectedType) || expectedType.IsEnum,
$"Type '{expectedType.GetTypeShortName()}' with ID '{expectedTypeId}' is primitive or enum and should not be in $defs. " +
$"Primitives and enums should be inlined in the schema.");
}
}
}
/// <summary>
/// Helper method to recursively collect all $ref values in a JSON schema
/// </summary>
private void CollectAllReferences(JsonNode? node, HashSet<string> references)
{
if (node == null) return;
if (node is JsonObject obj)
{
foreach (var kvp in obj)
{
if (kvp.Key == JsonSchema.Ref && kvp.Value != null)
{
references.Add(kvp.Value.ToString());
}
CollectAllReferences(kvp.Value, references);
}
}
else if (node is JsonArray arr)
{
foreach (var item in arr)
{
CollectAllReferences(item, references);
}
}
}
/// <summary>
/// Asserts that all $ref references found in the schema are defined in the $defs section.
/// This method recursively scans the entire schema for all $ref values and verifies that
/// each referenced type has a corresponding definition in $defs.
/// </summary>
protected void AssertAllRefsDefined(JsonNode schema)
{
Assert.NotNull(schema);
// Collect all $ref values in the schema
var allReferences = new HashSet<string>();
CollectAllReferences(schema, allReferences);
// If there are no references, we're done
if (allReferences.Count == 0)
{
return;
}
// References don't include restricted types
foreach (var reference in allReferences)
{
Assert.False(RestrictedDefineTypes.Any(x => JsonSchema.RefValue + x.GetSchemaTypeId() == reference),
$"Reference '{reference}' is for a restricted type that should not appear as a $ref.");
}
// Schema must have $defs if there are references
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs),
$"Schema contains {allReferences.Count} $ref reference(s) but no $defs section. References: {string.Join(", ", allReferences)}");
var defines = schema[JsonSchema.Defs]!.AsObject();
Assert.NotNull(defines);
// Check each reference to ensure it's defined
foreach (var reference in allReferences)
{
// Extract the type ID from the reference (e.g., "#/$defs/TypeId" -> "TypeId")
var typeId = reference.Replace(JsonSchema.RefValue, string.Empty);
Assert.True(defines.ContainsKey(typeId),
$"Reference '{reference}' (type ID: '{typeId}') is not defined in $defs. " +
$"Available definitions: {string.Join(", ", defines.Select(d => d.Key))}");
}
// Defines don't include restricted types
foreach (var define in defines)
{
Assert.False(RestrictedDefineTypes.Any(x => x.GetSchemaTypeId() == define.Key),
$"Reference '{define.Key}' is for a restricted type that should not appear as a $ref.");
}
}
/// <summary>
/// Asserts that a custom type return schema has the correct structure
/// </summary>
protected void AssertCustomTypeReturnSchema(JsonNode schema, string[] expectedProperties, bool shouldBeRequired = true)
{
Assert.NotNull(schema);
Assert.Equal(JsonSchema.Object, schema[JsonSchema.Type]?.ToString());
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Properties));
var properties = schema[JsonSchema.Properties]!.AsObject();
Assert.True(properties.ContainsKey(JsonSchema.Result));
var resultSchema = properties[JsonSchema.Result]!.AsObject();
// Check if it's a ref or inline
if (resultSchema.ContainsKey(JsonSchema.Ref))
{
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs));
}
else if (resultSchema.ContainsKey(JsonSchema.Properties))
{
var typeProperties = resultSchema[JsonSchema.Properties]!.AsObject();
foreach (var expectedProp in expectedProperties)
{
Assert.True(typeProperties.ContainsKey(expectedProp));
}
}
else
{
// If neither ref nor inline properties, this is unexpected
Assert.True(resultSchema.ContainsKey(JsonSchema.Ref) || resultSchema.ContainsKey(JsonSchema.Properties),
"Result schema should contain either $ref or properties");
}
if (shouldBeRequired)
AssertResultRequired(schema);
else
AssertResultNotRequired(schema);
}
/// <summary>
/// Asserts that an array return type has the correct schema structure
/// </summary>
protected void AssertArrayReturnSchema(JsonNode schema, string expectedItemType, bool shouldBeRequired = true)
{
Assert.NotNull(schema);
Assert.Equal(JsonSchema.Object, schema[JsonSchema.Type]?.ToString());
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Properties));
var properties = schema[JsonSchema.Properties]!.AsObject();
Assert.True(properties.ContainsKey(JsonSchema.Result));
var resultNode = properties[JsonSchema.Result]!;
if (resultNode is JsonObject resultSchema && resultSchema.ContainsKey(JsonSchema.Ref))
{
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs));
}
else if (resultNode is JsonObject resultInlineSchema)
{
Assert.Equal(JsonSchema.Array, resultInlineSchema[JsonSchema.Type]?.ToString());
Assert.True(resultInlineSchema.ContainsKey(JsonSchema.Items));
var items = resultInlineSchema[JsonSchema.Items]!;
Assert.Equal(expectedItemType, items[JsonSchema.Type]?.ToString());
}
else
{
Assert.Fail("Expected result to be a schema object");
}
if (shouldBeRequired)
AssertResultRequired(schema);
else
AssertResultNotRequired(schema);
}
/// <summary>
/// Asserts that a List<ComplexReturnType> return type has the correct schema structure
/// </summary>
protected void AssertComplexListReturnSchema(JsonNode schema, bool shouldBeRequired = true, bool itemsAreNullable = false)
{
Assert.NotNull(schema);
Assert.Equal(JsonSchema.Object, schema[JsonSchema.Type]?.ToString());
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Properties));
var properties = schema[JsonSchema.Properties]!.AsObject();
Assert.True(properties.ContainsKey(JsonSchema.Result));
var resultNode = properties[JsonSchema.Result]!;
if (resultNode is JsonObject resultSchema && resultSchema.ContainsKey(JsonSchema.Ref))
{
// Result is a reference to a list definition
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs));
}
else if (resultNode is JsonObject resultInlineSchema)
{
// Result is an inline array schema
Assert.Equal(JsonSchema.Array, resultInlineSchema[JsonSchema.Type]?.ToString());
Assert.True(resultInlineSchema.ContainsKey(JsonSchema.Items));
var items = resultInlineSchema[JsonSchema.Items]!;
// Items should be either a reference to ComplexReturnType or an inline object
if (items is JsonObject itemsSchema)
{
if (itemsSchema.ContainsKey(JsonSchema.Ref))
{
// Items are a reference to ComplexReturnType
Assert.Contains("ComplexReturnType", itemsSchema[JsonSchema.Ref]?.ToString());
Assert.True(schema.AsObject().ContainsKey(JsonSchema.Defs));
}
else if (itemsSchema.ContainsKey(JsonSchema.Type))
{
// Items are inlined
Assert.Equal(JsonSchema.Object, itemsSchema[JsonSchema.Type]?.ToString());
}
else
{
Assert.Fail("Items schema should contain either $ref or type");
}
}
else
{
Assert.Fail("Expected items to be a schema object");
}
}
else
{
Assert.Fail("Expected result to be a schema object");
}
if (shouldBeRequired)
AssertResultRequired(schema);
else
AssertResultNotRequired(schema);
}
#endregion
}
}