Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/LEGO.AsyncAPI.Readers/AsyncApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace LEGO.AsyncAPI.Readers
using LEGO.AsyncAPI.Extensions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.AsyncAPI.Writers;

internal static partial class AsyncApiDeserializer
{
Expand All @@ -16,7 +17,17 @@ internal static partial class AsyncApiDeserializer
"title", (a, n) => { a.Title = n.GetScalarValue(); }
},
{
"type", (a, n) => { a.Type = n.GetScalarValue(); }
"type", (a, n) =>
{
if (n.GetType() == typeof(ValueNode))
{
a.Type = new List<SchemaType> { n.GetScalarValue().GetEnumFromDisplayName<SchemaType>() };
}
else
{
a.Type = new List<SchemaType>(n.CreateSimpleList(n2 => n2.GetScalarValue().GetEnumFromDisplayName<SchemaType>()));
}
}
},
{
"required",
Expand Down
26 changes: 13 additions & 13 deletions src/LEGO.AsyncAPI.Readers/ParseNodes/AsyncApiAnyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
return new AsyncApiDateTime(dateTimeValue);
}
}
else if (type == "string")
else if (type.Contains(SchemaType.String))
{
if (format == "byte")
{
Expand Down Expand Up @@ -143,55 +143,55 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
}
else
{
if (type == "integer" && format == "int32")
if (type.Contains(SchemaType.Integer) && format == "int32")
{
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new AsyncApiInteger(intValue);
}
}

if (type == "integer" && format == "int64")
if (type.Contains(SchemaType.Integer) && format == "int64")
{
if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longValue))
{
return new AsyncApiLong(longValue);
}
}

if (type == "integer")
if (type.Contains(SchemaType.Integer))
{
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
{
return new AsyncApiInteger(intValue);
}
}

if (type == "number" && format == "float")
if (type.Contains(SchemaType.Number) && format == "float")
{
if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var floatValue))
{
return new AsyncApiFloat(floatValue);
}
}

if (type == "number" && format == "double")
if (type.Contains(SchemaType.Number) && format == "double")
{
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return new AsyncApiDouble(doubleValue);
}
}

if (type == "number")
if (type.Contains(SchemaType.Number))
{
if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out var doubleValue))
{
return new AsyncApiDouble(doubleValue);
}
}

if (type == "string" && format == "byte")
if (type.Contains(SchemaType.String) && format == "byte")
{
try
{
Expand All @@ -202,7 +202,7 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
}

// binary
if (type == "string" && format == "binary")
if (type.Contains(SchemaType.String) && format == "binary")
{
try
{
Expand All @@ -212,28 +212,28 @@ public static IAsyncApiAny GetSpecificAsyncApiAny(IAsyncApiAny asyncApiAny, Asyn
{ }
}

if (type == "string" && format == "date")
if (type.Contains(SchemaType.String) && format == "date")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
{
return new AsyncApiDate(dateValue.Date);
}
}

if (type == "string" && format == "date-time")
if (type.Contains(SchemaType.String) && format == "date-time")
{
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
{
return new AsyncApiDateTime(dateTimeValue);
}
}

if (type == "string")
if (type.Contains(SchemaType.String))
{
return asyncApiAny;
}

if (type == "boolean")
if (type.Contains(SchemaType.Boolean))
{
if (bool.TryParse(value, out var booleanValue))
{
Expand Down
78 changes: 44 additions & 34 deletions src/LEGO.AsyncAPI/Models/AsyncApiSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// type
writer.WriteProperty(AsyncApiConstants.Type, this.Type);
if (this.Type != null)
{
if (this.Type.Count == 1)
{
writer.WriteProperty(AsyncApiConstants.Type, this.Type.First().GetDisplayName());
}
else
{
writer.WriteOptionalCollection(AsyncApiConstants.Type, this.Type.Select(t => t.GetDisplayName()), (w, s) => w.WriteValue(s));
}
}

// allOf
writer.WriteOptionalCollection(AsyncApiConstants.AllOf, this.AllOf, (w, s) => s.SerializeV2(w));
Expand Down
Loading