Swagger-codegen: [JAVA] [SpringBoot server] Issue with date parameter in generated controller

Created on 2 Nov 2016  路  9Comments  路  Source: swagger-api/swagger-codegen

Description

I'm having some issues with a date parameter in a SpringBoot controller I've generated from my Swagger spec.
It seems like no matter what input is sent, there is an issue with the formatting.
When I launch a request like -

http://localhost:8080/v1/mysearch/search?departureDate=2016-12-12

I receive an error like this:

    Failed to convert value of type [java.lang.String] to required type [org.joda.time.LocalDate];
    nested exception is org.springframework.core.convert.ConversionFailedException:
    Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.RequestParam org.joda.time.LocalDate] for value '2016-12-12';
    nested exception is java.lang.IllegalArgumentException: Invalid format: "2016-12-12" is malformed at "16-12-12"

I thought perhaps my issue was with the formatting of the date and that a date-time might be required so I tried using "2016-12-22T21:03:41" based on date time in rfc3339
but that still resulted in an error -

    Failed to convert value of type [java.lang.String] to required type [org.joda.time.LocalDate];
    nested exception is org.springframework.core.convert.ConversionFailedException:
    Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiParam @org.springframework.web.bind.annotation.RequestParam org.joda.time.LocalDate] for value '2016-12-22T21:03:41';nested exception is java.lang.IllegalArgumentException: Invalid format: "2016-12-22T21:03:41" is malformed at "16-12-22T21:03:41"
Swagger-codegen version

v 2.2.1 - latest stable cloned directly from github

Swagger declaration file content or url

The spec definition for the parameter is as follows:
```yaml
parameters:
departureDate:
name: departureDate
in: query
required: true
type: string
format: date

The generated code looks like this
```Java
public ResponseEntity<Results> someSearchGet(
...        
        @ApiParam(value = "Blah", required = true, defaultValue = "2016-12-25")
        @RequestParam(value = "departureDate", required = true, defaultValue="2016-12-25")
        LocalDate departureDate,
...
Command line used for generation
$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \ 
     -i /d/git_clones/mysearch/swagger.yaml \
     -l spring 
     -o /d/git_clones/swagger-generated/mysearch_springboot
Steps to reproduce

  • Create swagger spec using the definition above
  • Generate a Spring Boot Server stub using the commandline given above
  • Use any client to make a request to the server with 2016-12-12 (or any date) as the input parameter
  • See that the error for date format is returned
Suggest a Fix

I found a workaround on a spring site (link here) by adding a annotation to the generated controller input parameter which works.
But if I need to regenerate my controller I guess I'll have to keep modifying manually.

  @ApiParam(value = "Blah", required = true, defaultValue = "2016-12-25")
  @RequestParam(value = "departureDate", required = true, defaultValue="2016-12-25")
  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate departureDate

Is there something in my input that isn't correct or is this a known issue?

Regards
DarrenC

Spring

Most helpful comment

Please reopen so that we can fix this properly.
The correct way to set the default date format for MVC is to add this class

@Configuration
public class DateTimeFormatConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
    }
}

All 9 comments

Spring/Spring-boot currently handles date types only in the JSON body.
Your way of using DateTimeFormat is fine but I think it would be better to register custom converters.
See https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-using-type-converters-with-spring-mvc/

Hi,

Thanks very much for the reply and explanation. I'll go ahead and close out this issue.

Summary for anyone reading this later

Issue:
Spring/SpringBoot date parameter in QUERY STRING was not generated in ServerStub correctly and was causing invalid format exceptions.

Resolution:
Spring/Spring Boot only supports the date/date-time format in BODY parameters.

My very manual workaround was following this suggestion -
link here to www.petrikainulainen.net article

But a better and cleaner solution was suggested by @cbornet above -
link here to another www.petrikainulainen.net article

Thanks very much to all.

Please reopen so that we can fix this properly.
The correct way to set the default date format for MVC is to add this class

@Configuration
public class DateTimeFormatConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
        registrar.setUseIsoFormat(true);
        registrar.registerFormatters(registry);
    }
}

reopened as requested - apologies I didn't realise this was a candidate for a full fix.

I run into the same issue but got "java.lang.ClassNotFoundException: java.time.LocalDateTime" with solution by @cbornet. Appears that this was because I was using java 7 so changing DateTimeFormatterRegistrar to JodaTimeFormatterRegistrar fixed it for me.

Hello,
is there any ETA to include one among the two working proposals (@DarrenC vs @cbornet)?

TIA,
Matteo

Following. I too have this issue.

Is this and/or #1235 ever going to get fixed?

What is the current work around for this issue?

Was this page helpful?
0 / 5 - 0 ratings