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

[BUG] @Getter and @Setter don't follow JavaBean naming convention #2650

Closed
gbaso opened this issue Nov 18, 2020 · 7 comments
Closed

[BUG] @Getter and @Setter don't follow JavaBean naming convention #2650

gbaso opened this issue Nov 18, 2020 · 7 comments

Comments

@gbaso
Copy link

gbaso commented Nov 18, 2020

Suppose I have a field named oDo. For this field, lombok generates methods getODo and setODo. I didn't question it at first, because it felt the natural capitalization, however it's not actually compliant with JavaBean naming convention.

According to 8.8: Capitalization of inferred names

to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone.

the getter getODo actually corresponds to a field name ODo. It follows that the correct getter name for oDo is getoDo. Indeed, this is the getter name generated by Eclipse, VS Code and I suppose other IDEs.

More importantly, it's the name used by Jackson to serialize fields, therefore a class like:

@Value
@Builder
@Jacksonized
class Foo {
	@JsonProperty("do")
	private String oDo;
}

results in a duplicate serialization:

{
	"do": "<value>",
	"odo": "<value>"
}

Interestingly, @Data doesn't suffer from this problem because @JsonProperty is copied onto the setter, making Jackson switch to that getter/setter pair for serialization/deserialization.

See also: https://stackoverflow.com/questions/26224938/getters-setters-when-first-word-has-second-letter-capitalized

Using jackson 1.18.16

@randakar
Copy link

randakar commented Nov 18, 2020 via email

@janrieke
Copy link
Contributor

janrieke commented Nov 18, 2020

In #1861, @rspilker said they'd accept a PR changing this behavior. I don't know if that changed in the meantime...

@gbaso
Copy link
Author

gbaso commented Nov 18, 2020

Well, that's unfortunate. As a workaround, maybe @Jacksonized could also copy @JsonProperty onto the getter?

@janrieke
Copy link
Contributor

If there are only a few fields affected, you can do this manually by adding @Getter(onMethod_=@JsonProperty("do")) to the field.

@gbaso
Copy link
Author

gbaso commented Nov 19, 2020

That would work, but @Value @Builder @Jacksonized is a common combination, it would be nice if it worked out-of-the-box for this corner case. Which, according to the number of times it came up, is arguably common as well.

@rzwitserloot
Copy link
Collaborator

duplicate of #1661. PRs accepted, but make a plan on how to get from here to there and explain it to us first or we may have to reject it.

@rzwitserloot
Copy link
Collaborator

Duplicate of #2693

@rzwitserloot rzwitserloot marked this as a duplicate of #2693 Jan 2, 2021
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

No branches or pull requests

4 participants