Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Provided Plugins

Shaun Francis edited this page Oct 3, 2017 · 13 revisions

ClassModifyingPlugin

AddFieldsAndMethodsToClassPlugin

Plugin that adds fields, a constructor and get methods to a class's Type Specification. Without this plugin each class will be generated with no fields nor get methods and with an empty constructor. For this reason this class is added by default; although it is possible to override it should you need to change it's behavior.

GenerateBuilderForClassPlugin

Adds a builder for the class as an static inner class and a static method for accessing the builder. For example, a class MyClass specified with one property myProperty would be generated thusly:

  public class MyClass {

       private final String myProperty;

       public MyClass(final String myProperty) {
           this.myProperty = myProperty;
       }

       public String getMyProperty() {
           return myProperty;
       }

       public static Builder myClass() {
           return new MyClass.Builder();
       }

       public static class Builder {

           private String myProperty;

           public Builder withMyProperty(final String myProperty) {
               this.myProperty = myProperty;
               return this;
           }

           public MyClass build() {
               return new MyClass(myProperty);
           }
       }
   }

AddAdditionalPropertiesToClassPlugin

This plugin allows pojos to support additional properties in your json file. The json schema document has the property 'additionalProperties' on an Object.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "myProperty": {
    "type": "string"
  },
  "additionalProperties": true
}

If this is set to true then it would be possible to have a json document that fulfils the schema, but would fail to be parsed into the java pojo. This plugin adds a java.util.Map as a field in your pojo with getters and setters for access. This is then parsable to and from json:

public class MyPojo {

    private final UUID myProperty;
    private final Map<String, Object> additionalProperties = new HashMap<>();

    public MyPojo(final UUID myProperty) {
        this.myProperty = myProperty;
    }

    public UUID getMyProperty() {
        return myProperty;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(final String name, final Object value) {
        additionalProperties.put(name, value);
    }
}

However, as you can see, to parse this into java pojos requires the Jackson annotations @JsonAnySetter and @JsonAnyGetter

AddHashcodeAndEqualsPlugin

Add java Object.equals(Object) and Object.hashCode() to the generated class.
The hashCode and equals methods use Objects.hashCode(Object) and Objects.equals(Object, Object) under the hood.

NB: this plugin will not add Object.equals(Object) nor Object.hashCode() if the class definition contains no fields

MakeClassSerializablePlugin

A class plugin that adds Serializable interface and serialVersionUID to the generated class.

Note: the serialVersionUID is always set to 1.

For example:

  public class MyClass implements Serializable {

       private static final long serialVersionUID = 1L;

       private final String myProperty;

       public MyClass(final String myProperty) {
           this.myProperty = myProperty;
       }

       public String getMyProperty() {
           return myProperty;
       }
  }

TypeModifyingPlugin

SupportJavaOptionalsPlugin

A type modifying plugin for setting all properties which are not marked as required in the json schema document to be wrapped in a java Optional.

For example:

  public class MyClass {

       private final Optional<String> myProperty;

       public MyClass(final Optional<String> myProperty) {
           this.myProperty = myProperty;
       }

       public Optional<String> getMyProperty() {
           return myProperty;
       }
  }

ReferenceCustomReturnTypePlugin

Adds support for using reference custom java types as return types and constructor parameters in the generated class.

To Use: Set the typeMappings tag in the generatorProperties tag. The tag of the type mapping corresponds to the name of the definition being referenced in the schema, the tag is the Java Class type to be used when generating the Java POJO, and the tag defines this as a reference mapping type. For example:

     <typeMappings>
          <typeMapping>
              <name>date</name>
              <type>reference</type>
              <implementation>java.time.ZonedDateTime</implementation>
          </typeMapping>
     </typeMappings>

The name value of "date" will be the definition name and is referenced in your json schema file:

     "myProperty": {
         "$ref": "#/definitions/date"
     },
     "definitions": {
         "date": {
             "type": "string",
             "pattern": "^[1|2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
         }
     }

This will generate the following code:

     public class MyClass {

         private final ZonedDateTime myProperty;

         public MyClass(final ZonedDateTime myProperty) {
             this.myProperty = myProperty;
         }

         public ZonedDateTime getMyProperty() {
             return myProperty;
         }
     }

FormatCustomReturnTypePlugin

Adds support for using format custom java types as return types and constructor parameters in the generated class.

To Use: Set the typeMappings tag in the generatorProperties tag. The tag of the type mapping corresponds to the format value of a string field in the schema, the tag is the Java Class type to be used when generating the Java POJO, and the tag defines this as a format mapping type. For example:

     <typeMappings>
         <typeMapping>
             <name>date-time</name>
             <type>format</type>
             <implementation>java.time.ZonedDateTime</implementation>
         </typeMapping>
     </typeMappings>

The name value of "date" will be the definition name and is referenced in your json schema file:

     "myProperty": {
         "type": "string",
         "format": "date-time"
     }

This will generate the following code:

     public class MyClass {

         private final ZonedDateTime myProperty;

         public MyClass(final ZonedDateTime myProperty) {
             this.myProperty = myProperty;
         }

         public ZonedDateTime getMyProperty() {
             return myProperty;
         }
     }

NameGeneratablePlugin

RootNameGeneratorPlugin

Generate the root class name from a schema filename.

The filename extension is removed, any prepending '.' are removed from the filename and then all '-' are removed and the first letter after each '-' is capitalized.

For example:

context.something-has-happened.json

becomes:

somethingHasHappened