Repository navigation
Schema Format
The built-in format is Simple Schema - a slightly altered JSON Schema. One file describes one top-level type.
- Extension:
.json - Location: anywhere under
src/main/model(scanned recursively) - File name: exactly
<title>.json, e.g.com.example.dto.Person.json
See also Property Types, Property Naming and Feature Configuration.
-
$modelmaker(required) - schema version. Only"v1.0"is supported. -
title(required) - fully-qualified class name, e.g."com.example.dto.Person". Must contain a., and the file must be named<title>.json. -
type(required) - must be"object". -
properties(required) - object of property name to property schema. -
required- array of property names that must always be set. -
description- emitted as@Schema(description = ...)on the class whenopenApiis on. -
features- per-file override of the project-wide feature flags, see Feature Configuration.
Unknown top-level keys are ignored; unknown keys inside features are rejected.
{
"$modelmaker": "v1.0",
"title": "com.example.dto.Person",
"description": "A person, as exposed by the public API.",
"type": "object",
"required": ["id", "email"],
"properties": {
"id": { "type": "string" },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "default": 0 }
}
}Each entry under properties is a small schema of its own. It must carry either a "type" or a "$ref" - a property
with neither is rejected.
-
type- one ofstring,bytes,integer,number,boolean,object,array. See Property Types. -
$ref- another schema'stitle, a nested type name, or a JDK type. Mutually exclusive withtype. -
format- oninteger/numberit widens the Java type; onstringit adds a@Patternor@Email, see Validation. -
items- the element schema of anarray. Required for arrays. -
title- names an inline nested class. Plain class name, no package. -
description-@Schema(description = ...)on the getter or field whenopenApiis on. -
example-@Schema(example = "...")for a scalar, always rendered as a string. -
default- value applied when an optional property is left unset. See below. -
sensitive-truemasks the value in the generatedtoString(). See below. -
Validation keywords -
pattern,enum,minLength,maxLength,minimum,maximum,minItems,maxItems. See Validation.
A property listed in the top-level required array is non-null: the field has no @Nullable, and
Builder.build() throws NullPointerException (via Objects.requireNonNull) when it was never set.
Everything else is optional and carries JSpecify's @Nullable on the field, the constructor parameter, the getter
return and the builder setter.
{
"required": ["id"],
"properties": {
"id": { "type": "string" },
"nickname": { "type": "string" }
}
}private final String id; // non-null
private final @Nullable String nickname;"default" gives an optional property a value used when it was left unset. The default is applied in the
constructor, which means it also applies to a Jackson-deserialized instance where the JSON omitted the property:
"loyaltyPoints": { "type": "integer", "minimum": 0, "default": 0 }this.loyaltyPoints = loyaltyPoints != null ? loyaltyPoints : 0;Consequences worth knowing:
- A property with a
defaultis effectively never null, so it is treated as non-null for nullability and for@NotNullemission - the field isInteger, not@Nullable Integer. - With
preferPrimitiveson, such a property becomesintrather thanInteger. - A
defaulton a required property is meaningless and ignored. - The default's JSON type must match the property type: a string default for
"type": "string", an array default for"type": "array", and so on. A mismatch fails the build. -
"bytes"does not supportdefault.
A property marked "sensitive": true has its value replaced with *** in the generated toString() - both the
DTO's and its builder's:
"password": { "type": "string", "sensitive": true }return "Account["
+ "id=" + id
+ ", password=" + "***"
+ "]";Nothing else changes: the getter returns the real value, and equals / hashCode still compare it. The point is
narrow - DTOs end up in logs, and a logged toString() should not carry a password or a token.
Two ways to reach another type:
"shippingAddress": { "$ref": "com.example.dto.Address" }references a separate schema file in the same source tree - it must exist, or the build fails.
"contact": {
"type": "object",
"title": "Contact",
"required": ["email"],
"properties": { "email": { "type": "string" } }
}declares an inline nested class rendered as a public static final class Contact inside the owning type. Without
"title", the class is named after the capitalized property name.
Details, including JDK $ref targets such as java.time.Instant, are on Property Types.
{
"$modelmaker": "v1.0",
"title": "com.example.dto.Person",
"type": "object",
"properties": { "id": { "type": "string" } },
"features": {
"jackson": { "enabled": true },
"validation": { "enabled": true, "annotateFields": true, "annotateGetters": false }
}
}Any key left out keeps the project-wide value. Full precedence rules and the shape of each entry are on Feature Configuration.
Schemas are parsed and cross-checked before anything is written. Rejections include a missing or unsupported
$modelmaker, a file name that does not match title, a top-level type other than "object", missing
properties, an unsupported property type, an array without items, nested arrays, a default of the wrong JSON
type, an unresolvable $ref, an unknown features entry, and the naming clashes described on
Property Naming.
The exact messages are listed on Troubleshooting.
ModelMaker - Apache License 2.0 - Issues - Wiki index
Start here
Schemas
Output
Features
Integration
Reference