swagger-api / swagger-api/swagger-codegen

[Java] An array of enums is not generated correctly

Open
#12,352 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When the YAML file contains a field with an array of enums, the API Java code generates the field as a single-option select list instead of a multi-select list.

It appears as:

image

Instead of:
image

Swagger-codegen version

swagger-codegen-maven-plugin 3.0.54

Swagger declaration file content or url
        - name: testfield
          in: query
          required: false
          schema:
            type: array
            items:
              type: integer
              enum:
                - '0'
                - '1'
                - '2'
                - '3'
                - '4'
Command line used for generation

mvn clean install

Steps to reproduce

Maven:

               <plugin>
                    <groupId>io.swagger.codegen.v3</groupId>
                    <artifactId>swagger-codegen-maven-plugin</artifactId>
                    <version>${swagger-codegen-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>java-model-generation</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <language>spring</language>
                                <library>spring-cloud</library>
                                <generateApis>true</generateApis>
                                <generateModels>true</generateModels>
                                <generateSupportingFiles>true</generateSupportingFiles>
                                <inputSpec>${project.basedir}/src/main/resources/${openapi.file.name}</inputSpec>
                                <output>${project.build.directory}/generated-sources/swagger</output>
                                <modelPackage>${openapi.model.package}</modelPackage>
                                <apiPackage>${openapi.client.package}</apiPackage>
                                <invokerPackage>${openapi.invoker.package}</invokerPackage>
                            <configOptions>
                                <java8>false</java8>
                                <jdk8>false</jdk8>
                                <defaultInterfaces>false</defaultInterfaces>
                                <interfaceOnly>true</interfaceOnly>
                                <dateLibrary>java8</dateLibrary>
                                <useTags>true</useTags>
                                <defaultInterfaces>false</defaultInterfaces>
                                <jakarta>true</jakarta>
                                <notNullJacksonAnnotation>true</notNullJacksonAnnotation>
                            </configOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

Suggest a fix/enhancement

When generating an array of enums, the parameter should be annotated with "@ArraySchema" instead of "@Schema". Now it is being generated as:

@Parameter(schema=@Schema(allowableValues={ "0", "1", "2", "3", "4" }))
@Valid @RequestParam(value = "testfield", required = false) List testfield

Instead of:
@Parameter(array = @ArraySchema(schema = @Schema(allowableValues = {"0", "1", "2", "3", "4"})))
@Valid @RequestParam(value = "testfield", required = false) List testfield

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 shown swagger-codegen-maven-plugin configuration using the Java Spring generation path, then trace the parameter generation that emits @Parameter and @Schema. Done means an array of enum query parameters generates @ArraySchema with the enum values and retains the List parameter type; verify the generated API output against the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.