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 
<https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14>
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 
<https://www.petrikainulainen.net/programming/spring-framework/spring-from-the-trenches-parsing-date-and-time-information-from-a-request-parameter/>)
 
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

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to