micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.03k stars 1.05k forks source link

@Format and @QueryValue should fail when an invalid LocalDate is sent #6293

Open sbeins55 opened 2 years ago

sbeins55 commented 2 years ago

Expected Behavior

When sending an invalid date string to a controller with @Format the controller should respond with a Bad Request if the QueryValue is not valid for the expected type.

Actual Behaviour

Invalid dates can be downgraded 2021-02-31 -> 2021-02-28 or just become null 2021-02-32 -> null

Steps To Reproduce

The repo below shows this behavior in the test cases.

  1. Create a controller endpoint with a query parameter using @Format("yyyy-MM-dd") @QueryValue test: LocalDate
  2. Send an invalid LocalDate string
  3. See some dates are downgraded or forced to be null

Environment Information

Example Application

https://github.com/sbeins55/date-parsing

Version

3.0.3

sbeins55 commented 2 years ago

I wouldn't mind contributing a PR for this as I think I found what's happening.

It looks like the type converter uses a SimpleDateFormat which by default uses lenient parsing. The lenient parsing seems to be causing the auto downgrades.

Only reason I'm hesitant to open a PR is I'm no longer 100% certain that an invalid query parameter should throw a bad request, but maybe make it null? I'm also thinking lenient parsing is sometimes desired so this should be a configuration with the default being the current behavior?

graemerocher commented 2 years ago

bad request makes sense to me unless it is @Nullable

PRs welcome

jameskleeh commented 2 years ago

@sbeins55 Agreed - making it configurable with the default being the current behavior