swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

Java RestTemplate generator should format correctly java8 dates in query parameters

Open spydesk opened this issue 6 years ago • 1 comments

Hi,

Having the given spec :

openapi: 3.0.0
info:
  version: "1.0"
  title: API
servers:
  - description: Mock
    url: ***
paths:
  /catalog/products:
    get:
      summary: finds products
      operationId: findProducts
      tags: 
        - catalog
      parameters:
        - in: query
          name: modifiedSince
          description: filter products modified since the given value
          required: false
          schema:
            type: string
            format: date-time
      responses:
        '200':
          description: products
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Product'
        '400':
          description: bad input parameter
components:
  schemas:
    Product:
      type: object
      required:
        - id
      properties:
        id:
          type: int

I'm using the swagger-codegen-maven-plugin with the given configuration :

<configuration>
	<inputSpec>v1/openapi.yaml</inputSpec>
	<language>java</language>
	<invokerPackage>${project.groupId}.api.v1.client.invoker</invokerPackage>
	<apiPackage>${project.groupId}.api.v1.client</apiPackage>
	<modelPackage>${project.groupId}.api.v1.model</modelPackage>
	<library>resttemplate</library>
	<generateApiTests>false</generateApiTests>
	<generateApiDocumentation>false</generateApiDocumentation>
	<configOptions>
		<dateLibrary>java8</dateLibrary>
		<java8>true</java8>
	</configOptions>
</configuration>

This generate the following client API :

public class CatalogApi {

    public List<Product> findProducts(OffsetDateTime modifiedSince) throws RestClientException {
        ...
    }

}

However, when calling this method with an OfffsetDateTime, like this :

catalogApi.findProducts(OffsetDateTime.now());

It generates the following HTTP request :

GET /catalog/products?modifiedSince=2019-02-05T14:57:54.928+01:00

The server answer with a 400 Bad request.

I suspect that the "+" sign is interpreted as the "space" character from the server in this case and it fails to deserialize the value correctly in the appropriate OffsetDateTime type...

Looking at the template "/src/main/resources/handlebars/Java/libraries/resttemplate/ApiClient.mustache" method "parameterToString" there is a special case to handle formatting of java.util.Date but nothing to handle java 8 dates, such as OffsetDateTime...

spydesk avatar Feb 05 '19 14:02 spydesk

After updating a bunch of libs (spring, spring-boot. jackson, OpenApi-generator-maven-plugin v5.2.1,..) I ran into the same problem with generatorName=java, library=resttemplate, dateLibrary=java8.

As said above ApiClient::parameterToString makes a return formatOffsetDateTime((OffsetDateTime) param); Even if configured like apiClient.setOffsetDateTimeFormatter(DateTimeFormatter.ISO_OFFSET_DATE_TIME) I get the correct format, but the 400 Bad Param as mentioned above. I guess some URL encoding should be done for the param explicitely.

(For library=webclient this is done correctly by the generated ApiClient.)

phirzel avatar Nov 07 '21 17:11 phirzel