swagger-api / swagger-api/swagger-codegen

Enum for path type on parameter block causes erroneous code generation

Open
#5,479 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Feature: Enum help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description
Swagger-codegen version

2..2.2

Swagger declaration file content or url

Given the following block:

  /eventinfo/{item}:
    get:
      description: Get a list of event items.
      operationId: getEventInfo
      parameters:
        - name: item
          in: path
          required: true
          type: string
          **enum: [USERNAME, EVENTTYPE, ACTION, MODEL, PUBID, VERSION, CONTENTPID]**
      produces:
        - application/json
      responses:
        '200':
          description: Ok
          schema:
            $ref: '#/definitions/eventInfoList'
        '404':
          description: file not found
          schema:
            $ref: '#/definitions/errorModel'
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/errorModel'

Causes the following erroneous generated Java line:
ResponseEntity getEventInfo(@ApiParam(value = "",required=true, allowableValues="{values=[USERNAME, EVENTTYPE, ACTION, MODEL, PUBID, VERSION, CONTENTPID], enumVars=[{name=USERNAME, value="USERNAME"}, {name=EVENTTYPE, value="EVENTTYPE"}, {name=ACTION, value="ACTION"}, {name=MODEL, value="MODEL"}, {name=PUBID, value="PUBID"}, {name=VERSION, value="VERSION"}, {name=CONTENTPID, value="CONTENTPID"}]}" ) @PathVariable("item") String item);

The additional quotes are around the value field are incorrect and cause compilation problems as shown:

[ERROR] src/gen/java/main/io/swagger/api/EventinfoApi.java:[32,208] ')' expected
[ERROR] /src/gen/java/main/io/swagger/api/EventinfoApi.java:[32,216] expected
[ERROR] src/gen/java/main/io/swagger/api/EventinfoApi.java:[32,244] ';' expected

Language type is "Spring" and I executed this via the maven plugin, so I'm hoping this is the correct area to place this in.

My plugin conf:

				<plugin>
					<groupId>io.swagger</groupId>
					<artifactId>swagger-codegen-maven-plugin</artifactId>
					<version>2.2.2</version>
					<executions>
						<execution>
							<phase>validate</phase>
							<goals>
								<goal>generate</goal>
							</goals>
							<configuration>
								<inputSpec>${project.basedir}/api.yaml</inputSpec>
								<language>spring</language>
								<output>${project.build.directory}/generated-sources</output>
								<configOptions>
									<generateApiTests>false</generateApiTests>
									<dateLibrary>joda</dateLibrary>
									<sourceFolder>src/gen/java/main</sourceFolder>
									<interfaceOnly>false</interfaceOnly>
									<library>spring-mvc</library>
								</configOptions>
							</configuration>
						</execution>
					</executions>
				</plugin>

A workaround is to generate a new type and refer to it there, however that is a little a bit of code clutter.
Please advise if there is better workaround or I guess this is probably a bug.

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 supplied api.yaml through the swagger-codegen Maven plugin using the Spring generator, then inspect how the enum parameter becomes the @ApiParam allowableValues text in EventinfoApi.java. Done means the generated Java output no longer contains the extra quotes and compiles successfully.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.