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] [Client] [Java] [Vertx] Current context may return NPE when creating new WebClient #8492

Closed
5 of 6 tasks
ricardozanini opened this issue Jan 20, 2021 · 8 comments · Fixed by #8501
Closed
5 of 6 tasks

Comments

@ricardozanini
Copy link
Contributor

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Hi!

While testing the generated client code for the petstore.yaml example, the ApiClient.java code is returning NPEs the first time it tries to create a WebClient reference. More specifically here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/vertx/ApiClient.mustache#L124-L132

Is there a reason to not use this.vertx.getOrCreateContext() instead? The vertx attribute is final. I can send a small PR to fix this, but I'd like to hear from the contributors first.

openapi-generator version

5.0.0

OpenAPI declaration file content or URL

Tested with petstore.yaml simple example.

Generation Details

Here's my plugin configuration:

<build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${version.openapi.generator}</version>
        <executions>
          <execution>
            <id>default</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/petstore.yaml</inputSpec>
              <generatorName>java</generatorName>
              <library>vertx</library>
              <configOptions>
                <dateLibrary>java8</dateLibrary>
              </configOptions>
              <apiPackage>${project.groupId}.client</apiPackage>
              <modelPackage>${project.groupId}.client</modelPackage>
              <invokerPackage>${project.groupId}.client</invokerPackage>
              <generateApiTests>false</generateApiTests>
              <generateModelTests>false</generateModelTests>
              <generateModelDocumentation>false</generateModelDocumentation>
              <generateApiDocumentation>false</generateApiDocumentation>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Steps to reproduce
  1. Generate the Code
  2. Run the tests with Junit5 and Vertx 3.9.5 (https://vertx.io/docs/3.9.5/vertx-junit5/java/)
Related issues/PRs

There's none

Suggest a fix

Replace Vertx.currentContext with this.vertx.getOrCreateContext()

By the way, I see that the server-side was upgraded to Vertx 4 (#7352). Don't we need to upgrade the client code as well? I can also send a PR for this purpose.

@wing328
Copy link
Member

wing328 commented Jan 21, 2021

@ricardozanini thanks for reporting the issue. Please submit a PR to address these issues and we'll review accordingly. Let me know if you need help with the PR.

@ricardozanini
Copy link
Contributor Author

@ricardozanini thanks for reporting the issue. Please submit a PR to address these issues and we'll review accordingly. Let me know if you need help with the PR.

@wing328 I've opened the PR (#8492), but it seems that we are lacking reviewers. Do you mind taking a look? Many thanks! 🙌

@wing328
Copy link
Member

wing328 commented Jan 27, 2021

Thanks for the PR. Will try to review later today.

@wing328 wing328 added this to the 5.0.1 milestone Jan 27, 2021
@wing328
Copy link
Member

wing328 commented Jan 27, 2021

@ricardozanini do you mind sharing more on how you reproduce the issue? We do have some tests in https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/java/vertx/src/test/java/org/openapitools/client/api/PetApiTest.java#L55-L61 so would appreciate if you can add some tests in that test file to catch the issue moving forward.

@ricardozanini
Copy link
Contributor Author

ricardozanini commented Jan 27, 2021

do you mind sharing more on how you reproduce the issue?

@wing328, the error only occurs on JUnit 5 + Vertx 3.9.x. This test case of yours will always work. In the PR, I added a verification code to ensure the Vertx context is never null. Instead of using the static class, we use the one provided by the constructor.

You would need to create a JUnit test like this one:
https://github.com/ricardozanini/openapi-codegen-poc/blob/main/src/test/java/com/github/zanini/openapi/codegen/PetStoreAPITest.java

Since our project here uses Junit 4, we won't catch the problem.

I don't think we need another unit test. What would be great is to upgrade to JUnit 5 and Vertx 4.x.

I can do it another time.

@wing328
Copy link
Member

wing328 commented Jan 28, 2021

@ricardozanini thanks for the explanation. I've merged your fix.

Can you please file a PR to upgrade dependencies to JUnit 5 and Vertx 4.x when you've time?

@ricardozanini
Copy link
Contributor Author

@wing328 of course, I'll do it. Should I open an issue first?

@wing328
Copy link
Member

wing328 commented Jan 28, 2021

Up to you. You can also link the PR to the discussion here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants