Examples
This is real output from the current generator, not hand-written
approximations. Every snippet below came out of an actual
openapi2code pull run. Paste
any of these specs into the
playground
yourself to see the rest of each target.
A model with a reference, an array, and an enum
Pet references
Category and
Tag, holds an array of
strings, and has an inline enum field. These are the shapes that
show up in almost every real API.
Pet:
type: object
required: [name, photoUrls]
properties:
id: { type: integer }
name: { type: string }
category: { $ref: '#/components/schemas/Category' }
tags: { type: array, items: { $ref: '#/components/schemas/Tag' } }
photoUrls: { type: array, items: { type: string } }
status: { type: string, enum: [available, pending, sold] }
allOf composition
Dog is composed from
Animal plus its own
breed field. TypeScript
renders this as extends;
every other target flattens it into one flat type or struct,
since none of them model inheritance the way TS interfaces do.
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
required: [breed]
properties:
breed: { type: string }
oneOf and circular references
TypeScript and Zod render oneOf
as a real union. The mobile targets don't yet (see
the playground
for the current target list). Rather than generate something
subtly wrong, they skip the field and leave an explanatory
comment instead. A self-referential type like
TreeNode works everywhere,
though Swift renders it as a
class instead of a
struct specifically for
cyclic models, since a Swift struct can't contain itself.
StringOrNumber:
oneOf:
- type: string
- type: number
TreeNode:
type: object
required: [value]
properties:
value: { type: string }
children: { type: array, items: { $ref: '#/components/schemas/TreeNode' } }