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

Parsing date into @RequestParam LocalDate argument using spring.mvc.date-format fails with 'yyyy-MM-dd' #13970

Closed
jo-ka opened this issue Aug 1, 2018 · 2 comments
Labels
type: bug A general bug
Milestone

Comments

@jo-ka
Copy link

jo-ka commented Aug 1, 2018

When using the new feature to set a global date pattern for all @RequestParam LocalDate arguments by specifying the property spring.mvc.date-format, which is discussed here, I noticed that the common ISO pattern yyyy-MM-dd does not work.

The exception thrown looks like this:

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-04-20'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-04-20]

Through debugging, I found that the root cause is this exception:

Text '2018-04-20' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=20, YearOfEra=2018, MonthOfYear=4},ISO of type java.time.format.Parsed

And I also found that the DateTimeFormatter that gets used for parsing is represented like that by toString():

Value(YearOfEra,4,19,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2)

I presume that the reason why a LocalDate could not be obtained is that the TemporalAccessor that gets constructed lacks a value for the Era field, but has a filled YearOfEra field.

So I tried with uuuu-MM-dd instead, which works perfectly. @wilkinsona suspected that this is unintended and a symptom of a bug, therefore this bug report.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 1, 2018
@wilkinsona
Copy link
Member

Thank you, @jo-ka.

Here's a small test that reproduces the behaviour that you have described:

assertThat(new WebConversionService("yyyy-MM-dd").convert("2018-01-01",
		java.time.LocalDate.class)).isEqualTo(java.time.LocalDate.of(2018, 1, 1));

As I said in #5523, I think we need to switch from ResolverStyle.STRICT to ResolverStyle.SMART but let's wait for confirmation from @bclozel.

@wilkinsona
Copy link
Member

We're going to switch to ResolverStyle.SMART.

@wilkinsona wilkinsona added this to the 2.0.x milestone Aug 15, 2018
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 15, 2018
@mbhave mbhave modified the milestones: 2.0.x, 2.0.5 Aug 17, 2018
@mbhave mbhave closed this as completed in f7135ff Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants