Skip to content

OpenAPI

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

OpenAPI

The openApi feature adds io.swagger.v3.oas.annotations.media.Schema metadata, so tools such as springdoc or swagger-core describe the DTO in the generated API document using the schema's own wording.

modelmaker {
    features {
        openApi { enabled = true }
    }
}

What it emits

Schema keyword Where Emitted as
description (top-level) class @Schema(description = "...")
description (property) getter or field @Schema(description = "...")
example (property) getter or field @Schema(example = "...")
property listed in required getter or field requiredMode = Schema.RequiredMode.REQUIRED
{
  "description": "A customer of the shop.",
  "required": ["id"],
  "properties": {
    "id": { "type": "string", "description": "Customer identifier.", "example": "C42" },
    "nickname": { "type": "string" }
  }
}
@Schema(description = "A customer of the shop.")
public final class Customer {

  @Schema(description = "Customer identifier.", example = "C42", requiredMode = Schema.RequiredMode.REQUIRED)
  public String getId() {
    return id;
  }

  public @Nullable String getNickname() {   // no @Schema - nothing to say about it
    return nickname;
  }
}

Two rules follow from that:

  • A class @Schema is emitted only when the schema has a description.
  • A property @Schema is emitted only when the property has a description or an example. requiredMode alone never triggers one - a required property with no prose gets no annotation.

example is always a string

example must be a JSON primitive, and it is rendered into the annotation as a string literal:

"loyaltyPoints": { "type": "integer", "example": 42 }
@Schema(example = "42")

That matches @Schema(example = ...), whose declared type is String. A non-primitive example (an object or an array) is ignored.

What is deliberately left out

Facets such as minimum, maximum, pattern, minLength and maxLength are not copied into @Schema. They are emitted as jakarta.validation annotations by the validation feature, which springdoc and swagger-core already read when building the document - duplicating them into @Schema would create two sources of truth for the same constraint.

So a typical API project turns on all three features:

modelmaker {
    features {
        jackson { enabled = true }
        validation { enabled = true }
        openApi { enabled = true }
    }
}

jackson fixes the property names and order, validation supplies the facets, openApi supplies the prose.

Placement

@Schema sits on the getters by default and is the first annotation on a member, before Jackson's and the validation constraints. See Feature Configuration.

Dependency

dependencies {
    implementation("io.swagger.core.v3:swagger-annotations-jakarta:2.2.30")
}

The -jakarta artifact is the one that matches the jakarta.* namespace used elsewhere in the generated code. It is required on the compile classpath whenever openApi is on.

Clone this wiki locally