Skip to content

Commit fe91b13

Browse files
VisualBeanMads Glahder
andauthored
Set Type as array or string (#49)
* Set Type as array or string * Update src/LEGO.AsyncAPI/Models/SchemaType.cs Co-authored-by: Mads Glahder <32570017+boiled-water-tsar@users.noreply.github.com>
1 parent 356259e commit fe91b13

5 files changed

Lines changed: 101 additions & 50 deletions

File tree

src/LEGO.AsyncAPI.Readers/AsyncApiSchemaDeserializer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace LEGO.AsyncAPI.Readers
77
using LEGO.AsyncAPI.Extensions;
88
using LEGO.AsyncAPI.Models;
99
using LEGO.AsyncAPI.Readers.ParseNodes;
10+
using LEGO.AsyncAPI.Writers;
1011

1112
internal static partial class AsyncApiDeserializer
1213
{
@@ -16,7 +17,17 @@ internal static partial class AsyncApiDeserializer
1617
"title", (a, n) => { a.Title = n.GetScalarValue(); }
1718
},
1819
{
19-
"type", (a, n) => { a.Type = n.GetScalarValue(); }
20+
"type", (a, n) =>
21+
{
22+
if (n.GetType() == typeof(ValueNode))
23+
{
24+
a.Type = new List<SchemaType> { n.GetScalarValue().GetEnumFromDisplayName<SchemaType>() };
25+
}
26+
else
27+
{
28+
a.Type = new List<SchemaType>(n.CreateSimpleList(n2 => n2.GetScalarValue().GetEnumFromDisplayName<SchemaType>()));
29+
}
30+
}
2031
},
2132
{
2233
"required",

src/LEGO.AsyncAPI.Readers/ParseNodes/AsyncApiAnyConverter.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
6262
return new AsyncApiDateTime(dateTimeValue);
6363
}
6464
}
65-
else if (type == "string")
65+
else if (type.Contains(SchemaType.String))
6666
{
6767
if (format == "byte")
6868
{
@@ -143,55 +143,55 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
143143
}
144144
else
145145
{
146-
if (type == "integer" && format == "int32")
146+
if (type.Contains(SchemaType.Integer) && format == "int32")
147147
{
148148
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
149149
{
150150
return new AsyncApiInteger(intValue);
151151
}
152152
}
153153

154-
if (type == "integer" && format == "int64")
154+
if (type.Contains(SchemaType.Integer) && format == "int64")
155155
{
156156
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
157157
{
158158
return new AsyncApiLong(longValue);
159159
}
160160
}
161161

162-
if (type == "integer")
162+
if (type.Contains(SchemaType.Integer))
163163
{
164164
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
165165
{
166166
return new AsyncApiInteger(intValue);
167167
}
168168
}
169169

170-
if (type == "number" && format == "float")
170+
if (type.Contains(SchemaType.Number) && format == "float")
171171
{
172172
if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var floatValue))
173173
{
174174
return new AsyncApiFloat(floatValue);
175175
}
176176
}
177177

178-
if (type == "number" && format == "double")
178+
if (type.Contains(SchemaType.Number) && format == "double")
179179
{
180180
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
181181
{
182182
return new AsyncApiDouble(doubleValue);
183183
}
184184
}
185185

186-
if (type == "number")
186+
if (type.Contains(SchemaType.Number))
187187
{
188188
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
189189
{
190190
return new AsyncApiDouble(doubleValue);
191191
}
192192
}
193193

194-
if (type == "string" && format == "byte")
194+
if (type.Contains(SchemaType.String) && format == "byte")
195195
{
196196
try
197197
{
@@ -202,7 +202,7 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
202202
}
203203

204204
// binary
205-
if (type == "string" && format == "binary")
205+
if (type.Contains(SchemaType.String) && format == "binary")
206206
{
207207
try
208208
{
@@ -212,28 +212,28 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
212212
{ }
213213
}
214214

215-
if (type == "string" && format == "date")
215+
if (type.Contains(SchemaType.String) && format == "date")
216216
{
217217
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
218218
{
219219
return new AsyncApiDate(dateValue.Date);
220220
}
221221
}
222222

223-
if (type == "string" && format == "date-time")
223+
if (type.Contains(SchemaType.String) && format == "date-time")
224224
{
225225
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
226226
{
227227
return new AsyncApiDateTime(dateTimeValue);
228228
}
229229
}
230230

231-
if (type == "string")
231+
if (type.Contains(SchemaType.String))
232232
{
233233
return asyncApiAny;
234234
}
235235

236-
if (type == "boolean")
236+
if (type.Contains(SchemaType.Boolean))
237237
{
238238
if (bool.TryParse(value, out var booleanValue))
239239
{

src/LEGO.AsyncAPI/Models/AsyncApiSchema.cs

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Models
44
{
55
using System;
66
using System.Collections.Generic;
7+
using System.Linq;
78
using LEGO.AsyncAPI.Models.Interfaces;
89
using LEGO.AsyncAPI.Writers;
910

@@ -18,13 +19,12 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
1819
public string Title { get; set; }
1920

2021
/// <summary>
21-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
22-
/// Value MUST be a string. Multiple types via an array are not supported.
22+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
2323
/// </summary>
24-
public string Type { get; set; }
24+
public IList<SchemaType> Type { get; set; }
2525

2626
/// <summary>
27-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
27+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
2828
/// </summary>
2929
public string Format { get; set; }
3030

@@ -35,43 +35,43 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
3535
public string Description { get; set; }
3636

3737
/// <summary>
38-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
38+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
3939
/// </summary>
4040
public decimal? Maximum { get; set; }
4141

4242
/// <summary>
43-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
43+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
4444
/// </summary>
4545
public bool? ExclusiveMaximum { get; set; }
4646

4747
/// <summary>
48-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
48+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
4949
/// </summary>
5050
public decimal? Minimum { get; set; }
5151

5252
/// <summary>
53-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
53+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
5454
/// </summary>
5555
public bool? ExclusiveMinimum { get; set; }
5656

5757
/// <summary>
58-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
58+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
5959
/// </summary>
6060
public int? MaxLength { get; set; }
6161

6262
/// <summary>
63-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
63+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
6464
/// </summary>
6565
public int? MinLength { get; set; }
6666

6767
/// <summary>
68-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
68+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
6969
/// This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect.
7070
/// </summary>
7171
public string Pattern { get; set; }
7272

7373
/// <summary>
74-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
74+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
7575
/// </summary>
7676
public decimal? MultipleOf { get; set; }
7777

@@ -104,101 +104,101 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
104104
public bool WriteOnly { get; set; }
105105

106106
/// <summary>
107-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
107+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
108108
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
109109
/// </summary>
110110
public IList<AsyncApiSchema> AllOf { get; set; } = new List<AsyncApiSchema>();
111111

112112
/// <summary>
113-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
113+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
114114
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
115115
/// </summary>
116116
public IList<AsyncApiSchema> OneOf { get; set; } = new List<AsyncApiSchema>();
117117

118118
/// <summary>
119-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
119+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
120120
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
121121
/// </summary>
122122
public IList<AsyncApiSchema> AnyOf { get; set; } = new List<AsyncApiSchema>();
123123

124124
/// <summary>
125-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
125+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
126126
/// Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema.
127127
/// </summary>
128128
public AsyncApiSchema Not { get; set; }
129129

130130
/// <summary>
131-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
131+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
132132
/// </summary>
133133
public AsyncApiSchema Contains { get; set; }
134134

135135
/// <summary>
136-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
136+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
137137
/// </summary>
138138
public AsyncApiSchema If { get; set; }
139139

140140
/// <summary>
141-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
141+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
142142
/// </summary>
143143
public AsyncApiSchema Then { get; set; }
144144

145145
/// <summary>
146-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
146+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
147147
/// </summary>
148148
public AsyncApiSchema Else { get; set; }
149149

150150
/// <summary>
151-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
151+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
152152
/// </summary>
153153
public ISet<string> Required { get; set; } = new HashSet<string>();
154154

155155
/// <summary>
156-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
156+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
157157
/// Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object
158158
/// and not a standard JSON Schema. items MUST be present if the type is array.
159159
/// </summary>
160160
public AsyncApiSchema Items { get; set; }
161161

162162
/// <summary>
163-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
163+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
164164
/// </summary>
165165
public int? MaxItems { get; set; }
166166

167167
/// <summary>
168-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
168+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
169169
/// </summary>
170170
public int? MinItems { get; set; }
171171

172172
/// <summary>
173-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
173+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
174174
/// </summary>
175175
public bool? UniqueItems { get; set; }
176176

177177
/// <summary>
178-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
178+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
179179
/// Property definitions MUST be a Schema Object and not a standard JSON Schema (inline or referenced).
180180
/// </summary>
181181
public IDictionary<string, AsyncApiSchema> Properties { get; set; } = new Dictionary<string, AsyncApiSchema>();
182182

183183
/// <summary>
184-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
184+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
185185
/// </summary>
186186
public int? MaxProperties { get; set; }
187187

188188
/// <summary>
189-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
189+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
190190
/// </summary>
191191
public int? MinProperties { get; set; }
192192

193193
/// <summary>
194-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
194+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html
195195
/// Value can be boolean or object. Inline or referenced schema
196196
/// MUST be of a Schema Object and not a standard JSON Schema.
197197
/// </summary>
198198
public AsyncApiSchema AdditionalProperties { get; set; }
199199

200200
/// <summary>
201-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
201+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
202202
/// </summary>
203203
public AsyncApiSchema PropertyNames { get; set; }
204204

@@ -209,17 +209,17 @@ public class AsyncApiSchema : IAsyncApiReferenceable, IAsyncApiExtensible, IAsyn
209209
public string Discriminator { get; set; }
210210

211211
/// <summary>
212-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
212+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
213213
/// </summary>
214214
public IList<IAsyncApiAny> Enum { get; set; } = new List<IAsyncApiAny>();
215215

216216
/// <summary>
217-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
217+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
218218
/// </summary>
219219
public IList<IAsyncApiAny> Examples { get; set; } = new List<IAsyncApiAny>();
220220

221221
/// <summary>
222-
/// follow JSON Schema definition: https://tools.ietf.org/html/draft-handrews-json-schema-validation-01.
222+
/// follow JSON Schema definition: https://json-schema.org/draft-07/json-schema-release-notes.html.
223223
/// </summary>
224224
public IAsyncApiAny Const { get; set; }
225225

@@ -302,7 +302,17 @@ public void SerializeV2WithoutReference(IAsyncApiWriter writer)
302302
writer.WriteOptionalObject(AsyncApiConstants.Const, this.Const, (w, s) => w.WriteAny(s));
303303

304304
// type
305-
writer.WriteProperty(AsyncApiConstants.Type, this.Type);
305+
if (this.Type != null)
306+
{
307+
if (this.Type.Count == 1)
308+
{
309+
writer.WriteProperty(AsyncApiConstants.Type, this.Type.First().GetDisplayName());
310+
}
311+
else
312+
{
313+
writer.WriteOptionalCollection(AsyncApiConstants.Type, this.Type.Select(t => t.GetDisplayName()), (w, s) => w.WriteValue(s));
314+
}
315+
}
306316

307317
// allOf
308318
writer.WriteOptionalCollection(AsyncApiConstants.AllOf, this.AllOf, (w, s) => s.SerializeV2(w));

0 commit comments

Comments
 (0)