Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Missing import statement in ApiClient.java for JsonNullableModule when using generator 'java' and library 'resttemplate' with option 'withXml' enabled #7209

Closed
5 of 6 tasks
anthonyhseb opened this issue Aug 13, 2020 · 6 comments

Comments

@anthonyhseb
Copy link

anthonyhseb commented Aug 13, 2020

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When adding the withXml : "true" option in order for the java generator (with the 'resttemplate' library) to include @XmlRootElement, @JacksonXmlRootElement annotations necessary to name the root element in the XML request body, the generated ApiClient.java class fails to compile giving the following error.

ApiClient.java:648: error: cannot find symbol
        xmlMapper.registerModule(new JsonNullableModule());
                                     ^
  symbol:   class JsonNullableModule
  location: class ApiClient

Looking at the imported classes in ApiClient.java, there is no import statement for that class, and it has to be added manually whenever the client code is regenerated.

Without withXml : "true" the generated source does compile but the class name is used for root elements instead of the declared name in the spec.

Using the Gradle plugin with the following config:

task myGenerateTask (type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = "java"
    library = "resttemplate"
    inputSpec = project.projectDir.getPath() + "/src/main/schema/myservice.yaml"
    outputDir = project.buildDir.getPath() + "/generated-sources/myservice"
    generateModelTests = false
    generateModelDocumentation = false
    generateApiTests = false
    generateApiDocumentation = false
    def rootPackage = "mypackage"
    apiPackage = rootPackage + ".api"
    invokerPackage = rootPackage + ".client"
    modelPackage = rootPackage + ".model"
    configOptions = [
            java8 : "true",
            dateLibrary: "java8",
            withXml : "true"
    ]
    inputs.file inputSpec
    outputs.dir outputDir
}
openapi-generator version

Tested with 4.3.0, 4.3.1, and 5.0.0-beta

OpenAPI declaration file content or url

I cannot share the spec file since it is confidential, the file passes validation, and is fairly standard.

The file contains type declarations, with an xml name property.

    MyType:
      properties:
        code:
          type: integer
          description: Identifier.
        label:
          type: string
          description: A human-readable label.
      example:
        code: 100
        label: Foo
      xml:
        name: myObject
Generation Details

Using Gradle with org.openapitools:openapi-generator-gradle-plugin:4.3.1 (or any of the versions mentioned above)

gradle myGenerateTask

Then compile the generated sources.

Steps to reproduce
  1. Generate client sources for any valid v3 yaml, using generator 'java' and library 'resttemplate' and configOption withXml enabled.
  2. Open the generated ApiClient.java and search for JsonNullableModule, you'll notice that it is used without an import statement.
  3. Compiling the generated sources will result with a "cannot find symbol" error message.
Related issues/PRs
Suggest a fix
@wing328
Copy link
Member

wing328 commented Aug 14, 2020

Can you please try the latest master instead to see if it's still an issue?

@anthonyhseb
Copy link
Author

anthonyhseb commented Aug 14, 2020

@wing328 I tried with 5.0.0-SNAPSHOT (modified 14 Aug 2020), the import statement for JsonNullableModule is still missing

@wing328
Copy link
Member

wing328 commented Aug 20, 2020

Thanks for the confirmation. May I know if you've time to contribute a fix? I can show you some good starting points.

@quarantam
Copy link
Contributor

I'm experiencing the same issue also with 5.1.1 plugin version...can you please provide a fix?

@quarantam
Copy link
Contributor

quarantam commented May 16, 2021

I investigated further, bug is in Apiclient.mustache template, JsonNullableModule import is driven by threetenbp... so, if you are using (as I do) a different dateLibrary implementation import statement will be skipped...
Fix is really simple, I created a pull request #9492

wing328 pushed a commit that referenced this issue May 17, 2021
premiumitsolution pushed a commit to premiumitsolution/openapi-generator that referenced this issue May 26, 2021
@boardtc
Copy link

boardtc commented Jan 20, 2023

the import statement is generated for me (import org.openapitools.jackson.nullable.JsonNullableModule) but the cinmpiler does not recognise openapitools..

@Philzen Philzen closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants