logoalt Hacker News

WickyNilliamslast Sunday at 11:26 PM4 repliesview on HN

This is very much possible, and I have done it, and it works great once it's all wired up.

But OpenAPI is verbose to the point of absurdity. You can't feasibly write it by hand. So you can't do schema first development. You need an open API compatible lib for authoring your API, you need some tooling to generate the schema from the code, then you need another tool to generate types from the schema. Each step tends to implement the spec to varying degrees, creating gaps in types, or just outright failing.

Fwiw I tried many, many tools to generate the typescript from the schema. Most resulted in horrendous, bloated code. The official generators especially. Many others just choked on a complex schema, or used basic string concatenation to output the typescript leading to invalid code. Additionally the cost of the generated code scales with the schema size, which can mean shipping huge chunks of code to the client as your API evolves

The tool I will wholeheartedly recommend (and which I am unaffiliated beside making a few PRs) is openapi-ts. It is fast and correct, and you pay a fixed cost - there's a fetch wrapper for runtime and everything else exists at the type level.

I was kinda surprised how bad a lot of the tooling was considering how mature OpenAPI is. Perhaps it's advanced in the last year or so, when I stopped working on the project where I had to do this.

https://openapi-ts.dev/


Replies

0x696C6961last Sunday at 11:34 PM

I write all of my openapi specs by hand. It's not hard.

show 2 replies
jcgllast Tuesday at 10:23 AM

Agree with the other comments about writing OpenAPI by hand. It’s really not that bad at all, and most certainly not “verbose to the point of absurdity.”

Moreover, system boundaries are the best places to invest in being explicit. OpenAPI specs really don’t have that much overhead (especially if you make use of YAML anchors), and are (usually) suitably descriptive to describe the boundary.

In any case, starting with a declarative contract/IDL and doing something like codegen is a great way to go.

fclairamblast Wednesday at 7:00 AM

YAML OpenAPI schema, like SQL, is quite easy to write by hand and more importantly by AI. Telling AI to keep the openapi in sync with the latest changes made on an API works great and can even help you identify inconsistencies.

hokkoslast Monday at 12:06 AM

I use https://typespec.io to generate openapi, writing openapi yaml quickly became horrible past a few apis.

show 1 reply