On this page
JSON Schema
JSON Schema is a human-readable format for exchanging data that also enables JSON data consistency, validity and interoperability.
Micronaut JSON Schema assists transforming schemas to beans and beans to schemas in your applications.
In order to create JSON Schema from beans, add Micronaut JSON Schema processor to the annotation processor scope of your build configuration,
annotationProcessor("io.micronaut.jsonschema:micronaut-json-schema-processor")and the Micronaut JSON Schema Annotations dependency to compile classpath: dependency::micronaut-json-schema-annotations[groupId="io.micronaut.jsonschema"]
In order to create beans from JSON Schema, add Micronaut JSON Schema generator dependency to compile classpath: dependency::micronaut-json-schema-generator[groupId="io.micronaut.jsonschema"]
Annotate a bean with JsonSchema to trigger the creation of a schema for it during build time:
The following file will be created on the classpath: META-INF/schemas/llama.schema.json.
{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$id":"http://localhost:8080/schemas/llama.schema.json",
"title":"Llama",
"description":"A llama. ",
"type":"object",
"properties":{
"age":{
"description":"The age",
"type":"integer",
"minimum":0
},
"name":{
"description":"The name",
"type":"string",
"minLength":1
}
},
"required":["age"]
}It can be used in your application and will be included in the jar file.
This section explains the processes involved for creating JSON schema from Beans.
The generation can be configured globally with annotation processor options:
// For Java
tasks.withType(JavaCompile).configureEach {
options.compilerArgs.add("-Amicronaut.jsonschema.baseUri=https://example.com/schemas") // (1)
}
// For Groovy
tasks.withType(GroovyCompile).configureEach {
options.compilerArgs.add("-Amicronaut.jsonschema.baseUri=https://example.com/schemas") // (1)
}
// For KSP
ksp {
arg("micronaut.jsonschema.baseUri", "https://example.com/schemas") // (1)
}
// For Kapt
kapt {
arguments {
arg("micronaut.jsonschema.baseUri", "https://example.com/schemas") // (1)
}
}
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-Amicronaut.jsonschema.baseUri=https://example.com/schemas</arg> <!-- (1) -->
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
-
Set the base URL for all the schemas. It will be prepended to all relative schema URLs.
With the previous configuration, the following file will be created on the classpath: META-INF/schemas/llama.schema.json.
{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$id":"https://example.com/schemas/llama.schema.json",
"title":"Llama",
"description":"A llama. ",
"type":"object",
"properties":{
"age":{
"description":"The age",
"type":"integer",
"minimum":0
},
"name":{
"description":"The name",
"type":"string",
"minLength":1
}
}
}All the supported options are:
| Option | Description |
|---|---|
|
Set the base URL for all the schemas. It will be prepended to all relative schema URLs. |
|
The location where JSON schemas will be generated inside the build |
|
Whether to encode byte array as a JSON array. The default and preferred behavior is to encode it as a Base64-encoded string. |
|
Specify the JSON Schema draft versions. Currently only |
|
Whether to generate schemas in strict mode. In strict mode unresolved properties in JSON will cause an error. All the properties that are not annotated as nullable must be non-null. |
Schema generation can be configured with properties of the JsonSchema annotation, for example:
This will affect the file name as well as the id by which this schema can be referenced.
For the previous class, the following file will be created on the classpath: META-INF/schemas/red-winged-blackbird.schema.json.
{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$id":"http://localhost:8080/schemas/red-winged-blackbird.schema.json",
"title":"RedWingedBlackbird",
"description":"A species of blackbird with red wings",
"type":"object",
"properties":{
"name":{
"description":"The name",
"type":"string"
},
"wingSpan":{
"description":"The wing span of the bird",
"type":"number"
}
}
}To expose the generated JSON Schema output from your running application, add static resources configuration.
If you want to load the generated JSON Schema, you can inject a bean of type JsonSchemaClassPathResourceLoader. You need the following dependency:
implementation("io.micronaut.jsonschema:micronaut-json-schema-utils")If you want to validate a JSON file against your generated JSON Schema, you can inject a bean of type JsonSchemaValidator. You need the following dependency:
implementation("io.micronaut.jsonschema:micronaut-json-schema-validation")Information for JSON schema is aggregated from multiple sources.
JavaDoc on types and properties will be added to the description properties of schemas. This includes class, property descriptions and record parameter descriptions.
The following jakarta.validation.constraints annotations are supported:
| Validation Annotations | Supported | Comment |
|---|---|---|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
❌ |
JSON schema does not define fields for validating date-time formats |
|
❌ |
JSON schema does not define fields for validating date-time formats |
|
❌ |
JSON schema does not define fields for validating date-time formats |
|
❌ |
JSON schema does not define fields for validating date-time formats |
By default, properties are not nullable. jakarta.annotations.Nullable can be added to make them nullable.
Note, that validation might not correspond to actual bean values, as by default null
values are completely omitted during JSON serialization.
|
Note
|
Custom validators cannot be supported, as this information is implementation-specific and not available during build time. |
The following com.fasterxml.jackson.annotation annotations are supported:
| Jackson Annotations | Supported | Comment |
|---|---|---|
|
✅ |
The annotation has no effect |
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
The annotation has no effect |
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
The annotation has no effect |
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
Cannot be supported* |
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
Note
|
*Custom serializers and deserializers cannot be supported, as this information is implementation-specific
and not available during build time. This also applies to some other features, like the JsonFilter annotation
which allows defining custom filters.
|
This section explains the processes involved for generating source code of beans from JSON schema.
|
Note
|
The Generator module can also be directly used with the Micronaut’s Gradle and Maven Plugins. Please check out their respective documentations. |
All the supported configuration options are:
| Option | Description |
|---|---|
|
Set the language of generation for the beans. This is an optional field. Defaults to JAVA. Micronaut SourceGen module is used for generation and thus available generation languages are given here. |
|
Classpath is taken from the gradle configurations for the task. This is a mandatory field. |
|
Loads a valid JSON schema from the given input URL. This is an optional field. |
|
Loads a valid JSON schema from the given input File. This is an optional field. |
|
Loads all valid JSON schemas from the given input folder. This is an optional field. |
|
The path where source files are generated. This is a mandatory field. |
|
The package name of the generated source files. This is an optional field. |
|
The file name of the generated source file. This field is taken into account when there is only one bean generated. If not specified, schema’s title or schema file’s name is considered for the generated file. This is an optional field. |
|
A String list that has allowed URL patterns that are accepted for the references inside the schema. This is an optional field. The default pattern is |
-
It is important to note that all three input options (
jsonURL,jsonFile, andinputDirectory) are optional but at least one must be stated. In case multiple inputs are given, only one will be accepted following the order:inputDirectory,jsonURL, andjsonFile.
The source generation from JSON schema can also be used during run-time with the generator package. The following code snippet shows how SourceGenerator object can be used to generate beans of your desired language from JSON schema.
The SourceGeneratorConfigBuilder methods are similar to that of `BeanGeneratorTask’s configuration explained in the previous section. There are explained below:
| Option | Description |
|---|---|
|
Loads a valid JSON schema from given an InputStream. |
|
Loads a valid JSON schema from the given input URL. |
|
Loads a valid JSON schema from the given input File. |
|
Loads all valid JSON schemas from the given input folder. |
|
The path where source files are generated. |
|
The package name of the generated source files. |
|
The file name of the generated source file. This field is taken into account when there is only one bean generated. If not specified, schema’s title or schema file’s name is considered for the generated file. |
-
It is important to note that one of input options (
inputStream,jsonURL,jsonFile, andinputFile) must be stated. In case multiple inputs are given, only one will be accepted following the order:inputFolder,inputStream,jsonURL, andjsonFile.
The following JSON schema keywords are processed by the generation:
| JSON Schema keyword | Supported | Notes or Limitations |
|---|---|---|
|
✅ |
|
|
✅ |
|
|
✅ |
Accepted type keywords are: |
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
❌ |
|
|
✅ |
Self, local, remote, and url references are accepted. However, for remote references, the generation needs to be given the top local input directory during configuration. |
|
❌ |
|
|
✅ |
The |
|
✅ |
Teh closest existing class is decided as the type of object when the format is given. For example, for a |
|
✅ |
|
|
✅ |
|
|
✅ |
|
|
✅ |
Only a top level |
|
✅ |
|
|
✅ |
The strategy to choose from an anyOf keyword: if empty, return null; if single schema, return that schema; if there are two schema but one has type "NULL", return the non-null schema; if all schemas has the same type, merge all schemas and return; else return empty Object schema. |
|
✅ |
These keywords are not actually part of JSON schema, but is commonly used (with |
|
✅ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
Even though default values are generally discarded, they are supported in |
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
|
❌ |
|
Note
|
The source code generation decides which object type to use for representing the schema depending on the schema’s values. An interface is created to represent a oneOf relation. An enum class is created when the enum keyword is used. A class object is created when it belongs to an inheritance, has too many properties for a record (255+), has const properties, or has additional properties. In the remaining cases of an object with properties, a record is created.
|
The validation conditions are kept on the generated beans through jakarta.validation.constraints annotations. Below is a list of JSON schema validation keywords that are supported and their corresponding annotations:
| Validation keywords | Supported | Annotation equivalent | Notes |
|---|---|---|---|
|
✅ |
|
|
|
❌ |
||
|
✅ |
|
|
|
❌ |
||
|
✅ |
|
Depends on whether the type is an integer or a floating point number. |
|
✅ |
|
Depends on whether the type is an integer or a floating point number. |
|
✅ |
|
Depends on whether the type is an integer or a floating point number. |
|
✅ |
|
Depends on whether the type is an integer or a floating point number. |
|
✅ |
|
|
|
✅ |
|
|
|
✅ |
|
|
|
✅ |
|
|
|
✅ |
When true, generates a Set object instead of the default List object. |
|
|
✅ |
|
|
|
✅ |
|
|
|
✅ |
This keyword is processed only when the |
|
|
✅ |
|
Supported fully with strings and has limited support with numerical types. Pattern information on numerical types can help decide on whether the value is positive, zero, and/or negative. |
|
✅ |
|
|
|
❌ |
||
|
❌ |
||
|
❌ |
||
|
❌ |
The following file is an example JSON schema describing an Animal interface which is implemented by Dog, Cat, Fish and Human objects.
{
"$schema":"https://json-schema.org/draft/2020-12/schema",
"$id":"https://example.com/schemas/llama.schema.json",
"title":"Animal",
"description":"An animal.",
"type":["object"],
"properties":{
"id": {
"description": "Unique id for the animal.",
"$ref": "#/definitions/id"
},
"birthdate":{
"description":"The birthdate",
"$ref": "#/definitions/date"
},
"name":{
"description":"The name",
"type":"string",
"minLength":1
}
},
"discriminator": {
"propertyName": "resourceType",
"mapping": {
"Cat": "#/definitions/Cat",
"Dog": "#/definitions/Dog",
"Fish": "#/definitions/Fish",
"Human": "#/oneOf/Human"
}
},
"oneOf": [{
"$ref": "#/definitions/Cat"
},{
"$ref": "#/definitions/Dog"
},{
"$ref": "#/definitions/Fish"
}, {
"title": "Human",
"type": "object",
"properties": {
"resourceType": {
"const": "Human"
},
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"sport": {
"type": "string"
}
}
}],
"$defs": {
"id": {
"pattern": "^[A-Za-z0-9\\-\\.]{1,64}$",
"type": "string",
"description": "Any combination of letters, numerals, \"-\" and \".\", with a length limit of 64 characters. (This might be an integer, an unprefixed OID, UUID or any other identifier pattern that meets these constraints.) Ids are case-insensitive."
},
"date": {
"pattern": "^([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1]))?)?$",
"type": "string",
"description": "A date or partial date (e.g. just year or year + month). There is no UTC offset. The format is a union of the schema types gYear, gYearMonth and date. Dates SHALL be valid dates."
},
"boolean": {
"pattern": "^true|false$",
"type": "boolean",
"description": "Value of \"true\" or \"false\""
},
"Cat": {
"description": "The definition of a cat.",
"properties": {
"resourceType": {
"description": "This is a Cat resource.",
"const": "Cat"
},
"hasMate": {
"description": "True when the cat has found their mate.",
"$ref": "#/definitions/boolean"
}
},
"type": ["object"],
"additionalProperties": false
},
"Dog": {
"description": "The definition of a dog.",
"properties": {
"resourceType": {
"description": "This is a Dog resource.",
"const": "Dog"
},
"hasMate": {
"description": "True when the dog has found their mate.",
"const": true
},
"nickname" : {
"description": "A nickname of a good doggo.",
"type": "string"
},
"enemies" : {
"description": "A list of the dog's cat enemies.",
"type": "array",
"items": {
"$ref": "#/definitions/Cat"
}
}
},
"type": "object",
"additionalProperties": {"type": "string"}
},
"Fish": {
"description": "The definition of a fish.",
"properties": {
"resourceType": {
"description": "This is a Fish resource.",
"const": "Fish"
},
"friends" : {
"description": "A list of the fish's aquarium friends.",
"type": "array",
"items": {
"$ref": "#"
}
}
},
"type": "object",
"additionalProperties": true
}
}
}The following example shows how the JsonMapper can map the JSON data to the best fitting/correct class in the inheritance relation even though only the interface is given to the function.
@Test
public void mapCat() throws JsonProcessingException {
var animal = jsonMapper.readValue("""
{
"id": "0x",
"birthdate": "2000-01-01",
"name": "Micronaut",
"resourceType": "Cat",
"hasMate": true
}
""", Animal.class);
assertEquals(Cat.class, animal.getClass());
Cat cat = (Cat) animal;
assertEquals("0x", cat.getId());
assertEquals("2000-01-01", cat.getBirthdate());
assertEquals("Micronaut", cat.getName());
assertEquals(true, cat.getHasMate());
assertEquals("Cat", cat.resourceType);
}The following example showcases that the generated beans can be re-serialized into the same JSON Schema.
@Test
void testSerializeGeneratedAdditionalProperty(ObjectMapper objectMapper) throws IOException {
String inputData = """
{
"resourceType": "Dog",
"id": "0x",
"birthdate": "2000-01-01",
"name": "Micronaut",
"nickname": "Goodie",
"enemies": [
{
"resourceType": "Cat",
"id": "1x",
"birthdate": "2000-01-01",
"name": "Pegasus",
"hasMate": false
}, {
"resourceType": "Cat",
"id": "2x",
"birthdate": "2000-01-01",
"name": "Micro-Pego",
"hasMate": false
}],
"hasMate": true,
"ownerName": "Owner"
}
""".replaceAll("\\s", "");
var dog = jsonMapper.readValue(inputData, Dog.class);
String result = objectMapper.writeValueAsString(dog);
assertEquals(inputData, result);
}You can find the source code of this project in this repository:
For this project, you can find a list of releases (with release notes) here: