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

Ability to globally reject strings containing time information for LocalDate #3223

Open
mjustin opened this issue Jul 26, 2021 · 2 comments
Open
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@mjustin
Copy link

mjustin commented Jul 26, 2021

Is your feature request related to a problem? Please describe.

I recently ran across a bug in code I was working on that was due to the client code passing across a date/time value for a different time zone than the server into a LocalDate property. Due to this, the date portion of the string was a different date than was intended. This would have been prevented had the backend code rejected the date/time value, and only accepted a value in yyyy-MM-dd format. I can configure that for each individual property using @JsonFormat(pattern = "yyyy-MM-dd"), but I would rather be able to configure something like this globally.

Describe the solution you'd like
I would like a feature that could be configured on ObjectMapper that causes a date with temporal information to be rejected when deserializing to a LocalDate. Two ways I could see this being done are by globally specifying the exact format to use (yyyy-MM-dd), or by having some sort of global flag telling Jackson to be strict about date formats that contain extra temporal information.

Here are some related Stack Overflow questions regarding this same situation:

  1. https://stackoverflow.com/q/68513802
  2. https://stackoverflow.com/q/57367013
@mjustin mjustin added the to-evaluate Issue that has been received but not yet evaluated label Jul 26, 2021
@kupci
Copy link
Member

kupci commented Jul 27, 2021

"or by having some sort of global flag telling Jackson to be strict about date formats that contain extra temporal information."

I believe this was recently fixed, in 2.13, see Java 8 issue 212:

Currently LocalDateDeserializer automatically accepts non-standard format where time part also exists.
While this is useful for some use cases, compatibility, it seems reasonable that if user forces "strict" handling (via @jsonformat, or global default), that part would not be accepted.

FasterXML/jackson-modules-java8#212

@mjustin
Copy link
Author

mjustin commented Aug 9, 2021

For those who (like me) had difficulty figuring out how to specify the globally default leniency, the ObjectMapper.setDefaultLeniency() and MapperBuilder.defaultLeniency() methods will do it:

ObjectMapper objectMapper = new ObjectMapper().setDefaultLeniency(false);

or

JsonMapper objectMapper = JsonMapper.builder().defaultLeniency(true).build();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants