Skip to content

Conversation

@zawodskoj
Copy link

This PR allows to add multiple discriminator values for single subtype which can cover all of discriminators. This is required for compound discriminators (tuples for example) and allows to write converters like this

JsonSubtypesConverterBuilder
    .Of(typeof(DeviceEvent), nameof(DeviceEvent.Discriminator))
    .RegisterSubtype(typeof(MetalDetectingAlert), (EventType.ALERT, DeviceType.METAL_DETECTOR))
    .RegisterSubtype(typeof(DeviceFailureWarning), (EventType.WARNING, DeviceType.METAL_DETECTOR))
    .RegisterSubtype(typeof(DeviceFailureWarning), (EventType.WARNING, DeviceType.CAMERA))
    .Build()

@manuc66
Copy link
Owner

manuc66 commented Mar 4, 2019

Hi,

Thanks for submitting a PR to this project

Could you please add some unit tests with serialization and deserialization ?

Does it also work with KnownSubTypeAttribute construct ?

And don't worry about the build failure related to SonarQube (it's a configuration problem)

_supportedTypes.Add(type.Value, type.Key);
if (_supportedTypes.ContainsKey(type.Value))
{
if (_addDiscriminatorFirst)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_addDiscriminatorFirst or _serializeDiscriminatorProperty ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be _serializeDiscriminatorProperty

foreach (var type in _subTypeMapping)
{
_supportedTypes.Add(type.Value, type.Key);
if (_supportedTypes.ContainsKey(type.Value))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of check is better suited in JsonSubtypesConverterBuilder (this class is internal)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConverterBuilder contains inverted dictionary, but we can add check for it anyway

@zawodskoj
Copy link
Author

It should work with KnownSubTypeAttribute. Attributed converter does not serialize discriminator and does not require type-to-discriminator mapping

I can add tests later

@manuc66 manuc66 changed the base branch from master to feature/support-for-multiple-discriminators-on-single-type April 28, 2019 14:19
@manuc66 manuc66 merged commit 5f7bd62 into manuc66:feature/support-for-multiple-discriminators-on-single-type Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants