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

Is it possible to add custom annotations on a generated class? #1452

Closed
giovanniberti opened this issue Dec 6, 2022 · 2 comments
Closed

Comments

@giovanniberti
Copy link

No description provided.

@unkish
Copy link
Collaborator

unkish commented Feb 12, 2023

Hi

It's not possible to do that with configuration options, there is a PR proposing adding annotations with via configuration options (which has drawbacks/limitations).
There are however several possibilities how that could theoretically be achieved:

  • by using/piggyback'ing customAnnotator such that would override public void typeInfo(JDefinedClass clazz, JsonNode schema) or usage of CompositeAnnotator (if generating pojo's programmatically).

NB! This solution won't annotate enum's

    public class CustomAnnotator extends Jackson2Annotator {

        public CustomAnnotator(GenerationConfig generationConfig) {
            super(generationConfig);
        }

        @Override
        public void typeInfo(JDefinedClass clazz, JsonNode schema) {
            // add custom annotations
            clazz.annotate(Deprecated.class);
            // delegate the rest to super class
            super.propertyInclusion(clazz, schema);
        }
    }
  • by using/providing customRuleFactory which would return customized (extended) ObjectRule and EnumRule capable of adding desired annotations.

@giovanniberti
Copy link
Author

Thanks! :)

@unkish unkish closed this as completed Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants