Reputation: 91
I'm generating pojo using jsonschema2pojo maven plugin. Using custom annotator, I'm including some lombok annotations (which works fine) but it also creates variables for annotations. Is there a way to force jsonschema2pojo to not create object variables for annotation and simply just have variables?
json schema
{
"title": "Person",
"type": "object",
"properties": {
"lombok-builder": true,
"lombok-data": true,
"lombok-to-string": true,
"name": {
"type": "string"
}
}
}
custom annotator
@Override
public void propertyField(JFieldVar field, JDefinedClass clazz, String property, JsonNode propertyNode) {
super.propertyField(field, clazz, property, propertyNode);
if (property.equals("lombok-builder")) {
clazz.annotate(Builder.class);
} else if (property.equals("lombok-data")) {
clazz.annotate(Data.class);
} else if (property.equals("lombok-to-string")) {
clazz.annotate(ToString.class);
}
}
Person.java generated by jsonschema2pojo
package com.package;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
/**
* Person
* <p>
*
*
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@Builder
@Getter
@Setter
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@ToString
@JsonPropertyOrder({
"lombok-builder",
"lombok-getter",
"lombok-setter",
"lombok-data",
"lombok-equals-and-hash-code",
"lombok-no-args-constructor",
"lombok-all-args-constructor",
"lombok-to-string",
"name"
})
public class Person {
@JsonProperty("lombok-builder")
public Object lombokBuilder;
@JsonProperty("lombok-data")
public Object lombokData;
@JsonProperty("lombok-to-string")
public Object lombokToString;
@JsonProperty("name")
public String name;
}
As you can see the annotations work fine but I'm getting public Object lombokToString;
, @JsonProperty("lombok-to-string")
etc. :|
Upvotes: 4
Views: 4857
Reputation: 91
After some time and research I realised I was going about it in a wrong way.
Needed to work on propertyInclusion
with additionalProperties
Below is the working annotator and example schema for anyone having similar issue.
import com.sun.codemodel.JDefinedClass;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.*;
import org.jsonschema2pojo.AbstractAnnotator;
public class LombokAnnotator extends AbstractAnnotator {
@Override
public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
JsonNode additionalProperties = schema.get("additionalProperties");
try {
additionalProperties.fieldNames().forEachRemaining(property -> {
Class annotation = getAnnotation(property);
if (!annotation.equals(IllegalArgumentException.class)) {
clazz.annotate(annotation);
}
});
} catch (NullPointerException e) {
System.out.println(String.format("No additionalProperties defined for %s.", clazz.fullName()));
}
}
@Override
public boolean isAdditionalPropertiesSupported() {
return false;
}
and updated json schema
{
"title": "Person",
"type": "object",
"additionalProperties": {
"lombok-builder": true,
"lombok-data": true,
"lombok-getter": true,
"lombok-setter": true,
"lombok-equals-and-hash-code": true,
"lombok-no-args-constructor": true,
"lombok-all-args-constructor": true,
"lombok-to-string": true
},
"properties": {
"name": {
"type": "string"
}
}
}
Upvotes: 2