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

1,186 views
Skip to first unread message

Darren Costello

unread,
Oct 27, 2016, 2:05:46 PM10/27/16
to Swagger
Hi,

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.

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


The generated code looks like this
public ResponseEntity<Results> someSearchGet(
...        
       
@ApiParam(value = "Blah", required = true, defaultValue = "2016-12-25")
       
@RequestParam(value = "departureDate", required = true, defaultValue="2016-12-25")
       
LocalDate departureDate,
...

but 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"

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

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



Cheers
Darren

Ron Ratovsky

unread,
Oct 31, 2016, 5:34:07 PM10/31/16
to swagger-sw...@googlegroups.com

For issues related to swagger-codegen, please open a ticket on the project.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages