swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.94k stars 6.03k forks source link

Groovy code-gen imports ArrayList incorrectly in models #7222

Open tobisinghania opened 6 years ago

tobisinghania commented 6 years ago
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
wing328 commented 6 years ago

@tobisinghania yes, I think we can go with explicit import for the time being. Would you have time to file a PR with the suggested fix?