Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to remove non-nullable converter if duplicate found for nullable targetType #1343

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

julek-kal
Copy link

Connection with issue(s)

Close #1339

Solution description

When targetType is nullable and the class is annotated with both converters for nullable and non-nullable type, _compatibleMatch returns two of them. So I've added a check if targetType is nullable and matchingAnnotations length is equal to 2, if so I proceed to try to find a non-null converter and remove it from the list of matchedConverters.

To Do

  • Check the original issue to confirm it is fully satisfied
  • Add solution description to help guide reviewers
  • Add unit test to verify new or fixed behaviour
  • If apply, add documentation to code properties and package readme

@google-cla
Copy link

google-cla bot commented Aug 6, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@julek-kal julek-kal changed the title select appropriate converter for nullable fieldtype Try to remove non-nullable converter if duplicate found for nullable targetType Aug 6, 2023
@@ -202,6 +202,14 @@ _JsonConvertData? _typeConverterFrom(
return null;
}

if (targetType.isNullableType && matchingAnnotations.length == 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if both annotations match? What if there are more than two?

I see how this would be nice for the case you're hitting – but I worry that it has too many edge cases.

Would it be better to find the set of "better" converters in the set – and see if that set of "better" converters has a length of 1?

Copy link
Author

Choose a reason for hiding this comment

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

I think that is behaving better than currently and don't break other things.
It's exactly trying to find a "better" converter by its field type. When there are more than two converters or both matches it would throw "Found more than one matching converter for $targetTypeCode."

It's fixing 1339, so I'm able to have a JsonSerializable with converters and use it on a class that contains both nullable and non-nullable of the same type.

What other "algorithm" do you suggest to find better converters? What is the factor of "betterness" of the converter? Maybe you want to suggest to check also a jsonType. Ok, maybe it's a good idea, but I think that the level of complexity will be increased noticeably

Copy link

@t-beckmann t-beckmann Aug 24, 2023

Choose a reason for hiding this comment

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

Maybe implement it as

Suggested change
if (targetType.isNullableType && matchingAnnotations.length == 2) {
if (targetType.isNullableType && matchingAnnotations.length > 1) {
matchingAnnotations
.removeWhere((element) => !element.fieldType.isNullableType);

so that it is not restricted to exactly 2 matches.

Afterwards you could favor an exact type match if the result is not unique yet. Finally, instead of throwing one could pick the first match, assuming the results are in order of declaration.

Copy link
Author

Choose a reason for hiding this comment

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

@t-beckmann thanks for your suggestion. @kevmoo what do you think about above solution?

Copy link

Choose a reason for hiding this comment

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

any update for this PR?

Copy link
Author

Choose a reason for hiding this comment

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

I'm still waiting for a response from @kevmoo with his opinion about t-beckmann's implementation and his look at the above topic. Cause I think that he is a decisional person, about that PR.

Copy link

Choose a reason for hiding this comment

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

For whatever reason, the current workaround is to define converters both as annotations and as list members, however, it must be done separately for nullable and non-nullable types, like so:

@JsonSerializable(
  converters: [
    ExampleSerializationHandlerString(),
    ExampleSerializationHandlerBool(),
    ExampleSerializationHandlerInt(),
    ExampleSerializationHandlerDouble(),
    ExampleSerializationHandlerMap(),
  ],
)
@ExampleSerializationHandlerStringNullable()
@ExampleSerializationHandlerBoolNullable()
@ExampleSerializationHandlerIntNullable()
@ExampleSerializationHandlerDoubleNullable()
@ExampleSerializationHandlerMapNullable()

This is with the latest json_serializable (6.8.0) and json_annotation (4.9.0) versions at the time of this writing.

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.

Found more than one matching converter for int?.
5 participants