OpenAPITools / OpenAPITools/openapi-generator

Invalid Java is generated from swagger (2.0) - copying definition names with « »

Open
#6,366 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Java generator generates invalid java code where swagger-codegen succeeded. openapi codegen produces from definition ResultList«GenericRow»-> ResultListGenericRowresultList«GenericRow» =

openapi-generator version

4.3.1 with maven plugin

OpenAPI declaration file content or url
{
	"swagger": "2.0",
	"info": {
		"description": "Api Documentation",
		"version": "1.0",
		"title": "Api Documentation",
		"termsOfService": "urn:tos",
		"contact": {
			
		},
		"license": {
			"name": "Apache 2.0",
			"url": "http://www.apache.org/licenses/LICENSE-2.0"
		}
	},
	"securityDefinitions": {
		"basicAuth": {
			"type": "basic"
		}
	},
	"security": [
		{
			"basicAuth": []
		}
	],
	"host": "example.com",
	"basePath": "/api",
	"tags": [
	],
	"paths": {
	},
	"definitions": {
		"GenericRow": {
			"type": "object",
			"title": "GenericRow",
			"additionalProperties": {
				"type": "object"
			}
		},
		"ResultList«GenericRow»": {
			"type": "object",
			"properties": {
				"offset": {
					"type": "integer",
					"format": "int64"
				},
				"overallCount": {
					"type": "integer",
					"format": "int64"
				},
				"results": {
					"type": "array",
					"items": {
						"$ref": "#/definitions/GenericRow"
					}
				}
			},
			"title": "ResultList«GenericRow»"
		}
	}
}
Command line used for generation
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generateApiTests>false</generateApiTests>
                        <generateModelTests>false</generateModelTests>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <environmentVariables>
                            <supportingFiles>
                                ApiClient.java,Authentication.java,OAuth.java,ApiKeyAuth.java,HttpBasicAuth.java,RFC3339DateFormat.java
                            </supportingFiles>
                        </environmentVariables>
                        <inputSpec>${project.basedir}/src/main/resources/api-tpz.json</inputSpec>
                        <generatorName>java</generatorName>
                        <configOptions>
                            <library>resttemplate</library>
                            <dateLibrary>java8</dateLibrary>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Steps to reproduce
  • set up maven java project
  • add given plugin setting
  • put api spec in file /src/main/resources/api-tpz.json
  • run mvn compile
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/1137

Suggest a fix/enhancement

a generator should never generate invalid code. Either it generates valid code or it errors out.
For me it would be sufficient to have a workaround. the swagger is given i could not regenerate. i need only a valid client.

Regarding file name and class the special character are handled correctly -> ResultListGenericRow is generated. The problem only occurs from variable names of that type -> resultList«GenericRow»

the part that handles these special characters for generating file names could be reused for generating variable names?!

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

Start by reproducing the provided Swagger 2.0 definition with the openapi-generator Maven plugin and Java generator. Compare the valid ResultListGenericRow class name with the generated resultList«GenericRow» variable, then verify that the generated client compiles with valid Java identifiers.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
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.