Repository navigation
Show and tell: generating laravel-data classes from OpenAPI specs (openapi-laravel) #1193
benjamineckstein
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I built a generator that emits laravel-data v4 classes from OpenAPI documents:
https://github.com/codewithagents/openapi-laravel. One command turns a spec component into a Data
class with promoted readonly properties,
#[MapName]where the wire name differs, and an explicitrules()method. laravel-data turned out to be a near-perfect target for this, and I wanted toshare the parts that were genuinely tricky.
rules() generation. I emit rules verbatim from spec constraints (
required,nullable,maxLength,pattern, formats,Rule::enum) rather than relying on runtime inference, keyed bythe wire names since validation runs on the raw payload. Optional fields get
sometimes, whichtook care to get right alongside
nullable.readOnly/writeOnly. A schema using these flags becomes two classes: a read variant (response
shape) and a write variant (request shape). The generated abstract controller then takes
PetWritableDataand returnsPetData, which makes the asymmetry explicit instead of magical.allOf. Flattened into one class: members unioned,
requireddeduped, conflicts resolveddeterministically, so regeneration is byte-identical.
oneOf/anyOf. Scalar unions work beautifully:
string|inthydrates and serializes withoutcoercion, proven over real HTTP in our e2e demo. Object unions are the honest limitation: without a
spec
discriminatorlaravel-data cannot pick a variant, so those are typedmixedwith a variantdocblock. If anyone has thoughts on a clean discriminator-less strategy, I am interested.
One more seam the demo surfaced: an empty
additionalPropertiesmap serializes as[]not{},PHP's classic empty-array ambiguity.
Thanks for laravel-data, it carried this whole project.
All reactions