Updating constructor matching for int keys #1277
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The following example works fine for mutable objects.
Unfortunately, it is impossible to repeat the same thing for immutable objects. Serializing the following example throws an exception: MessagePack.MessagePackSerializationException {"Failed to serialize Example value."}. InnerException: MessagePack.Internal.MessagePackDynamicObjectResolverException {"can't find matched constructor. type:Example "}.
The current implementation searches for the matched constructor according to the following logic: for each constructor parameter, there must be int key with value of the index of this parameter. So for immutable types, keys must start with 0 and have no gaps. It becomes more difficult to support changes for such types.
I propose a new implementation without breaking backward compatibility. We determine the indexes of the constructor parameters corresponding to the int keys in ascending order of their values.
This implementation does not break the current serialization logic and supports missing keys in the previous example.