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

Use @JsonProperty for Enum values also when READ_ENUMS_USING_TO_STRING enabled #4036

Merged

Conversation

iProdigy
Copy link
Contributor

When DeserializationFeature.READ_ENUMS_USING_TO_STRING is enabled, the enum deserializer neglects inspecting @JsonProperty; see the test case, it succeeds if READ_ENUMS_USING_TO_STRING is disabled and would fail with READ_ENUMS_USING_TO_STRING enabled (without this patch).

@cowtowncoder
Copy link
Member

cowtowncoder commented Jul 13, 2023

Hmmm. I guess this is a reasonable request as @JsonProperty should override not just Enum.name() provided value but also Enum.toString().

Can not fully validate suggested fix (although it makes sense); @JooHyukKim knows better I think having rewritten most of this code very recently.

@iProdigy iProdigy marked this pull request as ready for review July 13, 2023 23:02
@iProdigy
Copy link
Contributor Author

When @JsonProperty is applied, WRITE_ENUMS_USING_TO_STRING ought to use the overriding string rather than Enum#toString, in my opinion (let me know if you disagree @cowtowncoder) - should I tackle this in another PR or here?

@JooHyukKim
Copy link
Member

I guess this is a reasonable request as @JsonProperty should override not just Enum.name() provided value but also Enum.toString().

Can we use "improve" instead of "fix" in the title, @iProdigy ? Because some (bug) fixes can be merged back to 2.15, it's better not to look back and look for what bug we are trying to fix here. WDYT, @cowtowncoder ?

Can not fully validate suggested fix (although it makes sense); @JooHyukKim knows better I think having rewritten most of this code very recently.

I will take a look, seems straightforward though.

if (name == null) {
name = enumValue.toString();
}
map.put(name, enumValue);
Copy link
Member

Choose a reason for hiding this comment

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

As mentioend in the comment, we may not need to change behavior in this deprecated method

@iProdigy iProdigy changed the title fix(enum): use JsonProperty when READ_ENUMS_USING_TO_STRING feat(enum): use JsonProperty when READ_ENUMS_USING_TO_STRING Jul 14, 2023
@JooHyukKim
Copy link
Member

JooHyukKim commented Jul 14, 2023

I guess this is a reasonable request as @JsonProperty should override not just Enum.name() provided value but also Enum.toString().

It's a straightforward, so I think it work just fine. @cowtowncoder. How about backporting to 2.15?

@cowtowncoder
Copy link
Member

I think that for safety wrt avoiding regression, we should not try backporting improvements to behavior here.

@iProdigy yes I agree that name from @JsonProperty should override one from default/implicit, whether that is name() or toString(). That's the whole point here, right?

@JooHyukKim behavior of deprecated methods is something we could do either way, no strong opinion (that is; can fix, can leave without fix, as long as methods no longer called from within databind itself).

@iProdigy
Copy link
Contributor Author

That's the whole point here, right?

Agreed, shall I update this PR with the serialization side or create a separate PR?

@cowtowncoder
Copy link
Member

@iProdigy Ah! Sorry, missed the ser/deser part. Yeah, I think separate PR might make sense -- but either way is fine with me.

@cowtowncoder cowtowncoder added the cla-needed PR looks good (although may also require code review), but CLA needed from submitter label Jul 14, 2023
@cowtowncoder
Copy link
Member

Ok one last thing @iProdigy -- if you havent been asked for (and sent) CLA, from:

https://github.com/FasterXML/jackson/blob/master/contributor-agreement.pdf

we'd need that before merging the first contribution (once we have it, it's good for any number of contributions).
The usual way is to print, fill & sign, scan/photo, email to info at fasterxml dot com.
Once that's in, I can review and merge PRs. Apologies for this part of process but it's fortunately one-time thing (and if you already sent one just remind me of name/date and I'll check).

@iProdigy
Copy link
Contributor Author

@cowtowncoder Just sent the signed CLA to that inbox!

@JooHyukKim
Copy link
Member

LGTM!

@JooHyukKim
Copy link
Member

@JooHyukKim behavior of deprecated methods is something we could do either way, no strong opinion (that is; can fix, can leave without fix, as long as methods no longer called from within databind itself).

If that's the case, may I suggest "not" to make modifications in deprecated methods? For following reasons...

  1. Compliates going back and forth versions (like 2.15, 2.16, 3.0..). Too much attention spent.
  2. To encourage newer version usage.

WDYT? /cc @cowtowncoder @iProdigy

@iProdigy
Copy link
Contributor Author

It's worth noting that the deprecated EnumResolver.constructUsingToString is still called by:

  • StdKeyDeserializer IF _byToStringResolver is null
  • EnumDeserializer IF _lookupByToString is null

That said, these scenarios shouldn't (?) occur on 2.16, so updating the deprecated method may not be necessary

@cowtowncoder cowtowncoder removed the cla-needed PR looks good (although may also require code review), but CLA needed from submitter label Jul 15, 2023
@cowtowncoder
Copy link
Member

CLA received; will look over once more, then merge.

As to deprecated methods; yeah, need to be careful but at least existing unit test suite can catch some obvious issues.
But test coverage is not great for use of various types as Map keys, especially with "advanced" use cases (like use of annotations) so I guess it's hard choice. Adding handling for something not tested is not significantly better for long term than leaving things as are.

@cowtowncoder cowtowncoder changed the title feat(enum): use JsonProperty when READ_ENUMS_USING_TO_STRING Use @JsonProperty for Enum values also when READ_ENUMS_USING_TO_STRING enabled Jul 15, 2023
@cowtowncoder cowtowncoder added the 2.16 Issues planned for 2.16 label Jul 15, 2023
@cowtowncoder cowtowncoder merged commit 059ccee into FasterXML:2.16 Jul 15, 2023
3 checks passed
@iProdigy iProdigy deleted the fix/enum-deser-jsonproperty-tostring branch July 15, 2023 23:09
@cowtowncoder
Copy link
Member

@JooHyukKim Good points in favor of NOT adding support, I concur.

@JooHyukKim
Copy link
Member

@iProdigy Deleted earlier comment, because I thought we finished something before actually checking 😆. Anyways, thank you also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.16 Issues planned for 2.16
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants