r/golang • u/Emotional-Ask-9788 • 12d ago
help OpenAPI Specs
How do you handle OpenAPI in go?
I'm coming from C# .Net and Laravel, normally these auto generate the specs, but in go I haven't found the standard way of doing this, the golang way.
I understand there are packages you can use to do this by writing comments/annotations, which is very tedious.
Is there a proper way to do this, or do even go developers do this at all?
Your response is very welcome, especially from those working in companies with somewhat large code base.
Thank you!
Edit: Thank you all for your feedback and recommendations, but I'll stick to ASP.NET Core for now. Maybe in future when I have time I'll explore Go again, it's a beautiful language.
9
u/Helpful-Educator-415 12d ago
I don't write Go much anymore, but Huma seems to be the most popular.
43
u/radio-nl 12d ago
In C# it’s usually code first, in Go - SPEC/contract first is the preferable option. IMHO, at some point you realize that it’s always better to have a clear plan before even starting to implement, therefore the latter approach seems more mature.
Here’s a project that might be of a service - https://github.com/ogen-go/ogen
18
u/BornSpecific9019 12d ago
personally prefer spec first
- write oapi spec
- use openapi-codegen to generate
- use CI pipeline to check if generated code is up to date
- implement the generated interfaces (preferably with strict mode)
6
u/unhappylurker 12d ago
oapi-codegen/oapi-codegen is what I would use. Write the spec first, then generate the code using spec
5
u/IncomeOk7237 12d ago
Coming from ASP.NET I had the same reaction. If you really want code-first, Huma is the closest thing to Swashbuckle, it builds the spec from your actual request/response structs so it can't drift from the handlers. The comment annotation route (swag and friends) is the one I'd avoid, the comments go stale the first time someone renames a field and nothing fails.
That said spec-first grew on me. The nice side effect is that API shape changes show up as a yaml diff in review before anyone writes Go, which is way cheaper than arguing about it after the handlers exist.
4
u/dariusbiggs 12d ago
Do you want specification first, or code first for the authorive source?
Code first is Huma v2 and a few other options.
Specification first is pretty much everything else like swaggo, that is code generation from the schema.
3
u/Ok_Manufacturer_8213 12d ago
For Code first chi with huma is great!
3
u/itaranto 12d ago
Anything but the annotation/comment based solutions. It's the worst of both worlds. I remember using swaggo for this, you end up adding lots of comments and struct tags all over the place, it gets pretty complex pretty fast. It has the same downsides as witting the spec manually, there's no guarantee those comments/tags are (fully) in sync with the code. At that point you are better off writing the entire spec manually, which is bad, but at least it's less annoying.
Now, the ideal scenario would be to either use a spec-first or a code-first approach. I don't like much the idea of generating such a critical part of a server, code generators tend to add too much bloat an unnecessary stuff as they need to be generic.
So I think code-first theoretically should be better, but I haven't tried yet. I've heard nice things about Huma.
3
u/Feeling-Row8973 12d ago
write the spec in YAML first and generate handlers with oapi-codegen. Trying to infer it from Go structs or comments results in brittle code that fights you every time you add a header, validation rule, or complex schema.
5
u/gomsim 12d ago edited 12d ago
We do spec-first. Write the openapi spec and generate the code with oapi-codegen. I kind of hate it. So much of the generated code is incompatible with the stdlib i various ways. And openapi-specs invite structures like inheritence which makes the generated code very non-intuitive. Recently we tried the non-strict mode, which is better, but at this point it's not that big of a difference to just not generating anything at all. Personally I prefer to not generate one or the other, but that has obvious downsides too of course, hehe,
I haven't tried huma, but it sounds like that would yield a smoother experience.
4
u/WolverinesSuperbia 12d ago
I use swaggo/swag. Annotations in comments, tool for formatting and regeneration.
1
u/22goodnumber 12d ago
I'm a big fan of fuego (https://github.com/go-fuego/fuego). It uses the new(-ish) go generics so method signatures define the inputs and outputs and it generates swagger/open-api docs like what you're used to. Lots of options to customize the documentation as well.
1
u/iabqle_bis94 12d ago
Before AI, I used swag-go by adding comments; these days, I ask AI to generate the openapi-v3.yaml first, and once I review it, I ask my agent to write the api.
1
u/Open-Adhesiveness-86 11d ago
if you do go code-first with huma, check what version your consumers need before you commit. it emits 3.1 by default and a lot of client generators still choke on that, especially anything older built on openapi-generator. huma does serve a downgraded /openapi-3.0.yaml, but the conversion is lossy around nullable, so diff the two before you hand it to a frontend team.
1
u/fahim-sabir 11d ago
Can’t believe that even now people are going code first. Somewhat defeats the point if you ask me.
1
u/Heavy-Blacksmith-620 11d ago
I use https://github.com/oaswrap/spec . This is code first, and has adapter for most popular api library.
1
u/Turbulent_Ad9831 11d ago
Go doesn't really have an annotation magic culture like .NET or Laravel, so trying to generate OpenAPI from struct comments with swaggo usually turns messy fast. Most Go teams I've seen go spec-first with oapi-codegen so the YAML stays the source of truth and the server stubs stay strictly typed.
1
u/sonicLodge91 9d ago
had a swag ci check wired on one service, the other two drifted for omnths before anyone noticed
1
u/autogenusrz 9d ago
1
u/dariusbiggs 1d ago
So you want code first or schema first.
There are others in each category with more or less automation.
1
u/moodyindie 8d ago
Same diff check here, my pain is the client lib, every spec tweak reshuffles the generated types, how do you keep consumer churn down?