Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Included JSON Support

Wade Pearce edited this page Jan 28, 2020 · 2 revisions

The DropWizard RAML library includes support for JSON schema and example generation. This uses a combination of the Jackson libraries generally included in the DropWizard framework along with a third-party library that extends schema generation: Jackson jsonSchema Generator.

Overriding Types

If your schema always serializes a type differently (eg. serializing a BigDecimal as a string rather than a number), you can override that type by registering the mapping with the JsonSchemaFactory instance.

JsonSchemaFactory.registerRemappedType(BigDecimal.class, String.class);

This will mean the JSON schema generation will report BigDecimal as JSON type string.

This will work in conjunction with other injection annotations, such as:

@JsonSchemaInject(strings = @JsonSchemaString(path = "pattern", value = "^\\d+\\.\\d{2}$"))

Combined with the above remapping, will report a BigDecimal with the following JSON schema definition:

{
  "type": "string",
  "pattern": "^\\d+\\.\\d{2}$"
}

Defining Enum Allowed Values

Often when defining an enum in code via Jackson, the schema would ideally define the limitation of values for that particular property.

Given an enum type of:

public enum CarType {
  Sedan,
  StationWagon,
  SUV
}

Create a class that extends JsonSchemaEnumTypeSupplier.

public class CarTypeSchemaSupplier extends JsonSchemaEnumTypeSupplier<CarType> {
}

You can then add the following annotation to your enum property in your Jackson class:

@JsonSchemaInject(jsonSuppliers = JsonSchemaEnumTypeSupplier.class)

This will then update the JSON schema to report an enum property as:

{
  "type": "string",
  "enum": ["Sedan", "StationWagon", "SUV"]
}