OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.34k stars 6.46k forks source link

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

Open j0th opened 5 years ago

j0th commented 5 years ago
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:

    /**
     * @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));

    }
auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.