OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.44k stars 6.48k forks source link

[BUG][JAVA][Gradle Plugin] Java client generator not initializing fields #5157

Open saurabhsjoshi opened 4 years ago

saurabhsjoshi commented 4 years ago

Bug Report Checklist

Description

Using the openapi-generator gradle plugin I setup a task to generate code for the Petstore example YAML. Checking the generated Java classes I can see this in Pet.java:

 @JsonProperty("id")
  private Long id = ;

  @JsonProperty("name")
  private String name = ;

  @JsonProperty("tag")
  private String tag = ;

My Gradle task:

task generatePetStore(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    String basePackage = "com.example.petstore"
    verbose = true
    library = "resttemplate"
    generatorName = "java"
    inputSpec = "${project.rootDir}/swagger/petstore.yaml".toString()
    outputDir = "${project.buildDir}/generated/api/petstore"
    invokerPackage = basePackage
    apiPackage = basePackage + ".controller"
    modelPackage = basePackage + ".model"
    configOptions = [
            dateLibrary: "java8-localdatetime"
    ]
}
openapi-generator version

openapi-generator-gradle-plugin 4.2.2

OpenAPI declaration file content or url

URL: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore.yaml

Command line used for generation
Steps to reproduce
  1. Download Petstore Example YAML
  2. Run code generation using the Gradle Plugin
  3. Navigate to the Pet.java generated file
Related issues/PRs
Suggest a fix

The fields probably need to be initialized to null at least as the generated code is invalid Java syntax. Something like

  @JsonProperty("id")
  private Long id = null;

  @JsonProperty("name")
  private String name = null;

  @JsonProperty("tag")
  private String tag = null;
saurabhsjoshi commented 4 years ago

I seem to have narrowed down the issue. It appears that if you have the swagger-codegen plugin in your class path (specifically I had io.swagger:swagger-codegen:2.3.1) it results in the above error. Removing this plugin resolved the issue. Not sure if having a different plugin in the classpath should affect the functioning of this plugin so keeping the bug open.

Here is my original build.gradle configuration:

buildscript {
...
 dependencies {
        classpath("io.swagger:swagger-codegen:2.3.1")
        classpath "org.openapitools:openapi-generator-gradle-plugin:4.2.2"
    }
}

Removing the swagger-codegen solved the issue

AledLewis commented 3 years ago

Saurabh, you saved my day! Hitting the exact same issue and same resolution.