joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc

Home Page:http://www.jsonschema2pojo.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trying to make Custom annotations work (unsuccessfully) using jsonschema2pojo directly from code (embedded) using jsonschema2pojo-core. Is that doable?

ArvindRavishankar opened this issue · comments

Hi, @joelittlejohn

I am trying to use jsonschema2pojo directly from code (embedded) using jsonschema2pojo-core

Based on details provided in the issue #722 (comment) I understand how to make custom annotations work when using the maven plugin approach. I followed the suggestion provided here and it works.

However, I was hoping to make it work while using the embdedded approach.

I added the below in my GenerationConfig

@Override public Class<? extends Annotator> getCustomAnnotator() { return (Class<? extends Annotator>) Class.forName("io.github.paulushcgcj.js2pojo.LombokAnnotator"); }

Added the maven dependency for LombokAnnotator in my pom.xml
<dependency> <groupId>io.github.paulushcgcj</groupId> <artifactId>helper-js2pojo</artifactId> <version>1.0.1</version> </dependency>

When I try to generate POJOs using code I'm getting the below exception/error

Exception in thread "main" java.lang.NoClassDefFoundError: lombok/Data at io.github.paulushcgcj.js2pojo.LombokAnnotator.propertyInclusion(LombokAnnotator.java:45) at org.jsonschema2pojo.CompositeAnnotator.propertyInclusion(CompositeAnnotator.java:63) at org.jsonschema2pojo.rules.ObjectRule.createClass(ObjectRule.java:259) at org.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:94) at org.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:65) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:86) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:41) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:83) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:76) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:38) at org.jsonschema2pojo.SchemaMapper.generate(SchemaMapper.java:90) at org.jsonschema2pojo.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:79) at com.bnymellon.custody.utils.generation.experiment.automated.schemageneration.helpers.InternalUtils.generateJavaSourceFiles(InternalUtils.java:333) at com.bnymellon.custody.utils.generation.experiment.automated.schemageneration.CanonicalSchemaGenerator.generate(CanonicalSchemaGenerator.java:97) at com.bnymellon.custody.utils.generation.experiment.automated.schemageneration.Application.main(Application.java:15) Caused by: java.lang.ClassNotFoundException: lombok.Data at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 15 more

Am I doing something wrong?

Or custom annotations will not work when using (embedded) jsonschema2pojo-core approach?

Hi. Do you just need to add Lombok to the classpath? Is it missing from your project dependencies?

Another option: I believe with CodeModel it is also possible to add an annotation without referring to the actual class.

hi, thank you very much for the quick response.

Reg Lombok - adding it to the classpath resulted in the annotations getting added successfully with the embedded approach. That said, I also did explore the CodeModel approach you suggested above. Once the CodeModel is updated with the packages and classes, I was able to add the annotation on the generated JDefinedClass classes. It worked as well.

I am now able to get the Custom annotations work successfully using the embedded approach.