shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
51 stars 29 forks source link

Duplicate add of user properties on invocation of addUserProperty from stream-created auth message #30

Closed sethloco closed 2 years ago

sethloco commented 2 years ago

Hello,

First, great projects and thank you for all the work you do. It's much appreciated.

I think perhaps this assignment of _userProperties was intended to be with a copy of _propertySet.userProperties? (BTW, can this assignment be moved out of the for loop?) https://github.com/shamblett/mqtt5_client/blob/95f8e4a688bb5382e03acb611a011c1ba4d4a53e/lib/src/messages/authenticate/mqtt_authenticate_variable_header.dart#L117

Otherwise, it looks like it will add the property twice when called from here -- but only if creating the message from a byte buffer https://github.com/shamblett/mqtt5_client/blob/95f8e4a688bb5382e03acb611a011c1ba4d4a53e/lib/src/messages/authenticate/mqtt_authenticate_message.dart#L116

The following is a test showing the issue

Future<int> main() async {
  final user1 = MqttUserProperty();
  user1.pairName = 'User 1 name';
  user1.pairValue = 'User 1 value';
  final user2 = MqttUserProperty();
  user2.pairName = 'User 2 name';
  user2.pairValue = 'User 2 value';

  final message = MqttAuthenticateMessage()
      .withReasonCode(MqttAuthenticateReasonCode.success)
      .withAuthenticationMethod('method')
      .withAuthenticationData(typed.Uint8Buffer()..addAll([1, 2, 3, 4]))
      .withUserProperties([user1])
      .withReasonString('Reason String');

  final buffer = typed.Uint8Buffer();
  final stream = MqttByteBuffer(buffer);
  message.writeTo(stream);
  stream.reset();
  final outputHeader = MqttHeader.fromByteBuffer(stream);
  final message2 = MqttAuthenticateMessage.fromByteBuffer(outputHeader, stream);

  print(message.userProperties.length);  // 1
  print(message2.userProperties.length); // 1

  message.addUserProperty(user2);
  message2.addUserProperty(user2);

  print(message.userProperties.length);  // 2
  print(message2.userProperties.length); // 3 <should be 2>

  return 0;
}
shamblett commented 2 years ago

Ok thanks I'll have look at this.

shamblett commented 2 years ago

Fixed by #32, will be in the next release.

shamblett commented 2 years ago

Version 3.3.0 now released.

sethloco commented 2 years ago

Many thanks!

sethloco commented 2 years ago

Verified..thank you