Skip to content

kwinwithak/swag-gin-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Gin API with OpenAPI v3 Docs

This demonstrates a simple way to document an API built in Go with the Gin web framework. The OpenAPI definition is generated by swaggo/swag using annotations directly in your code.

Here I'm taking a "code-first" approach as opposed to the "contract-first" API development favored by popular API code generators like oapi-codegen and ogen. I agree with commenters on this thread who lament the complexity of these code generators and the general landscape of Go API documentation tools. My use case is that I already have an API that I need to document, so it's a waste of time to dump everything and go "contract-first". Plus I prefer the greater flexibility. I'm not a Go wizard so the code generated by oapi-codegen looks unnecessarily complex to me.

I pulled the majority of this demo from here: https://blog.logrocket.com/documenting-go-web-apis-with-swag/

Note swag's homepage states that it only supports OpenAPI v2. Not exactly true. Their v2.0.0-rc4 release supports OpenAPI v3.1. Hopefully they promote that to an official release soon, but for now it seems to work okay.

Usage

Generate the OpenAPI v3.1 definition:

swag init --v3.1

Run the API. The docs are served at http://localhost:8080/

go run main.go

About

Demo Gin API with OpenAPI docs generated with Swag

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published