Skip to content

Property Types

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

Property Types

Every property is either a "type" or a "$ref". Any other value for "type" is rejected at load time.

Scalars

type format Java type (boxed) Primitive
string - String String
integer absent or "int" Integer int
integer "long" Long long
number absent Double double
number "float" Float float
boolean - Boolean boolean
bytes - byte[] byte[]

preferPrimitives only downgrades a property that can never be null - one that is required or has a default. An optional property stays boxed and @Nullable.

A format on a string does not change the Java type; it drives Validation instead ("email", "uuid", "date", "time", "date-time", "decimal").

bytes

byte[] is mutable, so ModelMaker defends the array at every boundary:

private final byte[] payload;

public byte[] getPayload() {
  return payload.clone();          // callers never see the stored array
}
  • The builder setter clones the caller's array on the way in.
  • Every getter returns a clone.
  • A withXyz replacement clones too.
  • equals / hashCode use Arrays.equals / Arrays.hashCode - content, not identity.
  • toString prints the base64 form, via Base64.getEncoder().
  • Jackson serializes byte[] as a base64 string out of the box.
  • "default" is not supported for bytes.

Arrays

"tags": {
  "type": "array",
  "maxItems": 10,
  "items": { "type": "string", "minLength": 1, "maxLength": 40, "pattern": "^[a-z-]+$" }
}

renders as List<String> and, with validation on:

@Size(max = 10, message = "size must be at most 10")
public @Nullable List<@Pattern(regexp = "^[a-z-]+$", message = "must match \"^[a-z-]+$\"") @Size(min = 1, max = 40, message = "size must be between 1 and 40") String> getTags() {
  return tags != null ? Collections.unmodifiableList(tags) : null;
}

Rules:

  • "items" is required; an array without it is rejected.
  • Arrays of arrays are not supported.
  • The builder copies the incoming list (new ArrayList<>(...)); the getter returns Collections.unmodifiableList(...). The DTO never aliases a caller's list, and a caller can never mutate the DTO's.
  • minItems / maxItems size the List itself; keywords written inside "items" become container-element annotations on the element type.
  • A List of a generated type gets a @Valid cascade on the element when validation is on.
  • Element annotations render on the field / getter type only. The constructor and the builder keep the plain List<T>.

Inline objects

"contact": {
  "type": "object",
  "title": "Contact",
  "required": ["email"],
  "properties": {
    "email": { "type": "string", "format": "email" }
  }
}

becomes a nested class inside the owning type:

public final class Person {
  private final @Nullable Contact contact;
  // ...
  public static final class Contact {
    private final String email;
    // fields, constructor, getters, builder, BuilderMutator ...
  }
}
  • "title" names the class and must be a plain class name - a dotted value is rejected.
  • Without "title", the class is named after the capitalized property key (contact → Contact).
  • Nesting can go deeper; each level produces another nested class.
  • A nested object cannot carry its own features - only the top-level type's features applies to the whole file.
  • Nested types get the full treatment too: their own builder, BuilderMutator, equals / hashCode / toString, and their own Kotlin mutate { } extension.

$ref

"shippingAddress": { "$ref": "com.example.dto.Address" },
"createdAt":       { "$ref": "java.time.Instant" }

A $ref resolves to one of three things:

  • Another schema's title - the generated class for that schema. Cross-checked after loading; an unknown target fails the build.
  • A nested type's plain name - the inline class declared in the same file.
  • A java.* / javax.* type - used as-is and imported. Not generated, and not validated for existence - javac reports a bad one.

Notes:

  • A same-package reference renders as the simple class name; java.lang.* targets are used without an import.
  • A JDK $ref is opaque to ModelMaker: no constraints are derived from it, and it is not cascaded into with @Valid. Whether Jackson can bind it is up to your ObjectMapper configuration.
  • $ref is the escape hatch for anything the type system above cannot express - see FAQ.

Not supported

  • enum as a Java enum - rendered as a String with a generated @Pattern over the allowed values.
  • Maps / dictionaries - no mapping. Use a nested object with fixed keys, or a $ref to a JDK type.
  • Arrays of arrays - rejected at load time.
  • Polymorphism (oneOf / anyOf / allOf) - not parsed.
  • Recursion by inline nesting - an inline object is expanded in place, so it cannot nest into itself. A $ref cycle between top-level schemas is fine, since refs are resolved by name rather than inlined.

The guiding rule: generated models support the simple types most likely to round-trip cleanly between a Java object and JSON.

Clone this wiki locally