swagger-api / swagger-api/swagger-codegen

[JAVA] [Bug] '$' character in enums is converted to '_' by swagger-codgen

Open
#11,285 1 comment 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

Hi,

I'm using swagger-codgen to generate code from OpenAPI yaml file.
When I generate models with enum containing dollar character '$', the '$' character is converted to underscore '_'

OpenAPI declaration file

openapi: 3.0.3
...
components:
	schemas:
		DocType:
		  type: string
		  description: Type of doc
		  enum:
			- TEST$1
			- TEST$2
			- TEST$3

Generation plugin in pom.xml

<plugin>
	<groupId>io.swagger.codegen.v3</groupId>
	<artifactId>swagger-codegen-maven-plugin</artifactId>
	<version>${io.swagger.codegen.v3.version}</version>
	<executions>
		<execution>
			<goals>
				<goal>generate</goal>
			</goals>
			<configuration>
				<inputSpec>${project.basedir}/openapi.yml</inputSpec>
				<language>spring</language>
				<modelPackage>com.he.model</modelPackage>
				<apiPackage>com.he.api</apiPackage>
				<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
				<configOptions>
					<dateLibrary>java8</dateLibrary>
					<delegatePattern>true</delegatePattern>
					<useTags>true</useTags>
					<unhandledException>true</unhandledException>
				</configOptions>
			</configuration>
		</execution>
	</executions>
</plugin>

Generated model

public enum DocType {
  TEST_1("TEST$1"),
    TEST_2("TEST$2"),
    TEST_3("TEST$3");

  private String value;

  DocType(String value) {
    this.value = value;
  }

  @Override
  @JsonValue
  public String toString() {
    return String.valueOf(value);
  }

  @JsonCreator
  public static DocType fromValue(String text) {
    for (DocType b : DocType.values()) {
      if (String.valueOf(b.value).equals(text)) {
        return b;
      }
    }
    return null;
  }
}
Swagger-codegen version

3.0.30

Command line used for generation

Generate code with mvn install

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 OpenAPI declaration and Maven plugin configuration using mvn install. Inspect the generated DocType enum and trace the Java enum-name generation entry point or templates. Done means enum constants generated from values containing '$' remain distinct while preserving their original serialized values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.