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][JavaSpring] wrong parsing of space separated lombok annotations #18868

Open
4 of 5 tasks
gunnarkessler opened this issue Jun 5, 2024 · 4 comments
Open
4 of 5 tasks

Comments

@gunnarkessler
Copy link

gunnarkessler commented Jun 5, 2024

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?
Description

Starting with Version 7.6.0 the lombok Annotation @NoArgsConstructor collides with a generated default constructor since the pojo.mustache for JavaSpring was changed with #18650 and the inverted Section {{^lombok.NoArgsConstructor}} surrounding the template for the default constructor was removed. The PR and the Release Notes seem to not contain any infos regarding this change, which led me to the hypothesis this change might have been unintentional.

actual output: a pojo with a default constructor and the lombok annotation @NoArgsConstructor added that does not compile
expected output: a pojo with no default constructor and the lombok annotation @NoArgsConstructor added that does compile

openapi-generator version

7.6.0

OpenAPI declaration file content or url

Issue does not seem to be dependent on details of YAML

Generation Details

openapi-maven-generator-plugin version 7.6.0 was used to generate - relevant to the issue at hand is adding @lombok.NoArgsConstructor via <additionalModelTypeAnnotations>.

<configuration>
    <inputSpec>${kontrakte.dir}/contract.yaml</inputSpec>
    <generatorName>spring</generatorName>
    <generateApis>true</generateApis>
    <generateModels>true</generateModels>
    <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
    <generateModelDocumentation>false</generateModelDocumentation>
    <generateApiDocumentation>false</generateApiDocumentation>
    <generateApiTests>false</generateApiTests>
    <generateModelTests>false</generateModelTests>
    <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
    <configOptions>
        <useSpringBoot3>true</useSpringBoot3>
        <useTags>true</useTags>
        <sourceFolder>src/gen/java</sourceFolder>
        <serializableModel>true</serializableModel>
        <dateLibrary>java8-localdatetime</dateLibrary>
        <delegatePattern>true</delegatePattern>
        <!--suppress UnresolvedMavenProperty -->
        <additionalModelTypeAnnotations>
            @lombok.Builder @lombok.NoArgsConstructor @lombok.AllArgsConstructor
        </additionalModelTypeAnnotations>
        <generatedConstructorWithRequiredArgs>false</generatedConstructorWithRequiredArgs>
    </configOptions>
</configuration>
Steps to reproduce

Use openapi-generator-maven-plugin in version 7.6.0 with the above to generate artifacts. The generated Pojo does not compile.

After downgrading the plugin to version 7.5.0 the problem is resolved. No default constructor is generated that collides with the Lombok-Annotation. The generated Pojo compiles.

Related issues/PRs

I checked all issues created since the release of version 7.6.0 and did not find any already adressing this problem.

Suggest a fix

Re-adding the removed inverted section {{^lombok.NoArgsConstructor}} around the template for the default constructor should resolve this issue.

@jpfinne
Copy link
Contributor

jpfinne commented Jun 7, 2024

@gunnarkessler There is no such {{^lombok.NoArgsConstructor} in pojo.mustache in version 7.6.0. Do you use another version or do you have custom templates?

{{^lombok.NoArgsConstructor} was removed in favor of a more robust {{#vendorExtensions.x-java-no-args-constructor}} computed in java in SpringCodeGen.postProcessAllModels(Map<String, ModelsMap> objs)

To generate a no-arg-constructor, the spring generator needs to check the combination of

  • fields present
  • required fields present with generatedConstructorWithRequiredArgs=true
  • required fields present with generatedConstructorWithRequiredArgs=false (maybe not done in 7.6.0. I will check)
  • additionalProperties in the contract
  • generateConstructorWithAllArgs flag
  • generatedConstructorWithRequiredArgs
  • lombok dedicated handling introduced in templates add lombok model support on spring #17622
  • additionalModelTypeAnnotations option with different combination of lombok annotations

This is nearly impossible to achieve in straight mustache conditions.

It seems your usage of lombok is to create a builder.
For that purpose you can use the new generateBuilders option and remove your additionalModelTypeAnnotations

@gunnarkessler
Copy link
Author

There is no such {{^lombok.NoArgsConstructor} in pojo.mustache in version 7.6.0. Do you use another version or do you have custom templates?

Sorry, if I put it ambiguously. I was trying to say, that the above section was still present in 7.5.0. and removed with 7.6.0

Thank you for the pointer to SpringCodeGen.postProcessAllModels(Map<String, ModelsMap> objs). I was only looking at the mustache-template.

I am currently performing a spring boot upgrade of an application consuming numerous extrenal REST-services and therefore also updating the generator used and a lot of external dependencies.

Our application stack expects objects to be deserialized to have a default constructor. Before upgrading the generator all generated pojos contained no default constructor but the aforementioned lombok annotation, after the upgrade pojos for contract objects with required fields hat explicit default constructors generated, and objects without required fields had none. I was looking for a way to generate the pojos with the same structure as before the update.

I had already stumbled upon vendorExtensions.x-java-no-args-constructor but I have not yet understood, how and where to use this option.

@jpfinne
Copy link
Contributor

jpfinne commented Jun 8, 2024

@gunnarkessler The root cause of the bug is the parsing of ADDITIONAL_MODEL_TYPE_ANNOTATIONS and the subsequent parsing of lombok annotations
The parsing of ADDITIONAL_MODEL_TYPE_ANNOTATIONS is done with a split using this regex
"\\s*(;|\\r?\\n)\\s*"
This code exists for years, so I would not consider that as an issue.
You use space instead of semi column in your configuration. So the result is additionalModelTypeAnnotations with one row.

The lombok annotion parsing is done with this regex:
"@lombok.(\\w+\\.)*(?<ClassName>\\w+)(\\(.*?\\))?"
The result is that lombokAnnotations contains only one row: @lombok.Builder
This is an additional bug introduced by the lombok handling. See #17793 for similar issues.

So the handling in postProcessAllModels() does not know about NoArgsConstructor.

The fact that it worked in 7.5.0 and before is probably pure luck.

Workaround for you: use semicolon separated values:
@lombok.Builder;@lombok.NoArgsConstructor;@lombok.AllArgsConstructor

Can you rename your bug report to reflect the finding? Like "wrong parsing of space separated lombok annotations"

@gunnarkessler gunnarkessler changed the title [BUG][JavaSpring] @NoArgsConstructor collides with generated default constructor [BUG][JavaSpring] wrong parsing of space separated lombok annotations Jun 8, 2024
@gunnarkessler
Copy link
Author

Thank you for the explanation and all the infos. I will look into the workaround.

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

3 participants