OpenAPI to Java DTO Generator

Paste an OpenAPI 3 spec — JSON or YAML — and get Java records or classes with Jackson annotations, enums and java.time mapping.

In-browser & private
Advertisement
OpenAPI spec (JSON or YAML) Ctrl + Enter to generate
Java
Paste a spec on the left, or press Sample.
Types — Fields — Size 0 B

How the types are chosen

An OpenAPI schema carries more type information than raw JSON does, so this maps rather than guesses — but a few of those mappings are judgement calls, and they are worth stating.

  • number becomes BigDecimal unless the spec says float or double. Prices and quantities turn up in almost every real spec, and binary floating point is the wrong default for either.
  • Dates follow the format. date becomes LocalDate, date-time becomes OffsetDateTime — not LocalDateTime, because an API timestamp carries an offset and dropping it loses information.
  • allOf is flattened. It is how OpenAPI expresses composition, and Java has no structural intersection type — so the members are merged into one flat DTO.
  • Enums keep their wire value. A value like in progress cannot be a Java constant, so the constant is IN_PROGRESS and the original string is retained for Jackson to serialise.
  • Names are converted, not renamed. total_amount becomes totalAmount and carries @JsonProperty("total_amount") so it still binds. Java keywords get a suffix — a field called class would not compile.

Frequently asked questions

Is my spec uploaded anywhere?
No. The spec is parsed and the Java generated in your browser, so an internal API description never leaves the page.
Which OpenAPI versions work?
OpenAPI 3.0 and 3.1, in YAML or JSON, and the two give identical output. A 3.1 union such as type: [string, "null"] becomes the non-null type. Swagger 2.0 is refused with a pointer to a converter rather than half-supported, because its schemas live somewhere else entirely.
What happens to $ref, allOf and oneOf?
A local $ref resolves to the generated class. allOf is flattened into one type, which is what a DTO has to be. oneOf and anyOf have no Java equivalent and become Object, and a reference this document does not define does too. Every one of those is listed as a note under the toolbar rather than left for you to notice.
Why are numbers BigDecimal and dates OffsetDateTime?
A bare type: number carries money and quantities in most real specs, and binary floating point is the wrong default for either. Add format: float or double to get those. date-time is an instant with an offset, which is what OffsetDateTime models; date becomes LocalDate.
Will the generated code compile as it stands?
Yes. Names that collide once converted to Java are given distinct identifiers while keeping their original wire names in @JsonProperty, so nothing is quietly dropped and the files compile. You need Jackson on the classpath for the annotations, and Bean Validation if you tick that box.
Can I generate the Spring controllers too?
Yes, from the same spec: the OpenAPI to Spring controller generator writes one @RestController per tag and imports these DTOs.

More free tools