Skip to content

Reconsider fallback date formats for ISO variants in DateTimeFormatterRegistrar #28130

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

Closed
membersound opened this issue Mar 3, 2022 · 4 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@membersound
Copy link

membersound commented Mar 3, 2022

spring-boot-starter-webflux-2.6.4

The following controller can take a date in yyyy-MM-dd format in a @PostMapping request without problems, but fails for a @GetMapping query parameter.

@RestController
public class DateTimeServlet {    
    static class DateTimeDto {
        private LocalDate date;
        //getter, setter
    }

    @PostMapping("/datetime")
    public Mono<String> datetimePost(@RequestBody DateTimeDto dto) {
        return Mono.just("OK");
    }

    @GetMapping("/datetime")
    public Mono<String> datetimeGet(DateTimeDto dto) {
        return Mono.just("OK);
    }
}

Works:

POST localhost:8080/datetime
{
    "date": "2022-02-02"
}

Does not work:

GET localhost:8080/datetime?date=2022-02-02

Result:

{
    "timestamp": "2022-03-03T08:57:56.248+00:00",
    "status": 400,
    "message": "'date': : Parse attempt failed for value [2022-02-02]. rejectedValue: 2022-02-02"
}

Why are get and post requests behaving that differently? Shouldn't either both succeed, or both fail?

Update: while I discovered that POST bodies are converted using jackson, and GET parameters using Spring Conversation Service, I wonder if it wouldn't be better that both could have the same behavior?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 3, 2022
@wilkinsona wilkinsona self-assigned this Mar 3, 2022
@wilkinsona
Copy link
Member

The difference in behaviour is because the POST that uses @RequestBody is being converted by Jackson, whereas the GET is being converted using a ConversionService. Jackson uses DateTimeFormatter.ISO_LOCAL_DATE by default for the conversion whereas the ConversionService uses DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).

We could consider changing these defaults for Spring Boot 3.0 so that they align. Beyond that, this issue is really a duplicate of spring-projects/spring-boot#30041.

@membersound
Copy link
Author

Okay, so the workaround for the moment is:

spring.webflux.format.date=ISO
spring.webflux.format.time=ISO
spring.webflux.format.date-time=ISO

@wilkinsona wilkinsona removed their assignment Mar 3, 2022
@bclozel bclozel transferred this issue from spring-projects/spring-boot Mar 3, 2022
@bclozel bclozel changed the title @GetMapping cannot parse LocalDate by default in webflux? Reconsider fallback date formats for ISO variants in DateTimeFormatterRegistrar Mar 3, 2022
@bclozel
Copy link
Member

bclozel commented Mar 3, 2022

Looking at this issue transferred from Spring Boot, we could reconsider in the 6.0 timeline the date time fallback formats to use ISO variants instead of FormatStyle.SHORT.

@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Mar 3, 2022
@sbrannen
Copy link
Member

sbrannen commented Mar 3, 2022

@sbrannen sbrannen closed this as completed Mar 3, 2022
@sbrannen sbrannen added status: duplicate A duplicate of another issue type: enhancement A general enhancement and removed for: team-attention status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants