OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Java Spring] Illegal date format in generated delegate examples

Open
#3,908 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Spring
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

The generated code (generator v4.1.2) for the example responses in an api delegate has illegal date formats for the properties of type "string" and format "date".

public interface DummyApiDelegate {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    /**
     * @see DummyApi#dummyGet
     */
    default ResponseEntity<Result> dummyGet() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "{  \"attr1\" : \"2019-09-18T00:00:00.000+0000\"}");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.valueOf(200));
    }
}
openapi-generator version

4.1.2

OpenAPI declaration file content or url
openapi: 3.0.2

info:
  title: Test for illegal date format in delegate examples
  version: 1.0.0

paths:
  /dummy:
    get:
      summary: just a dummy
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                  $ref: '#/components/schemas/result'

components:
  schemas:
    result:
      description: the result with the illegal date format
      type: object
      properties:
        attr1:
          type: string
          format: date
          example: "2019-09-18"

Command line used for generation

docker run --rm -v %CD%:/local openapitools/openapi-generator-cli:v4.1.2 generate -i /local/test.yaml -g spring -o /local/testexample -c /local/config-test.json

The config-test.json looks like this:

{
  "library": "spring-boot",
  "delegatePattern": true,
  "returnSuccessCode": true,
  "hideGenerationTimestamp": true
}
Steps to reproduce

Just run the generator, you'll see

Related issues/PRs

Nothing found.

Suggest a fix

Not a fix , but a hint: The generated code have the expected date format:

  • if I use version 3.3.4 of the generator-cli
  • if I remove example: "2019-09-18" from attr1 (best solution for me, because I want to use version 4.x of the generator)
  • if I remove type: string from attr1 (but the the type of the generated getter and setter is just object, not so good)
    /**
     * @see DummyApi#dummyGet
     */
    default ResponseEntity<Result> dummyGet() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    ApiUtil.setExampleResponse(request, "application/json", "{  \"attr1\" : \"2019-09-18\"}");
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.valueOf(200));

    }

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

Reproduce the issue with the provided OpenAPI YAML, config-test.json, and Spring generator command using delegatePattern. Compare the generated Java delegate example for the date property with the declared example and the expected 2019-09-18 value. Done means generated date examples use the valid date format without regressing other example responses.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.