/
InheritanceTests.cs
343 lines (284 loc) · 10.1 KB
/
InheritanceTests.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NJsonSchema.Converters;
using NJsonSchema.Generation;
using NJsonSchema.Generation.SchemaProcessors;
using Xunit;
namespace NJsonSchema.Tests.Generation
{
public class InheritanceTests
{
[Fact]
public async Task When_more_properties_are_defined_in_allOf_and_type_none_then_all_of_contains_all_properties()
{
//// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'x-typeName': 'Foo',
'properties': {
'prop1' : { 'type' : 'string' }
},
'allOf': [
{
'type': 'object',
'properties': {
'baseProperty' : { 'type' : 'string' }
}
},
{
'properties': {
'prop2' : { 'type' : 'string' }
}
}
]
}";
//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
//// Assert
Assert.NotNull(schema.InheritedSchema);
Assert.Equal(2, schema.ActualProperties.Count);
Assert.True(schema.ActualProperties.ContainsKey("prop1"));
Assert.True(schema.ActualProperties.ContainsKey("prop2"));
}
[Fact]
public async Task When_allOf_schema_is_object_type_then_it_is_an_inherited_schema()
{
//// Arrange
var json = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'x-typeName': 'Foo',
'properties': {
'prop1' : { 'type' : 'string' }
},
'allOf': [
{
'type': 'object',
'x-typeName': 'Bar',
'properties': {
'prop2' : { 'type' : 'string' }
}
}
]
}";
//// Act
var schema = await JsonSchema4.FromJsonAsync(json);
//// Assert
Assert.NotNull(schema.InheritedSchema);
Assert.Equal(1, schema.ActualProperties.Count);
Assert.True(schema.ActualProperties.ContainsKey("prop1"));
}
[Fact]
public async Task When_generating_type_with_inheritance_then_allOf_has_one_item()
{
//// Arrange
//// Act
var schema = await JsonSchema4.FromTypeAsync<Teacher>();
//// Assert
Assert.NotNull(schema.ActualProperties["Class"]);
Assert.Equal(2, schema.AllOf.Count);
Assert.Contains(schema.Definitions, d => d.Key == "Person");
Assert.NotNull(schema.AllOf.First().ActualSchema.Properties["Name"]);
}
public class Teacher : Person
{
public string Class { get; set; }
}
public class Person
{
public string Name { get; set; }
}
[Fact]
public async Task When_generating_type_with_inheritance_and_flattening_then_schema_has_all_properties_of_inherited_classes()
{
//// Arrange
//// Act
var schema = await JsonSchema4.FromTypeAsync<CC>(new JsonSchemaGeneratorSettings
{
FlattenInheritanceHierarchy = true
});
var data = schema.ToJson();
//// Assert
Assert.Equal(4, schema.Properties.Count);
}
public abstract class AA
{
public string FirstName { get; set; }
public abstract int Age { get; set; }
}
public class BB : AA
{
public string LastName { get; set; }
public override int Age { get; set; }
}
public class CC : BB
{
public string Address { get; set; }
}
public class Dog : Animal
{
public string Foo { get; set; }
}
public class Horse : Animal
{
public string Bar { get; set; }
}
[KnownType(typeof(Dog))]
[KnownType(typeof(Horse))]
[JsonConverter(typeof(JsonInheritanceConverter), "kind")]
public class Animal
{
public string Baz { get; set; }
}
[Fact]
public async Task When_root_schema_is_inherited_then_schema_is_generated()
{
//// Arrange
//// Act
var schema = await JsonSchema4.FromTypeAsync<Animal>();
var data = schema.ToJson();
//// Assert
Assert.NotNull(data);
}
[DataContract]
[KnownType(typeof(ACommonThing))]
[KnownType(typeof(BCommonThing))]
public abstract class CommonThingBase
{
}
[DataContract]
public class ACommonThing : CommonThingBase { }
[DataContract]
public class BCommonThing : CommonThingBase { }
public class ViewModelThing
{
public CommonThingBase CommonThing { get; set; }
}
[Fact]
public async Task When_discriminator_is_externally_defined_then_it_is_generated()
{
//// Arrange
var settings = new JsonSchemaGeneratorSettings
{
SchemaProcessors =
{
new DiscriminatorSchemaProcessor(typeof(CommonThingBase), "discriminator")
}
};
//// Act
var schema = await JsonSchema4.FromTypeAsync<ViewModelThing>(settings);
var data = schema.ToJson();
//// Assert
Assert.True(schema.Definitions.ContainsKey(nameof(CommonThingBase)));
Assert.True(schema.Definitions.ContainsKey(nameof(ACommonThing)));
Assert.True(schema.Definitions.ContainsKey(nameof(BCommonThing)));
var baseSchema = schema.Definitions[nameof(CommonThingBase)];
Assert.Equal("discriminator", baseSchema.Discriminator);
}
[Fact]
public async Task When_serializing_object_with_inheritance_then_discriminator_is_added()
{
/// Arrange
var thing = new ViewModelThing
{
CommonThing = new ACommonThing()
};
/// Act
var json = JsonConvert.SerializeObject(thing, Formatting.Indented, new[]
{
new JsonInheritanceConverter(typeof(CommonThingBase), "discriminator")
});
/// Assert
Assert.Contains("\"discriminator\": \"ACommonThing\"", json);
}
[Fact]
public async Task When_deserializing_object_with_inheritance_then_correct_type_is_generated()
{
/// Arrange
var json =
@"{
""CommonThing"": {
""discriminator"": ""ACommonThing""
}
}";
/// Act
var vm = JsonConvert.DeserializeObject<ViewModelThing>(json, new JsonSerializerSettings
{
Converters = new[]
{
new JsonInheritanceConverter(typeof(CommonThingBase), "discriminator")
}
});
/// Assert
Assert.Equal(typeof(ACommonThing), vm.CommonThing.GetType());
}
[KnownType(typeof(InheritedClass_WithStringDiscriminant))]
[JsonConverter(typeof(JsonInheritanceConverter), nameof(Kind))]
public class BaseClass_WithStringDiscriminant
{
public string Kind { get; set; }
}
public class InheritedClass_WithStringDiscriminant : BaseClass_WithStringDiscriminant
{
}
[Fact]
public async Task Existing_string_property_can_be_discriminant()
{
//// Arrange
//// Act
var schema = await JsonSchema4.FromTypeAsync<BaseClass_WithStringDiscriminant>();
//// Assert
Assert.NotNull(schema.Properties["Kind"]);
}
[KnownType(typeof(InheritedClass_WithIntDiscriminant))]
[JsonConverter(typeof(JsonInheritanceConverter), nameof(Kind))]
public class BaseClass_WithIntDiscriminant
{
public int Kind { get; set; }
}
public class InheritedClass_WithIntDiscriminant : BaseClass_WithStringDiscriminant
{
}
[Fact]
public async Task Existing_non_string_property_cant_be_discriminant()
{
//// Arrange
//// Act
Task<JsonSchema4> GetSchema() => JsonSchema4.FromTypeAsync<BaseClass_WithIntDiscriminant>();
//// Assert
await Assert.ThrowsAsync<InvalidOperationException>(GetSchema);
}
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar : Dictionary<string, string>
{
public string Baz { get; set; }
}
[Fact]
public async Task When_class_inherits_from_dictionary_then_allOf_contains_base_dictionary_schema_and_actual_schema()
{
//// Arrange
var settings = new JsonSchemaGeneratorSettings
{
SchemaType = SchemaType.OpenApi3
};
//// Act
var schema = await JsonSchema4.FromTypeAsync<Foo>(settings);
var json = schema.ToJson();
//// Assert
var bar = schema.Definitions["Bar"];
Assert.Equal(2, bar.AllOf.Count);
Assert.Equal(bar.AllOf.Last(), bar.ActualTypeSchema);
Assert.Equal(bar.AllOf.First(), bar.InheritedSchema);
Assert.True(bar.AllOf.First().IsDictionary); // base class (dictionary)
Assert.True(bar.AllOf.Last().ActualProperties.Any()); // actual class
}
}
}