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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void SerializeProperties(IAsyncApiWriter writer, AsyncApiVersion version
protected override FixedFieldMap<MQTTMessageBinding> FixedFieldMap => new()
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "payloadFormatIndicator", (a, n) => { a.PayloadFormatIndicator = n.GetIntegerValueOrDefault(); } },
{ "payloadFormatIndicator", (a, n) => { a.PayloadFormatIndicator = n.GetIntegerValue(); } },
{ "correlationData", (a, n) => { a.CorrelationData = AsyncApiJsonSchemaDeserializer.LoadSchema(n); } },
{ "contentType", (a, n) => { a.ContentType = n.GetScalarValue(); } },
{ "responseTopic", (a, n) => { a.ResponseTopicValue = MQTTStringOrSchema.Parse(n); } },
Expand Down
6 changes: 3 additions & 3 deletions src/ByteBard.AsyncAPI.Bindings/MQTT/MQTTServerBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ public int? MaximumPacketSize
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "clientId", (a, n) => { a.ClientId = n.GetScalarValue(); } },
{ "cleanSession", (a, n) => { a.CleanSession = n.GetBooleanValueOrDefault(); } },
{ "cleanSession", (a, n) => { a.CleanSession = n.GetBooleanValue(); } },
{ "lastWill", (a, n) => { a.LastWill = n.ParseMap(LastWillFixedFields); } },
{ "keepAlive", (a, n) => { a.KeepAlive = n.GetIntegerValueOrDefault(); } },
{ "keepAlive", (a, n) => { a.KeepAlive = n.GetIntegerValue(); } },
{ "sessionExpiryInterval", (a, n) => { a.SessionExpiryIntervalValue = MQTTIntegerOrSchema.Parse(n); } },
{ "maximumPacketSize", (a, n) => { a.MaximumPacketSizeValue = MQTTIntegerOrSchema.Parse(n); } },
};

private static FixedFieldMap<LastWill> LastWillFixedFields = new()
{
{ "topic", (a, n) => { a.Topic = n.GetScalarValue(); } },
{ "qos", (a, n) => { a.QoS = (uint?)n.GetIntegerValueOrDefault(); } },
{ "qos", (a, n) => { a.QoS = (uint?)n.GetIntegerValue(); } },
{ "message", (a, n) => { a.Message = n.GetScalarValue(); } },
{ "retain", (a, n) => { a.Retain = n.GetBooleanValue(); } },
};
Expand Down
6 changes: 3 additions & 3 deletions src/ByteBard.AsyncAPI.Bindings/ROS2/ROS2QosPolicies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class ROS2QosPolicies : IAsyncApiElement
{ "reliability", (a, n) => { a.Reliability = n.GetScalarValue(); } },
{ "history", (a, n) => { a.History = n.GetScalarValue(); } },
{ "durability", (a, n) => { a.Durability = n.GetScalarValue(); } },
{ "lifespan", (a, n) => { a.Lifespan = n.GetIntegerValueOrDefault(); } },
{ "deadline", (a, n) => { a.Deadline = n.GetIntegerValueOrDefault(); } },
{ "lifespan", (a, n) => { a.Lifespan = n.GetIntegerValue(); } },
{ "deadline", (a, n) => { a.Deadline = n.GetIntegerValue(); } },
{ "liveliness", (a, n) => { a.Liveliness = n.GetScalarValue(); } },
{ "leaseDuration", (a, n) => { a.LeaseDuration = n.GetIntegerValueOrDefault(); } },
{ "leaseDuration", (a, n) => { a.LeaseDuration = n.GetIntegerValue(); } },
};

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ByteBard.AsyncAPI.Bindings/ROS2/ROS2ServerBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ROS2ServerBinding : ServerBinding<ROS2ServerBinding>
protected override FixedFieldMap<ROS2ServerBinding> FixedFieldMap => new()
{
{ "rmwImplementation", (a, n) => { a.RmwImplementation = n.GetScalarValue(); } },
{ "domainId", (a, n) => { a.DomainId = n.GetIntegerValueOrDefault(); } },
{ "domainId", (a, n) => { a.DomainId = n.GetIntegerValue(); } },
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace ByteBard.AsyncAPI.Tests.Bindings
{
using FluentAssertions;
using ByteBard.AsyncAPI.Bindings;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Readers;
using NUnit.Framework;

internal class BindingInvalidScalars_Should : TestBase
{
[TestCase("mqtt", "cleanSession: yes-please", "bool")]
[TestCase("mqtt", "keepAlive: abc", "integer")]
[TestCase("mqtt", "lastWill:\n topic: /wills\n qos: high", "integer")]
[TestCase("ros2", "domainId: abc", "integer")]
public void ServerBinding_WithInvalidScalar_ReportsError(string bindingKey, string field, string expectedType)
{
var yaml =
"host: localhost\n" +
"protocol: " + bindingKey + "\n" +
"bindings:\n" +
" " + bindingKey + ":\n" +
Indent(field, 4);

new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiServer>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics);

diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain($"could not parse to {expectedType}");
}

[Test]
public void MessageBinding_WithInvalidPayloadFormatIndicator_ReportsError()
{
var yaml =
"bindings:\n" +
" mqtt:\n" +
" payloadFormatIndicator: abc\n";

new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiMessage>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics);

diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain("could not parse to integer");
}

[TestCase("lifespan")]
[TestCase("deadline")]
[TestCase("leaseDuration")]
public void ROS2OperationBinding_WithInvalidQosDuration_ReportsError(string field)
{
var yaml =
"action: send\n" +
"channel:\n" +
" $ref: '#/channels/camera'\n" +
"bindings:\n" +
" ros2:\n" +
" qosPolicies:\n" +
" " + field + ": forever\n";

new AsyncApiStringReader(Settings()).ReadFragment<AsyncApiOperation>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics);

diagnostics.Errors.Should().ContainSingle().Which.Message.Should().Contain("could not parse to integer");
}

private static AsyncApiReaderSettings Settings() => new() { Bindings = BindingsCollection.All };

private static string Indent(string value, int spaces)
{
var prefix = new string(' ', spaces);
return prefix + value.Replace("\n", "\n" + prefix) + "\n";
}
}
}
Loading