Skip to content

Commit

Permalink
fix: async schema deserializer "additionalProperties" not deserializi…
Browse files Browse the repository at this point in the history
…ng JsonSchema correctly (#120)
  • Loading branch information
UlrikSandberg authored Jul 11, 2023
1 parent 9e4867f commit 3761f52
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/LEGO.AsyncAPI.Readers/V2/AsyncApiSchemaDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class JsonSchemaDeserializer
{
"additionalProperties", (a, n) =>
{
if (n.GetBooleanValueOrDefault(null) == false)
if (n is ValueNode && n.GetBooleanValueOrDefault(null) == false)
{
a.AdditionalProperties = new NoAdditionalProperties();
}
Expand Down Expand Up @@ -171,6 +171,9 @@ public class JsonSchemaDeserializer
{
"deprecated", (a, n) => { a.Deprecated = bool.Parse(n.GetScalarValue()); }
},
{
"nullable", (a, n) => { a.Nullable = n.GetBooleanValue(); }
},
};

private static readonly PatternFieldMap<AsyncApiSchema> schemaPatternFields =
Expand Down
84 changes: 84 additions & 0 deletions test/LEGO.AsyncAPI.Tests/Models/AsyncApiSchema_Should.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright (c) The LEGO Group. All rights reserved.

using System.Linq;
using LEGO.AsyncAPI.Bindings;
using LEGO.AsyncAPI.Readers;

namespace LEGO.AsyncAPI.Tests.Models
{
using System;
Expand Down Expand Up @@ -89,6 +93,16 @@ public class AsyncApiSchema_Should
MinLength = 2,
},
},
AdditionalProperties = new AsyncApiSchema
{
Properties = new Dictionary<string, AsyncApiSchema>
{
["Property8"] = new AsyncApiSchema
{
Type = SchemaType.String | SchemaType.Null,
},
},
},
},
},
Nullable = true,
Expand Down Expand Up @@ -393,6 +407,16 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
""type"": ""string"",
""minLength"": 2
}
},
""additionalProperties"": {
""properties"": {
""Property8"": {
""type"": [
""null"",
""string""
]
}
}
}
}
},
Expand All @@ -411,6 +435,66 @@ public void SerializeAsJson_WithAdvancedSchemaObject_V2Works()
actual.Should().Be(expected);
}

[Test]
public void Deserialize_WithAdditionalProperties_Works()
{
// Arrange
var json = @"{
""title"": ""title1"",
""properties"": {
""property1"": {
""properties"": {
""property2"": {
""type"": ""integer""
},
""property3"": {
""type"": ""string"",
""maxLength"": 15
}
},
""additionalProperties"": false
},
""property4"": {
""properties"": {
""property5"": {
""properties"": {
""property6"": {
""type"": ""boolean""
}
}
},
""property7"": {
""type"": ""string"",
""minLength"": 2
}
},
""additionalProperties"": {
""properties"": {
""Property8"": {
""type"": [
""null"",
""string""
]
}
}
}
}
},
""nullable"": true,
""externalDocs"": {
""url"": ""http://example.com/externalDocs""
}
}";
var expected = AdvancedSchemaObject;

// Act
var actual = new AsyncApiStringReader().ReadFragment<AsyncApiSchema>(json, AsyncApiVersion.AsyncApi2_0, out var _diagnostics);

// Assert
actual.Should().BeEquivalentTo(expected);
_diagnostics.Errors.Should().BeEmpty();
}

[Test]
public void SerializeAsJson_WithAdvancedSchemaWithAllOf_V2Works()
{
Expand Down

0 comments on commit 3761f52

Please sign in to comment.