openAPI 3.1 codegen with java spring expression language

144 views Asked by At

I am trying to write a REST service using Java Spring and I want to use openAPI to generate my controller. One problem I am facing is that I cannot generate it properly. The reason is: In my REST service, I want to be able to search between two dates. To trivialize the controller logic, I want to set the endDate always to the current time(LocalDateTime.now).

OpenAPI (code snippet which matters):

          schema:
            default: 50
            type: integer
            description: The maximum number of items to return
        - in: item
          name: startDate
          schema:
            type: string
            format: date
          description: The start Date of the Items to search for
        - in: item
          name: endDate
          schema:
            type: string
            format: string
            default: "#{T(java.time.LocalDateTime).now()}"     
          description: The end Date of the Items to search for

Generation command:

java -jar openapi-generator-cli.jar generate -i item.yaml -g spring --additional-properties delegatePattern=true --skip-validate-spec

generated Method:

    default ResponseEntity<List<Item>> itemsGet(

        @Parameter(name = "limit", description = "The maximum number of items to return", in = ParameterIn.ITEM) @Valid @RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit,

        @Parameter(name = "startDate", description = "The start Date of the items to search for", in = ParameterIn.ITEM) @Valid @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,

        @Parameter(name = "endDate", description = "The end Date of the items to search for", in = ParameterIn.ITEM) @Valid @RequestParam(value = "endDate", required = false, defaultValue = "#{T(java.time.LocalDateTime).now()}") String endDate

    ) {

        return getDelegate().itemsGet(limit, start, startDate, endDate);

    }

I am aware that the codegeneration is technically correct,but I donĀ“t want it to be a type of string. However if I use in my yaml specification as format date-time it will simply ignore the default value and does not generate it in the method.

So in summary I am looking for a way to get the following output:

    default ResponseEntity<List<Item>> itemsGet(

        @Parameter(name = "limit", description = "The maximum number of items to return", in = ParameterIn.ITEM) @Valid @RequestParam(value = "limit", required = false, defaultValue = "50") Integer limit,

        @Parameter(name = "startDate", description = "The start Date of the items to search for", in = ParameterIn.ITEM) @Valid @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,

        @Parameter(name = "endDate", description = "The end Date of the items to search for", in = ParameterIn.ITEM) @Valid @RequestParam(value = "endDate", required = false, defaultValue = "#{T(java.time.LocalDateTime).now()}") LocalDateTime endDate

    ) {

        return getDelegate().itemsGet(limit, start, startDate, endDate);

    }
1

There are 1 answers

0
lingulata On

I got the answer myself:

in OpenAPI yaml file:

- in: item
  name: endDate
  schema:
    type: string
    format: date-time
    default: "#{T(java.time.LocalDateTime).now()}"     
  description: The end Date of the Items to search for

In Gradle Task:

    openApiGenerate {
...
    typeMappings = ['string+date-time': 'LocalDateTime']
    importMappings = ['LocalDateTime': 'java.time.LocalDateTime']
...

}

In Get Request add at least to set it yourself:

?startDate=2023-10-31T21:22:10

Or if blank the current Time will be the default