Skip to content

Groovy code-gen imports ArrayList incorrectly in models  #7222

Open
@tobisinghania

Description

@tobisinghania
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions