News for hackers. Stuff that matters.
Re:Write OpenAPI with TypeSpec by FourOnTheFloor on Thursday March 28, 2024 @12:04AM (#39847662)
Targeting OpenAPI 3.0 is a bad idea. It wasn't until 3.1 that it became truly useful, if only because it fixed one glaring blunder: Pre-3.1, you inexplicably couldn't provide a description along with a $ref. This defeats one of the major purposes of OpenAPI, which is... documenting your API.

Example: If you define a structure called Rectangle and use it all over the place, you can't say what this rectangle means or that rectangle means when the structure is used in your API. How was that ever deemed acceptable? Widespread use of common structures (which one would certainly expect in a well-designed API) resulted in an API that couldn't be documented in OAS.

The OpenAPI code-generation landscape is truly abysmal. Not only do almost none of the tools support version 3.1 (which has been current for years), but the generators themselves are of widely varying (but mostly shitty) quality and scattered across a couple of apparent major repos. The documentation is likewise a confusing morass of conflicting and incomplete information from several sources. I flailed away trying to fix one generator for weeks, or write a new one. But I couldn't even find a concise list of the data structures offered to the template engine after an OpenAPI doc is ingested by OpenAPIGenerator. Isn't that a fundamental part of creating a new generator?

So it raises the question of whether OpenAPI is salvageable, or whether this new DSL is better... from design to code generation. Given the trashy state of OpenAPI code generators, why bother generating OAS at all? Just move on and write some competent generators using this new language.

Re:Write OpenAPI with TypeSpec by bterlson on Thursday March 28, 2024 @12:47AM (#39847877)
For what it's worth, we support 3.0 because as you note the ecosystem doesn't support 3.1 broadly yet. I'm personally interested to see if 3.1 becomes prevalent before 4.0 is released. Maybe the ecosystem will just skip 3.1?

We generate OAS because it's useful for many folks, including for us in Azure. But like you we didn't have very good luck getting high quality codegen from OpenAPI. Our latest client codegen tech doesn't use OpenAPI, we generate code directly from the TypeSpec, which offers a number of advantages that result in higher quality. Probably at topic for a blog of its own! Anyway, you might be happy to know that we are working on bringing our emitters into the TypeSpec project so anyone can use it with their TypeSpecs.

Re:Write OpenAPI with TypeSpec by FourOnTheFloor on Thursday March 28, 2024 @12:53AM (#39847906)
Cool, thanks for the reply!
Re:Write OpenAPI with TypeSpec by mstade on Friday March 29, 2024 @10:05PM (#39871174)
Is this client generation available anywhere, or internal tooling?
Re:Write OpenAPI with TypeSpec by bterlson on Saturday March 30, 2024 @04:22AM (#39872909)
We're working on bringing it into the TypeSpec project as we speak. You can see an initial demo of it working in this repo: https://github.com/bterlson/typespec-todo.
Re:Write OpenAPI with TypeSpec by mstade on Monday April 01, 2024 @12:59PM (#39896279)
Awesome – thank you so much for sharing!
Re:Write OpenAPI with TypeSpec by bterlson on Thursday March 28, 2024 @12:59AM (#39847929)
By the way, you can use the "allOf hack" to put documentation alongside a ref. TypeSpec emits this for common cases like this one:

https://typespec.io/playground?c=bW9kZWwgRm9vIHsNCiAgLyoqIHR...

Re:Write OpenAPI with TypeSpec by FourOnTheFloor on Thursday March 28, 2024 @02:48AM (#39848398)
Hadn't heard of that, thanks.

Does TypeSpec have description fields? I don't think I've seen any in the examples.

Re:Write OpenAPI with TypeSpec by bterlson on Thursday March 28, 2024 @03:03AM (#39848461)
The playground I posted has a description. They're pulled from JSDoc style comments. You can also use the `@doc` decorator, though that's usually reserved for more advanced cases like when you need string interpolation or something.
Re:Write OpenAPI with TypeSpec by FourOnTheFloor on Friday March 29, 2024 @04:23AM (#39861768)
Thanks. I don't see any in that playground though. Which document are they in? I think I looked in all the ones in the drop-down.
Re:Write OpenAPI with TypeSpec by bterlson on Friday March 29, 2024 @06:54PM (#39869904)
The link should load up with a sample I wrote, I think? It does for me anyway! Let me know if you don't see it. But I'll also paste it here:

    model Foo {
      /** this is a description */
      x: Bar;
    }

    /** this is also a description */
    model Bar {}
Re:Write OpenAPI with TypeSpec by FourOnTheFloor on Sunday March 31, 2024 @07:28PM (#39889226)
Thanks. Never having seen a description in this language, I wouldn't know that the above syntax designated one; it looks like a comment.

I also can't find any similar strings in any of the samples in the drop-down list.