Improve exception used, message, when indicating a `required` property is not set
See original GitHub issueI’m using Jackson 2.9.8.
I want to deserialize a payload to a class with JsonProperty(required = true)
annotated fields.
If the payload does not contain all necessary fields, I want to inform my API user with a concise (possibly custom) message.
However, the Jackson deserialization only gives me an InvalidDefinitionException
(“Cannot construct instance of…”) with the root cause, which depends on the underlying technology. For instance, with the AutoValue framework, the root cause is an IllegalStateException
(“Missing required properties:…”) and with Kotlin data classes, it is an IllegalArgumentException
(“Parameter specified as non-null is null”).
Since all fields are annotated, I guess Jackson could be able to produce something like a MissingFieldException
(with field name, path…). The same way it is able to produce a UnrecognizedFieldException
when an unknown field is met.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
I don’t know
jackson-databind
enough, but my naive thought was that, since it can report any unexpected property (base on@JsonProperty
annotations), it could also report any missing property. We often have objects with a lot of properties, so instead of using the constructor pattern, with the@JsonCreator
annotation, we rather use the builder pattern provided by AutoValue (with@JsonProperty
annotations).Not sure how to proceed here; and while I am +100 on improving error messages, would need more specific information on what is missing, with a test case against Jackson 2.12.
May be re-opened or re-filed with more information; closing for now.