-
-
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
Differentiate between input and code exceptions on deserialization #1356
Comments
I am open to adding an intermediate (or maybe even leaf level) Anyway: I do like the idea of adding a small number (one or two) new types of (more) semantic exceptions, instead of overloading small number of existing exceptions, because existing ones have some definition (except for loose
From this list I think there could be one other actual semantic exception: invalid-coercion.
Then again, invalid coercion could also just be mapped to On timing/versioning; new types will have to go in 2.9, so we can plan on that. I am still open to smaller incremental improvements; mostly cases where existing input-related problems are NOT mapped as As to |
I agree with everything stated, and Based on your description of
as The issue of coercion, like you pointed out is a subtle one, but I feel like there is a distinction between "a coercion exists, but failed ( Don't worry about how to version everything. Do what you gotta do and we can discuss what version fits with dropwizard for the time being. |
On naming, one suggestion: instead of
On programmatic... hmmh. Bit torn on whether it should start with
|
Looks nice, though some may argue Pojo to be misleading due to all the ways Jackson can serialize/deserialize. Maybe even drop it for
Yeah, I can get behind
|
Right, "pojo" and "bean" are both sub-optimal. Could just start with base exception of So I think:
would be the way to go then. |
FWIW, starting to add convenience methods in |
Working on this. One unfortunate finding: there is already |
Right, donning my naming cap:
|
Very creative naming suggestions! I'll go, for now, with |
Good to hear, let me know when/if you want me to try anything out on Dropwizard. |
Ok, so, after all tests pass, here's the first draft. I didn't change the name (another option: There are some remaining cases where I'm not sure what to do -- specifically, catch-wrap-rethrow for exceptions from user code (Creators, setters, getters) -- but first things first. |
I wanted to do a quick test with dropwizard, and I installed snapshots of 2.9 for annotations and core, but databind is failing. Looks like the pom references a parent pom, is this correct? Because this repo doesn't have a parent pom. |
@nickbabcock you need to built that locally, although I think I can do a snapshot deploy for convenience |
Oof, didn't see Jackson-parent repo 😊 I should be all set |
Ok tried out 2.9 on dropwizard and the following line was almost a 100% success final boolean return500code = cause instanceof InvalidDefinitionException; The one case that failed:
Which is when a user submits json of false when expected json looks like {
"message": 1,
"date": "2012-01-01"
} The lack of a boolean constructor is causing a |
Definitely there are cases where type is not explicitly chosen, and code probably relies on throw-catch-rethrow. I'll hunt down this one, but it would be useful to add stack trace to simplify the task, for cases you encounter. |
Hmmh. I can not reproduce this issue at this point. I'll do a new snapshot deploy to make sure latest one is out. |
I cloned and installed all the necessary repos locally (parent, core, annotations, databind). Given @Test
public void runTest() throws IOException {
final ObjectReader reader = new ObjectMapper().readerFor(Tester.class);
reader.readValue("false");
}
public class Tester {
private Integer message;
@JsonProperty
public Integer getMessage() {
return message;
}
@JsonProperty
public void setMessage(Integer message) {
this.message = message;
}
} Results in stacktrace:
|
Ah! My mistake -- I didn't think this through, and assumed |
Fixed via #1404. One other thought: although I do prefer not to add "json" any more, since it's but one of input formats; it would be possible to use |
I'm +1 for preferring prefixing exceptions with Maybe some future work for Jackson 3/4 😉 |
Renaming of other exceptions would be for later versions for sure. But in this case we still have full control of the new names, before 2.9.0 is released. I am also ok with naming discrepancy; |
Summary of dropwizard discussion, specifically the following comment:
When a server receives a payload, attempts to deserialize it, and an exception is raised, there are two possibilities in our eyes:
The issue is that both of these situations may throw the same root exception
JsonMappingException
, which makes it hard to determine what response should be returned. I propose a new exception:JsonInputMappingException
(name subject to change) or increase usage ofInvalidFormatException
Examples
Examples that use just a plain
JsonMappingException
where newJsonInputMappingException
orInvalidFormatException
would be usedThe Dream
The code needed to determine if it client or server issue:
or maybe even:
and have
InvalidFormatException
andPropertyBindingException
extend fromJsonInputMappingException
, though I do not know if this would be 100% applicable.Considerations
By creating a new exception that is supposed to represent bad input, exceptions thrown due to bad input in third party libraries that use plain
JsonMappingException
, if not changed, can be unexpectedly interpreted as a server/programmatic error. I believe it is best to err on the side of blaming the client/input. Instead, creating aJsonProgrammaticMappingException
may fix this problem.Would
JsonParseException
be a part ofJsonInputMappingException
The suggested exception,
JsonInputMappingException
, does not apply to serialization. An error on serialization is almost certainly a programmatic error, though I can see a situation where a custom serializer throws on certain input. I understand this to be less of a use case.Do not create a new exception
JsonInputMappingException
orJsonProgrammaticMappingException
, but instead throw instances ofInvalidFormatException
for the given examples (more examples may exist, but I'm not aware of them at this point).Conclusion
Let me know if you need help recreating any examples. I'm torn whether it's best to create a new exception or use
InvalidFormatException
more.The text was updated successfully, but these errors were encountered: