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][JAVA] Static Initializer Block missing in JSON support class #16860

Closed
bergerchris opened this issue Oct 18, 2023 · 7 comments · Fixed by #16892
Closed

[BUG][JAVA] Static Initializer Block missing in JSON support class #16860

bergerchris opened this issue Oct 18, 2023 · 7 comments · Fixed by #16892

Comments

@bergerchris
Copy link

Description

To reproduce the error I just used the Swagger Petstore example. I generated java code with openapi-generator version 7.0.1 and tried to create a model an use toJson() to get the Json-String from the model.

Customer customer = new Customer();
customer.setUsername("test");

String test = customer.toJson();

The result is an NullPointerException: Cannot invoke "com.google.gson.Gson.toJson(Object)" because the return value of "com.domain.test.JSON.getGson()" is null

I think the problem might be, that no static initializer block is used in the JSON support class. There is only an instance initializer block like this

    {
        GsonBuilder gsonBuilder = createGson();
        gsonBuilder.registerTypeAdapter(Date.class, dateTypeAdapter);
        gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter);
        gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter);
        gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter);
        gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter);
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Address.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Category.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Customer.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.ModelApiResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Order.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Pet.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Tag.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.User.CustomTypeAdapterFactory());
        gson = gsonBuilder.create();
    }

When i add "static" to make it a static initializer block everything works as expected.

   static {
        GsonBuilder gsonBuilder = createGson();
        gsonBuilder.registerTypeAdapter(Date.class, dateTypeAdapter);
        gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter);
        gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter);
        gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter);
        gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter);
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Address.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Category.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Customer.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.ModelApiResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Order.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Pet.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.Tag.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new com.domain.test.model.User.CustomTypeAdapterFactory());
        gson = gsonBuilder.create();
    }
blutorange added a commit to blutorange/openapi-generator that referenced this issue Oct 23, 2023
@JanUlrichR
Copy link

Hey, thanks for providing a fix for that issue. Can we estimate when this fix will be merged ?

@wing328
Copy link
Member

wing328 commented Nov 2, 2023

@JanUlrichR have you tested it locally to confirm the fix?

@wing328 wing328 mentioned this issue Nov 4, 2023
5 tasks
@wing328
Copy link
Member

wing328 commented Nov 4, 2023

I've filed #16984 to add a test but it doesn't throw any exception.

How can I properly repeat the issue?

@JanUlrichR
Copy link

Hey @wing328 ,
I am a little bit confused about this. In your PR the pipeline shows the exact error that was mentioned in the description.
Applying the changes from blutorange solve that issue.
When checking out the repo and running the test locally i get the same errors as the PR pipeline.
The changes made by blutorange also fixed the problem locally.

@wing328
Copy link
Member

wing328 commented Nov 8, 2023

When checking out the repo and running the test locally i get the same errors as the PR pipeline.

Are you using sbt to run the test?

I forgot to mention my tests passed locally (and in the CI) when ran with mvn clean package. (I didn't test with sbt locally)

@JanUlrichR
Copy link

For me sbt does not work. But the errors I receive seem to be some misconfiguration on my end. (Haven't worked with sbt before)

I checked with maven.

@wing328
Copy link
Member

wing328 commented Nov 8, 2023

Which version of JDK are you using?

I'm using OpenJDK 11 on Mac

wing328 pushed a commit that referenced this issue Feb 28, 2024
…16892)

* [okhttp-gson] Make initializer block for GSON builder static #16860

Make initializer block for GSON builder static, see #16860

* update sapmles

---------

Co-authored-by: Andre Wachsmuth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants