Skip to content

Commit

Permalink
Moved check on Options.ValidateFeatures below ThrowIf methods to prev…
Browse files Browse the repository at this point in the history
…ent NullReferenceException (#1800)

* Moved check on Options.ValidateFeatures below ThrowIf methods to prevent NullReferenceException

* Add Unit Test

* Update ReleaseNotes.md

---------

Co-authored-by: Christian <[email protected]>
  • Loading branch information
ramonsmits and chkr1011 authored Aug 13, 2023
1 parent 1018986 commit b1dc860
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
* [Client] Fixed _PlatformNotSupportedException_ when using Blazor (#1755, thanks to @Nickztar).
* [Client] Fixed _NullReferenceException_ when performing several actions when not connected (#1800, thanks to @ramonsmits).
* [Server] Fixed _NullReferenceException_ in retained messages management (#1762, thanks to @logicaloud).
* [Server] Exposed new option which allows disabling packet fragmentation (#1753).
* [Server] Expired sessions will no longer be used when a client connects (#1756).
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,27 @@ public async Task Return_Non_Success()
Assert.AreEqual(response.UserProperties[0].Value, "Value");
}
}

[TestMethod]
public async Task Throw_Proper_Exception_When_Not_Connected()
{
try
{
var mqttFactory = new MqttFactory();
using (var mqttClient = mqttFactory.CreateMqttClient())
{
await mqttClient.SubscribeAsync("test", MqttQualityOfServiceLevel.AtLeastOnce);
}
}
catch (MqttCommunicationException exception)
{
if (exception.Message == "The client is not connected.")
{
return;
}
}

Assert.Fail();
}
}
}
6 changes: 3 additions & 3 deletions Source/MQTTnet/Client/MqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,14 @@ public async Task<MqttClientSubscribeResult> SubscribeAsync(MqttClientSubscribeO
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
}

ThrowIfDisposed();
ThrowIfNotConnected();

if (Options.ValidateFeatures)
{
MqttClientSubscribeOptionsValidator.ThrowIfNotSupported(options, _adapter.PacketFormatterAdapter.ProtocolVersion);
}

ThrowIfDisposed();
ThrowIfNotConnected();

var subscribePacket = MqttPacketFactories.Subscribe.Create(options);
subscribePacket.PacketIdentifier = _packetIdentifierProvider.GetNextPacketIdentifier();

Expand Down

0 comments on commit b1dc860

Please sign in to comment.