swagger-api / swagger-api/swagger-codegen-generators

Java RestTemplate generator should format correctly java8 dates in query parameters

Open
#295 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

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...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/main/resources/handlebars/Java/libraries/resttemplate/ApiClient.mustache, at the parameterToString method and its existing java.util.Date handling. Reproduce the generated request with an OffsetDateTime query parameter, then verify that the resulting date-time value is correctly formatted for the server instead of treating the plus sign as a space.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.