Skip to content

Schema Format

Damian Malczewski edited this page Sep 10, 2026 · 1 revision

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.

Top-level keys

  • $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 when openApi is 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 }
  }
}

Property keys

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 of string, bytes, integer, number, boolean, object, array. See Property Types.
  • $ref - another schema's title, a nested type name, or a JDK type. Mutually exclusive with type.
  • format - on integer / number it widens the Java type; on string it adds a @Pattern or @Email, see Validation.
  • items - the element schema of an array. Required for arrays.
  • title - names an inline nested class. Plain class name, no package.
  • description - @Schema(description = ...) on the getter or field when openApi is 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 - true masks the value in the generated toString(). See below.
  • Validation keywords - pattern, enum, minLength, maxLength, minimum, maximum, minItems, maxItems. See Validation.

Required and optional

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;

Defaults

"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 default is effectively never null, so it is treated as non-null for nullability and for @NotNull emission - the field is Integer, not @Nullable Integer.
  • With preferPrimitives on, such a property becomes int rather than Integer.
  • A default on 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 support default.

Sensitive properties

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.

Nested and referenced types

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.

Per-file feature overrides

{
  "$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.

Load-time validation

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.

Clone this wiki locally