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
22k stars 6.6k forks source link

[BUG] x-field-extra-annotation for spring generator produces invalid Java source #19953

Open wilx opened 1 month ago

wilx commented 1 month ago

Bug Report Checklist

Description

The Java Spring generator produces invalid Java source. The documentation suggests that the x-field-extra-annotation parameter is a list of extra annotations:

List of custom annotations to be added to property

However, in generated code it outputs the annotations between [] and also multiple times when there are multiple annotations in the list, e.g.:

[@MyAnnotation, @My2ndAnnotation] [@MyAnnotation, @My2ndAnnotation] @Parameter(name = "status", description = "", in = ParameterIn.QUERY) @Valid @RequestParam(value = "status", required = false) String status
openapi-generator version

7.9.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Test
  version: 0.0.1
tags:
  - name: Test
paths:
  /pet/findByStatus:
    get:
      tags:
        - Test
      operationId: findPetsByStatus
      parameters:
        - name: status
          x-field-extra-annotation:
            - '@MyAnnotation'
            - '@My2ndAnnotation'
          in: query
          required: false
          schema:
            type: string
      responses:
        '204':
          description: successful operation
Generation Details
Steps to reproduce

Run with the OpenAPI document above.

java -jar openapi-generator-cli.jar generate -i min.yml -g spring
Related issues/PRs
Suggest a fix

The list of the annotations should be concatenated with a space.

L4rue commented 3 days ago

It might be a mistake in pojo.mustache in L66-68

I try to change

  {{#vendorExtensions.x-field-extra-annotation}}
  {{{vendorExtensions.x-field-extra-annotation}}}
  {{/vendorExtensions.x-field-extra-annotation}}

to

  {{#vendorExtensions.x-field-extra-annotation}}
  {{#.}}
  {{{.}}}
  {{/.}}
  {{/vendorExtensions.x-field-extra-annotation}}

and it works perfect.

Here are steps what I do.

  1. Get the pojo.mustache and put in resources, such as src/main/resources/mustache
  2. Modify the part that handles x-field-extra-annotation in the pojo.mustache file as what I say before
  3. Modify the openApiGenerate config in build.gradle, add templateDir = "$projectDir/src/main/resources/mustache". (Just on Gradle, maybe different on maven)