swagger-api / swagger-api/swagger-codegen

Groovy code-gen imports ArrayList incorrectly in models

Open
#7,222 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Groovy Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The groovy code generator imports the ArrayList class for array types in models, but does not
import it correctly.

Instead of generating import java.util.ArrayList (or for Groovy nothing at all), the generated code imports the ArrayList class from the models package.

Swagger-codegen version

The error happens for swagger-codegen version 2.2.3. For older versions I did not check.

Swagger declaration file content or url
Command line used for generation

I generate the code with the org.hidetake.swagger.generator gradle plugin:

task generateApi {
        inputs.file("$projectDir/$swaggerSourceFile")
        doLast {
            def config = new CodegenConfigurator()
            config.setInputSpec("file:///$projectDir/$swaggerSourceFile")
            config.setOutputDir("$buildDir")
            config.setLang('groovy')
            config.setAdditionalProperties([
                    'apiPackage'    : 'com.example.messaging.api',
                    'modelPackage'  : 'com.example.messaging.model',
                    'sourceFolder'  :  "../src/main/groovy"
            ])
            new DefaultGenerator().opts(config.toClientOptInput()).generate()
        }

but the error is also reproducible when the code is generated on https://editor.swagger.io/.

With the provided gradle task, the ArrayList class is imported as import com.example.messaging.model.ArrayList and with the online generator it is imported as import io.swagger.model.ArrayList

The error seems to only occur for Groovy and not for Java.

Steps to reproduce

Generate the model with the previously posted yaml file for Groovy.

Related issues/PRs

This issue seems to be related to issue Gradle build error #5046.

Suggest a fix/enhancement

If primitive types were not imported at all for the groovy language, the error would be fixed as no explicit imports are needed for

import java.lang.*
import java.util.*
import java.io.*
import java.net.*
import groovy.lang.*
import groovy.util.*
import java.math.BigInteger
import java.math.BigDecimal

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 provided YAML and the Groovy generation path using CodegenConfigurator and DefaultGenerator. Inspect how generated model imports are selected, then verify that an array model does not import ArrayList from the model package and instead uses the correct import or no explicit import.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
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.