openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

How could I add custom annotation @Generator or other annotation to all class in Java

Open akash33 opened this issue 5 years ago • 5 comments

Description

I want to add custom annotation to all classes that is generated by openapi. Something like @Generated so Jacoco can exclude those classes. Currently generator does support adding annotation to model classes but I want to add it all the classes. I do see @javax.annotation.Generated on all classes but the retention policy is SCOPE so jacoco is not able to exclude those classes from the report. Is there any functionality like this additionalModelTypeAnnotations: '@lombok.Generated' that supports adding annotations to all classes?

akash33 avatar Jun 11 '21 14:06 akash33

I second this. Another use case is adding @SuppressWarnings annotations since the generated classes usually lead to lots of code warnings difficult to deal with in projects with zero warnings policy.

mbert avatar Jul 28 '21 10:07 mbert

I also think this feature would be extremely useful and valuable.

  • The generated code causes a lot of warnings. Add @SuppressWarnings("all") would make these warnings go away. I think there is a strong argument to just also include this annotation, but an option to add this specific annotation or to add custom annotations would also work.
  • There would also be value in having the ability to include @lombok.Generated on classes. This could be an option to add this specific annotation, or an option to add custom annotations

baincd avatar Aug 08 '21 13:08 baincd

Any updates on this? We are facing the same issue.

TomasLukac avatar Apr 04 '24 07:04 TomasLukac

Yep. I'd also be interested in a solution.

MarvinVaillant avatar Feb 21 '25 14:02 MarvinVaillant

For the record, there is a workaround which isn't pretty but does the job: maven-replacer-plugin. In order to add annotations (in this case: suppress warnings) you can proceed as follows:

<execution>
  <phase>process-sources</phase>
  <goals>
    <goal>replace</goal>
  </goals>
  <configuration>
    <filesToInclude>${project.build.directory}/generated-sources/**/*.java</filesToInclude>
    <replacements>
      <replacement>
        <!-- First remove existing @SuppressWarnings annotations in order to avoid duplicating them.-->
        <token>(@SuppressWarnings|@edu.umd.cs.findbugs.annotations.SuppressFBWarnings).*</token>
        <value></value>
      </replacement>
      <replacement>
        <!-- Now insert our new annotations.-->
        <!-- Note that we should really have a '^' here, but it does not work, see bug #98 (https://code.google.com/archive/p/maven-replacer-plugin/issues/
98).-->
        <token>public (class|enum|interface) </token>
        <value>@SuppressWarnings({ your-list-of-suppressions-here })
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings({ your-list-of-suppressions-here })
public $1 </value>
      </replacement>
    </replacements>
  </configuration>
</execution>

I think we all agree that an "official way" to do this would be better, but at least this gets the job done.

mbert avatar Feb 21 '25 14:02 mbert