Openapi generator cannot generate query param with enum

Viewed 4047

I'm trying to generate a service that has enum as query parameter, but it keeps generating it wrong. Here's the part of yaml:

      name: language
      in: query
      description: language
      schema:
        type: string
        enum:
        - en
        - de

and I'm using it in parameters:

        - $ref: '#/components/parameters/language'

The error that I get is:

[ERROR] .../api/TestApi.java:[110,65] illegal start of type
[ERROR] .../api/TestApi.java:[110,66] ')' expected
[ERROR] .../api/TestApi.java:[110,82] ';' expected

Here's what the code looks like:

public Response getByLanguage( @PathParam("id") Long id, , allowableValues="en, de" @QueryParam("language") String language

And here's my plugin:

                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>5.0.1</version>
                    <configuration>
                        <inputSpec>${basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <output>${project.build.directory}/generated-sources/java</output>
                        <generatorName>jaxrs-resteasy-eap</generatorName>
                        <modelPackage>com.openapi.example.model</modelPackage>
                        <apiPackage>com.openapi.example.api</apiPackage>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <sourceFolder>openapi</sourceFolder>
                            <dateLibrary>java8</dateLibrary>
                            <interfaceOnly>true</interfaceOnly>
                            <java8>true</java8>
                            <serializableModel>true</serializableModel>
                            <useTags>true</useTags>
                            <performBeanValidation>true</performBeanValidation>
                            <useBeanValidation>true</useBeanValidation>
                        </configOptions>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>generate-resources</phase>
                            <id>generate</id>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

The only way I've managed to get it to work is to make it as an array of enum values, but that's not what I need here.

EDIT: dependencies I have defined in the project:

        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-dependency-analyzer</artifactId>
            <version>1.11.1</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jaxrs</artifactId>
            <version>1.5.8</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.1.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.11.3</version>
        </dependency>
3 Answers

Try out with below demo yaml.

Enums should be defined as enum: [en, de].

openapi: 3.0.2
info: {title: Demo Info., description: Demo Description., version: 1.0.0}
servers:
- {url: 'https://something.com', description: Demo Description.}
paths:
  /something:
    post:
      tags: [Something]
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/SomethingRequest'}
      parameters:
      - {$ref: '#/components/parameters/language'}
      responses:
        200:
          description: OK
          content:
            application/json:
              schema: {$ref: '#/components/schemas/SomethingResponse'}
components:
  parameters:
    language:
      name: language
      schema:
        type: string
        enum: [en, de]
        default: en
      in: query
  schemas:
    SomethingRequest:
      properties:
        demo: {type: string}
    SomethingResponse:
      properties:
        demo: {type: string}

It depends what generator you are using but jaxrs-resteasy-eap is broken when using enums in query parameters.

I think this commit is causing the problem.

As a temporary fix in my project I edited the queryParams.mustache template file to as this:

{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{^isContainer}}{{#allowableValues}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}, {{> allowableValues }}){{/allowableValues}}{{#defaultValue}} @DefaultValue("{{{.}}}"){{/defaultValue}}{{/isContainer}} @QueryParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}

Just noticed that there are also github issue of this. The recommendation of removing the allowableValues from queryParams.mustache might be the correct way to go:

{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{^isContainer}}{{#defaultValue}} @DefaultValue("{{{.}}}"){{/defaultValue}}{{/isContainer}} @QueryParam("{{baseName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}

It depends how you are using the generator how to use own templates.

I am using it from npm @openapitools/openapi-generator-cli There you can apply your own templates by adding -t/--template-dir option into your generate command. Here is mine as example:

npx @openapitools/openapi-generator-cli generate -t=templates/jaxrs -i openapi.yaml -o generated/jaxrs -g jaxrs-resteasy-eap -c config-jaxrs.yaml

I just copied my modified queryParams.mustache to root of templates/jaxrs and the generated api functions did not have double comma syntax problems anymore:

public Response getPets( @ApiParam(value = "My param description", allowableValues="AA, BB, CC") @QueryParam("petType") PetType petType,@Context SecurityContext securityContext);

I haven't used these files yet, so I don't know if everything is correct, but atleast the syntax is correct now.

More info here: templating documentation

Related