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

JavaFileGenerator in the generate models command ONLY generates the java model classes correctly if the openapi specs follow the java naming conventions #1038

Closed
oliverkuntze opened this issue Dec 15, 2023 · 9 comments · Fixed by #1039 or #1080
Labels
bug Something isn't working released

Comments

@oliverkuntze
Copy link

Describe the bug

At the moment the JavaFileGenerator in the generate models command ONLY generates the java model classes correctly if the openapi specs follow the java naming conventions.

For example if a schema in the openapi spec contains a property myID the generate models java command will generate a property myId in the corresponding class.

We cannot assume that authors of asyncapi specs strictly follow the rules of java naming conventions.

We should assure that all openapi specs can be correctly generated into java model classes independent of following java naming conventions when writing the specs or not.

Description

In https://github.com/asyncapi/cli/blob/master/src/commands/generate/models.ts the JavaFileGenerator currently is instantiated this way:
fileGenerator = new JavaFileGenerator();

The JavaFileGenerator should be instantiated this way:
fileGenerator = new JavaGenerator({ presets: [JAVA_JACKSON_PRESET] });

This way the schema properties not following the java naming conventions would annotated with the corresponding JsonProperty annotations.

This won't be a breaking change other than one has to add the jackson-annotations dependency to one's project.

How to Reproduce

Take any asyncapi spec with schema properties that are not written in camel case and run the cli with generate models java.
Have a look at the model classes to find all non camel case schema properties converted to camel case java properties, thus breaking the contract.

Expected behavior

Since asyncapi does not enforce the usage of java compliant naming conventions for the schema properties in their specs it should correctly generate model classes for specs that don't follow the java naming conventions.

@oliverkuntze oliverkuntze added the bug Something isn't working label Dec 15, 2023
Copy link
Contributor

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@oliverkuntze
Copy link
Author

If it helps I could do the coding myself and provide a pr.
Fixing this bug would allow me to integrate asyncapi as nicely as openapi in my gradle builds.

@Athul0491
Copy link
Contributor

Athul0491 commented Dec 16, 2023

@oliverkuntze
I've added a PR for this, any feedback is welcome. Thanks!

@oliverkuntze
Copy link
Author

Great news. Thank you, @Athul0491 !

Can't wait for the release. :-)

In the PR:
Is the
presets.push({ preset: JAVA_COMMON_PRESET, options }
needed for javaJackson and javaConstraints?

Cheers
Oliver

@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@oliverkuntze
Copy link
Author

oliverkuntze commented Jan 19, 2024

@Athul0491 , could it be that I was right with the remark that
presets.push({ preset: JAVA_COMMON_PRESET, options }
probably was incorrect for javaJackson and javaConstraints?
Activating for example the javaJackson option yields code that cannot be compiled since duplicate methods for equals, hashCode, toString and toIntendedString are generated.
Is my understanding correct that JAVA_COMMON_PRESET is responsible for the generation of these methods?
Looking at the code JAVA_COMMON_PRESET is pushed TWICE to presets. That would explain the duplication of the methods.

@Athul0491
Copy link
Contributor

Athul0491 commented Jan 19, 2024

@oliverkuntze now that i look at the PR again, i realize that you are right. I'll push the changes asap! Pretty sure the same problem exists for TS as well.

@oliverkuntze
Copy link
Author

Perfect! Thanks @Athul0491 ! We are really eager to use the feature. :-)

@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 1.4.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants