swagger-api / swagger-api/swagger-codegen-generators

[JavaSpring] Usage of Array-Type leads to syntax error in generated coding

Open
#998 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

Hi,

when generating based on yamls like this (simplified):

openapi: 3.0.1
info:
  title: Test for Array Issue
  description: Test for Array Issue
  version: 1.0.0
servers:
- url: /test/array/issue
paths:
  /array/issue:
    get:
      summary: Provoke Array Issue
      operationId: getProvokeArrayIssue
      responses:
        200:
          description: Ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorInformation'
components:
  schemas:
    MessageText:
      type: string
      description: Message Text
    Message:
      required:
        - text
      type: object
      properties:
        text:
          $ref: '#/components/schemas/MessageText'
      description: Message
    Messages:
      minItems: 1
      type: array
      description: Messages
      items:
        $ref: '#/components/schemas/Message'
    ErrorInformation:
      type: object
      required:
        - messages
      properties:
        messages:
          $ref: '#/components/schemas/Messages'
      description: Error Information

with maven plugin setting like

			<plugin>
				<groupId>io.swagger.codegen.v3</groupId>
				<artifactId>swagger-codegen-maven-plugin</artifactId>
				<version>3.0.30</version>
				<executions>
					<execution>
						<id>code-gen-swagger-test-array-isssue</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<addCompileSourceRoot>true</addCompileSourceRoot>
							<modelPackage>com.test.array.issue.model</modelPackage>
							<apiPackage>com.test.issue.api</apiPackage>
							<inputSpec>ArrayIssueTest.yaml</inputSpec>
							<generateApiTests>false</generateApiTests>
							<language>spring</language>
							<configOptions>
								<java8>true</java8>
								<fullJavaUtil>true</fullJavaUtil>
								<dateLibrary>java8</dateLibrary>
							</configOptions>
						</configuration>
					</execution>
				</executions>
			</plugin>

this leads to generated code like

...
public class Messages extends java.util.ArrayList<Message>  {
...

and

...
public class ErrorInformation   {
  @JsonProperty("messages")
  private Messages messages = new java.util.ArrayList<>();
...

where private Messages messages = new java.util.ArrayList<>(); is syntactically not correct.

Please have a look.

Thanks in advance,
Andreas

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 YAML and swagger-codegen-maven-plugin configuration using the spring language. Start by tracing the generator templates or entry points responsible for array model declarations and properties. Done means the generated ErrorInformation and Messages Java classes are syntactically valid and compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
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.