Skip to content

Commit adcd017

Browse files
authored
fix: add missing properties to json schema (#124)
1 parent e53db72 commit adcd017

File tree

6 files changed

+189
-21
lines changed

6 files changed

+189
-21
lines changed

src/LEGO.AsyncAPI.Readers/V2/AsyncApiSchemaDeserializer.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class JsonSchemaDeserializer
130130
{
131131
if (n is ValueNode && n.GetBooleanValueOrDefault(null) == false)
132132
{
133-
a.AdditionalProperties = new NoAdditionalProperties();
133+
a.AdditionalProperties = new FalseApiSchema();
134134
}
135135
else
136136
{
@@ -139,7 +139,36 @@ public class JsonSchemaDeserializer
139139
}
140140
},
141141
{
142-
"items", (a, n) => { a.Items = LoadSchema(n); }
142+
"items", (a, n) =>
143+
{
144+
if (n is ValueNode && n.GetBooleanValueOrDefault(null) == false)
145+
{
146+
a.Items = new FalseApiSchema();
147+
}
148+
else
149+
{
150+
a.Items = LoadSchema(n);
151+
}
152+
}
153+
},
154+
{
155+
"additionalItems", (a, n) =>
156+
{
157+
if (n is ValueNode && n.GetBooleanValueOrDefault(null) == false)
158+
{
159+
a.AdditionalItems = new FalseApiSchema();
160+
}
161+
else
162+
{
163+
a.AdditionalItems = LoadSchema(n);
164+
}
165+
}
166+
},
167+
{
168+
"patternProperties", (a, n) => { a.PatternProperties = n.CreateMap(LoadSchema); }
169+
},
170+
{
171+
"propertyNames", (a, n) => { a.PropertyNames = LoadSchema(n); }
143172
},
144173
{
145174
"contains", (a, n) => { a.Contains = LoadSchema(n); }

src/LEGO.AsyncAPI/Models/AsyncApiConstants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,8 @@ public static class AsyncApiConstants
138138
public const string MaxMessageBytes = "max.message.bytes";
139139
public const string TopicConfiguration = "topicConfiguration";
140140
public const string GeoReplication = "geo-replication";
141+
public const string AdditionalItems = "additionalItems";
142+
public const string PropertyNames = "propertyNames";
143+
public const string PatternProperties = "patternProperties";
141144
}
142145
}

src/LEGO.AsyncAPI/Models/AsyncApiSchema.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
159159
/// </summary>
160160
public AsyncApiSchema Items { get; set; }
161161

162+
/// <summary>
163+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
164+
/// Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object
165+
/// and not a standard JSON Schema. items MUST be present if the type is array.
166+
/// </summary>
167+
public AsyncApiSchema AdditionalItems { get; set; }
168+
162169
/// <summary>
163170
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
164171
/// </summary>
@@ -197,6 +204,8 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
197204
/// </summary>
198205
public AsyncApiSchema AdditionalProperties { get; set; }
199206

207+
public IDictionary<string, AsyncApiSchema> PatternProperties { get; set; } = new Dictionary<string, AsyncApiSchema>();
208+
200209
/// <summary>
201210
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
202211
/// </summary>
@@ -335,7 +344,24 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)
335344
writer.WriteOptionalCollection(AsyncApiConstants.Required, this.Required, (w, s) => w.WriteValue(s));
336345

337346
// items
338-
writer.WriteOptionalObject(AsyncApiConstants.Items, this.Items, (w, s) => s.SerializeV2(w));
347+
if (this.Items is FalseApiSchema)
348+
{
349+
writer.WriteOptionalProperty<bool>(AsyncApiConstants.Items, false);
350+
}
351+
else
352+
{
353+
writer.WriteOptionalObject(AsyncApiConstants.Items, this.Items, (w, s) => s.SerializeV2(w));
354+
}
355+
356+
// additionalItems
357+
if (this.AdditionalItems is FalseApiSchema)
358+
{
359+
writer.WriteOptionalProperty<bool>(AsyncApiConstants.AdditionalItems, false);
360+
}
361+
else
362+
{
363+
writer.WriteOptionalObject(AsyncApiConstants.AdditionalItems, this.AdditionalItems, (w, s) => s.SerializeV2(w));
364+
}
339365

340366
// maxItems
341367
writer.WriteOptionalProperty(AsyncApiConstants.MaxItems, this.MaxItems);
@@ -356,7 +382,7 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)
356382
writer.WriteOptionalProperty(AsyncApiConstants.MinProperties, this.MinProperties);
357383

358384
// additionalProperties
359-
if (this.AdditionalProperties is NoAdditionalProperties)
385+
if (this.AdditionalProperties is FalseApiSchema)
360386
{
361387
writer.WriteOptionalProperty<bool>(AsyncApiConstants.AdditionalProperties, false);
362388
}
@@ -365,6 +391,10 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)
365391
writer.WriteOptionalObject(AsyncApiConstants.AdditionalProperties, this.AdditionalProperties, (w, s) => s.SerializeV2(w));
366392
}
367393

394+
writer.WriteOptionalMap(AsyncApiConstants.PatternProperties, this.PatternProperties, (w, s) => s.SerializeV2(w));
395+
396+
writer.WriteOptionalObject(AsyncApiConstants.PropertyNames, this.PropertyNames, (w, s) => s.SerializeV2(w));
397+
368398
// discriminator
369399
writer.WriteOptionalProperty(AsyncApiConstants.Discriminator, this.Discriminator);
370400

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace LEGO.AsyncAPI.Models
2+
{
3+
/// <summary>
4+
/// An object representing 'false' for properties of AsyncApiSchema that can be false OR a schema.
5+
/// </summary>
6+
/// <seealso cref="AsyncApiSchema" />
7+
public class FalseApiSchema : AsyncApiSchema
8+
{
9+
}
10+
}

src/LEGO.AsyncAPI/Models/JsonSchema/NoAdditionalProperties.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/LEGO.AsyncAPI.Tests/Models/AsyncApiSchema_Should.cs

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public class AsyncApiSchema_Should
7171
MaxLength = 15,
7272
},
7373
},
74-
AdditionalProperties = new NoAdditionalProperties(),
74+
AdditionalProperties = new FalseApiSchema(),
75+
Items = new FalseApiSchema(),
76+
AdditionalItems = new FalseApiSchema(),
7577
},
7678
["property4"] = new AsyncApiSchema
7779
{
@@ -93,6 +95,26 @@ public class AsyncApiSchema_Should
9395
MinLength = 2,
9496
},
9597
},
98+
PatternProperties = new Dictionary<string, AsyncApiSchema>()
99+
{
100+
{
101+
"^S_",
102+
new AsyncApiSchema()
103+
{
104+
Type = SchemaType.String,
105+
}
106+
},
107+
{
108+
"^I_", new AsyncApiSchema()
109+
{
110+
Type = SchemaType.Integer,
111+
}
112+
},
113+
},
114+
PropertyNames = new AsyncApiSchema()
115+
{
116+
Pattern = "^[A-Za-z_][A-Za-z0-9_]*$",
117+
},
96118
AdditionalProperties = new AsyncApiSchema
97119
{
98120
Properties = new Dictionary<string, AsyncApiSchema>
@@ -103,8 +125,28 @@ public class AsyncApiSchema_Should
103125
},
104126
},
105127
},
128+
Items = new AsyncApiSchema
129+
{
130+
Properties = new Dictionary<string, AsyncApiSchema>
131+
{
132+
["Property9"] = new AsyncApiSchema
133+
{
134+
Type = SchemaType.String | SchemaType.Null,
135+
},
136+
},
137+
},
138+
AdditionalItems = new AsyncApiSchema
139+
{
140+
Properties = new Dictionary<string, AsyncApiSchema>
141+
{
142+
["Property10"] = new AsyncApiSchema
143+
{
144+
Type = SchemaType.String | SchemaType.Null,
145+
},
146+
},
147+
},
106148
},
107-
["property9"] = new AsyncApiSchema
149+
["property11"] = new AsyncApiSchema
108150
{
109151
Const = new AsyncApiString("aSpecialConstant"),
110152
},
@@ -387,6 +429,8 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
387429
""title"": ""title1"",
388430
""properties"": {
389431
""property1"": {
432+
""items"": false,
433+
""additionalItems"": false,
390434
""properties"": {
391435
""property2"": {
392436
""type"": ""integer""
@@ -399,6 +443,26 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
399443
""additionalProperties"": false
400444
},
401445
""property4"": {
446+
""items"": {
447+
""properties"": {
448+
""Property9"": {
449+
""type"": [
450+
""null"",
451+
""string""
452+
]
453+
}
454+
}
455+
},
456+
""additionalItems"": {
457+
""properties"": {
458+
""Property10"": {
459+
""type"": [
460+
""null"",
461+
""string""
462+
]
463+
}
464+
}
465+
},
402466
""properties"": {
403467
""property5"": {
404468
""properties"": {
@@ -421,9 +485,20 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
421485
]
422486
}
423487
}
488+
},
489+
""patternProperties"": {
490+
""^S_"": {
491+
""type"": ""string""
492+
},
493+
""^I_"": {
494+
""type"": ""integer""
495+
}
496+
},
497+
""propertyNames"": {
498+
""pattern"": ""^[A-Za-z_][A-Za-z0-9_]*$""
424499
}
425500
},
426-
""property9"": {
501+
""property11"": {
427502
""const"": ""aSpecialConstant""
428503
}
429504
},
@@ -443,13 +518,15 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
443518
}
444519

445520
[Test]
446-
public void Deserialize_WithAdditionalProperties_Works()
521+
public void Deserialize_WithAdvancedSchema_Works()
447522
{
448523
// Arrange
449524
var json = @"{
450525
""title"": ""title1"",
451526
""properties"": {
452527
""property1"": {
528+
""items"": false,
529+
""additionalItems"": false,
453530
""properties"": {
454531
""property2"": {
455532
""type"": ""integer""
@@ -462,6 +539,26 @@ public void Deserialize_WithAdditionalProperties_Works()
462539
""additionalProperties"": false
463540
},
464541
""property4"": {
542+
""items"": {
543+
""properties"": {
544+
""Property9"": {
545+
""type"": [
546+
""null"",
547+
""string""
548+
]
549+
}
550+
}
551+
},
552+
""additionalItems"": {
553+
""properties"": {
554+
""Property10"": {
555+
""type"": [
556+
""null"",
557+
""string""
558+
]
559+
}
560+
}
561+
},
465562
""properties"": {
466563
""property5"": {
467564
""properties"": {
@@ -484,9 +581,20 @@ public void Deserialize_WithAdditionalProperties_Works()
484581
]
485582
}
486583
}
584+
},
585+
""patternProperties"": {
586+
""^S_"": {
587+
""type"": ""string""
588+
},
589+
""^I_"": {
590+
""type"": ""integer""
591+
}
592+
},
593+
""propertyNames"": {
594+
""pattern"": ""^[A-Za-z_][A-Za-z0-9_]*$""
487595
}
488596
},
489-
""property9"": {
597+
""property11"": {
490598
""const"": ""aSpecialConstant""
491599
}
492600
},

0 commit comments

Comments
 (0)