-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
JsonMappingException: Conflicting getter / setter definitions when using property names that start with set, get or is #2729
Comments
…capitalization
As per my comment on patch, I am not convinced that premises here are correct: and that it is likely that there is significant usage for such naming. I am not saying using such naming is a good idea (capitalization oddities will cause problems wrt matching to fields, probably), but I would not just go and significantly change name mangling. |
Ah. I missed one part: note on Scala module. This can complicate things a lot... I do not think pure Java reproduction of class would have these issues (since method like But I will try to reproduce this with just |
I can not reproduce conflicting setters/getters part with But there seems to be the other aspect: that of whether getter/setter detection should require that the character following "get", "set" or "is" marker should be capitalized or not. As behavior of Jackson since beginning has been not to require this, alternate behavior would need to be opt-in: this could be done by adding something like Wrt this second aspect it would be good to find definitions that require such capitalization limits (Bean naming spec, maybe?) to refer to. |
In order not to comment on both the pull request and the issue at the same time I'll copy my comment from the pull request over here once more: Sure, see the specification of JavaBeans chapter 8.3 (page 55ff):
If you decide not to follow this convention how else would you differentiate a setter from a property name that simply starts with "set"? |
Since capitalization in my view is a standard for getters and setters I would much rather see an opt-out toggle rather than an opt-in toggle. Would you accept the pull request if I were to add such a MapperFeature? Moving the issue to |
To maybe give you a bit more background: we are using Spring Boot and generate our DTOs using swagger-codegen, which I would consider a pretty standard setup. |
…capitalization
…capitalization
I added a second pull request that contains a new feature |
@janeisklar Thank you for PR! I feel quite strongly about not enabling this by default for But I think Scala-module could change this feature by default on registration (even in 2.x) if that makes sense. Still... I am wondering if there is an issue with logic Scala module uses on detecting properties.
should be auto-detected by default, to match with It could then be that there are 2 issues: one is (to me) minor deviation from Bean naming convention. |
After writing all of above, I think I will have to think a bit more about this. There is an existing feature, I will send a note on user mailing list. Such change would have to go in 2.12(.0), being opt-out change essentially. |
At this point, I am not certain I will want to proceed with this since there has not been any recent change to name handling for years (unless I miss something), so it seems more likely something has changed either in Scala module, or some interaction between the two. So instead of trying to make a potentially risky change it would be necessary to first check what might have triggered the problem. @pjfanning any guesses as to what might be going on here? I wonder if I should move this to |
I do not see any Scala code so far - and the Java code would not map to any Scala coding norms that I am aware of. If there is a reproducible scala test case, I'll look at it. |
@pjfanning while original code is in Java, it would not fail the way described without Scala module: I think Scala module applies rules to connect methods like Another thing that might help would be to know fixes that went in 2.10 -- I know release notes have sometimes been incomplete for Scala module (and/or I haven't know where to look?) -- so https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.10 only lists one fix which does not seem related. But maybe some other fix could be related. |
@cowtowncoder https://github.com/pjfanning/jackson-databind/tree/issue-2729 and https://github.com/pjfanning/jackson-databind/tree/issue-2729-2.10 show the issue exists in jackson-databind 2.11 and 2.10 respectively - neither case has the jackson-scala-module enabled the same test fails on the 2.9 branch too |
@pjfanning I think there is a bit of (understandable) confusion here. So, the problem test reports wrt there: public String getaways() { throw new Error("Should not get called"); }
public Boolean island() { throw new Error("Should not get called"); } is something separate from what title suggest, conflicting getters. At this point, and for past 12+ years or so, these 2 methods have been and are considered valid getters to infer property. This may not be Bean Naming compliant, although I don't know if other Java tools would exclude them. But what was originally claimed, as I understand it, was not just that "hey do not recognize these methods as accessors", but also that there would be conflict between (I thought): private Boolean island;
public Boolean island() { return island; }
public Boolean isIsland() { return island; } (and various combinations) Regular databinding derives property If this is not the case, and I just misunderstood part of "conflicting getters" -- conflict meaning there are multiple ones causing exception -- and what is meant is "using wrong method", then that's my bad. @janeisklar Do you have the original exception stack trace available? That would help validate the 2nd issue (conflicting methods); compared to 1st issue which is clear (that of Bean Naming incompatibility). |
To hopefully make everything a bit clearer I added a minimal project that reproduces the issue within the setup we are using. When running the project with
The relevant exception is this one:
If I changed the pull request such that this becomes an optional flag that is deactivated by default, would that be an acceptable approach to move this forward? |
A fix could take a while - a potential workaround (other than using the jackson 2.9) is to use separate ObjectMapper instances for scala and java classes. You don't need to register the scalamodule if you are working with plain java classes. |
We are currently using a custom patch build based on my pull request. That's not ideal, but takes off the pressure for the moment. The internal framework we are using is also using scala and it's a bit hard to use a different ObjectMapper as it is used by Spring Boot implicitly. Nevertheless, let me know if I can help. |
Thank you for the update @janeisklar and apologies for slow follow up. I think the PR suggested makes sense, and the only hold up I have has to do with thoughts on Also thank you for adding stack trace: it does indeed look like |
One new (for 2.12) feature that could be helpful is this one: #2800 wherein a new pluggable handle ( Strategy implementation is initialized on per-type basis so that it should also be possible to inspect the whole class first, and thereby consider combination of field+method and other per-class aspects. This could open door both for per-module (Scala, Kotlin) default alternatives and, perhaps, reusable alternate implementations. |
FasterXML/jackson-module-scala#455 might help too |
@pjfanning ah cool. Yes. |
One further thought: I can quite easily make default
Configuration would be done with something like:
and would apply to all accessors (setter/with-method, getter/is-getter). |
Implemented: configuration would be done by using: ObjectMapper mapper = JsonMapper.builder()
.accessorNaming(new DefaultAccessorNamingStrategy.Provider()
// accept lower-case first? no - accept non-letter first? no:
.withFirstCharAcceptance(false, false))
.build(); but it is possible to define other validation rules as well. I will file a follow-up request to change default handling for 3.0, to change accepted naming as suggested here. |
Created #2882 |
I started to experience the following issue starting from 2.10 in combination with the jackson scala module.
Consider a POJO like this:
When serializing, one will receive a
JsonMappingException
because bothgetaways()
andgetGetaways()
, as well asisland()
andisIsland()
are considered conflicting getters with the same priority.When deserializing, both
settlementDate(String)
andsetSettlementDate(String)
will be conflicting setters with the same priority.Case-sensitivity should be respected when determining the priority of setters and getters, i.e.
settlementDate()
should not be the same assetTlementDate()
.Also see
_setterPriority()
inPOJOPropertyBuilder
(as well as_getterPriority()
):I'll submit a pull request to have this fixed later today.
The text was updated successfully, but these errors were encountered: