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
12 changes: 9 additions & 3 deletions src/ByteBard.AsyncAPI.Bindings/MQTT/LastWill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public class LastWill : IAsyncApiElement
{
private bool? retain;

/// <summary>
/// The topic where the Last Will and Testament message will be sent.
/// </summary>
Expand All @@ -26,7 +28,11 @@ public class LastWill : IAsyncApiElement
/// <summary>
/// Whether the broker should retain the Last Will and Testament message or not.
/// </summary>
public bool Retain { get; set; }
public bool Retain
{
get => this.retain ?? false;
set => this.retain = value;
}

public void Serialize(IAsyncApiWriter writer)
{
Expand All @@ -36,10 +42,10 @@ public void Serialize(IAsyncApiWriter writer)
}

writer.WriteStartObject();
writer.WriteRequiredProperty("topic", this.Topic);
writer.WriteOptionalProperty("topic", this.Topic);
writer.WriteOptionalProperty("qos", (int?)this.QoS);
writer.WriteOptionalProperty("message", this.Message);
writer.WriteRequiredProperty("retain", this.Retain);
writer.WriteOptionalProperty("retain", this.retain);
writer.WriteEndObject();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,35 @@ public void OperationBinding_OmitsUnsetQosAndRetain()
var yaml = new MQTTOperationBinding().SerializeAsYaml(AsyncApiVersion.AsyncApi3_0);
yaml.Should().NotContain("qos:").And.NotContain("retain:");
}

[TestCase(AsyncApiVersion.AsyncApi2_0)]
[TestCase(AsyncApiVersion.AsyncApi3_0)]
public void LastWill_WithoutTopicAndRetain_RoundTripsWithoutAddingThem(AsyncApiVersion version)
{
var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection.MQTT };
var yaml = (version == AsyncApiVersion.AsyncApi2_0 ? "url" : "host") + ": localhost\n" +
"""
protocol: mqtt
bindings:
mqtt:
lastWill:
qos: 1
""";

var server = new AsyncApiStringReader(settings).ReadFragment<AsyncApiServer>(yaml, version, out var diagnostics);
diagnostics.Errors.Should().BeEmpty();

var actual = server.SerializeAsYaml(version);
actual.Should().Contain("qos: 1").And.NotContain("topic:").And.NotContain("retain:");
}

[Test]
public void LastWill_WithExplicitRetainFalse_WritesRetain()
{
var lastWill = new LastWill { Topic = "/last-wills", Retain = false };
var server = new MQTTServerBinding { LastWill = lastWill };

server.SerializeAsYaml(AsyncApiVersion.AsyncApi3_0).Should().Contain("topic: /last-wills").And.Contain("retain: false");
}
}
}
Loading