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

Reference Schema

Shaun Francis edited this page Sep 20, 2017 · 6 revisions

Reference Schemas

Definitions

The POJO generator supports definitions in a schema for example:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "recordId": {
      "$ref": "#/definitions/java.util.UUID"
    },
    "payByDate": {
      "$ref": "#/definitions/java.time.ZonedDateTime"
    },
    "lumpSumAmount": {
      "$ref": "#/definitions/money"
    }
  },
  "required": [
    "recordId",
    "payByDate",
    "lumpSumAmount"
  ],
  "definitions": {
    "java.util.UUID": {
      "type": "string",
      "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
    },
    "java.time.ZonedDateTime": {
      "type": "string",
      "pattern": "^[1|2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
    },
    "money": {
      "type": "number",
      "multipleOf": 0.01
    }
  }
}

Will convert to the following Java Class:

package uk.gov.justice.pojo.complex.schema;

import java.io.Serializable;
import java.lang.String;
import java.math.BigDecimal;

public class Record implements Serializable {
  private static final long serialVersionUID = 1L;

  private final BigDecimal lumpSumAmount;

  private final String payByDate;

  private final String recordId;

  public Record(final BigDecimal lumpSumAmount, final String payByDate, final String recordId) {
    this.lumpSumAmount = lumpSumAmount;
    this.payByDate = payByDate;
    this.recordId = recordId;
  }

  public BigDecimal getLumpSumAmount() {
    return lumpSumAmount;
  }

  public String getPayByDate() {
    return payByDate;
  }

  public String getRecordId() {
    return recordId;
  }
}

Converting definition to Class Type of UUID and ZonedDateTime

By adding Type Name Plugins the generation of the Java Class can be changed to use specific or custom class types. See Plugins

A plugin is provided for Reference Schema Definitions:

  • CustomReturnTypePlugin - Adds support for using custom types as return types and constructor parameters in the generated class.

For example the same Schema above with the plugins enabled produces:

package uk.gov.justice.pojo.complex.schema;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.util.UUID;

public class Record implements Serializable {
  private static final long serialVersionUID = 1L;

  private final BigDecimal lumpSumAmount;

  private final ZonedDateTime payByDate;

  private final UUID recordId;

  public Record(final BigDecimal lumpSumAmount, final ZonedDateTime payByDate, final UUID recordId) {
    this.lumpSumAmount = lumpSumAmount;
    this.payByDate = payByDate;
    this.recordId = recordId;
  }

  public BigDecimal getLumpSumAmount() {
    return lumpSumAmount;
  }

  public ZonedDateTime getPayByDate() {
    return payByDate;
  }

  public UUID getRecordId() {
    return recordId;
  }
}