woodpecker/docs/docs/92-development/08-swagger.md
Martin W. Kirst 14177635b6
Update swagger API specification (#1782)
# Summary

This PR drops the outdated former swagger.yaml/json and introduced
automatic API document generation from Go code.
The generated code is also used to generate documentation/markdown for
the community page,
as well as enable the Woodpecker server to serve a Swagger Web UI for
manual tinkering.

I did opt-in for gin-swagger, a middleware for the Gin framework, to
ease implementation and have a sophisticated output.
This middleware only produces Swagger v2 specs. AFAIK the newer OpenApi
3x tooling is not yet that mature,
so I guess that's fine for now.

## Implemenation notes

- former swagger.json files removed
- former // swagger godocs removed
- introduced new dependency gin-swagger, which uses godoc annotations on
top of Gin Handler functions.
- reworked Makefile to automatically generate Go code for the server
- introduce new dependency go-swagger, to generate Markdown for
documentation purposes
- add a Swagger Web UI, incl. capabilities for manual API exploration
- consider relative root paths in the implementation
- write documentation for all exposed API endpoints
- incl. API docs in the community website (auto-generated)
- provide developer documentation, for the Woodpecker authors
- no other existing logic/code was intentionally changed

---------

close #292

---------

Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
Co-authored-by: 6543 <6543@obermui.de>
2023-06-03 21:38:36 +02:00

3.2 KiB

Swagger, API Spec and Code Generation

Woodpecker uses gin-swagger middleware to automatically generate Swagger v2 API specifications and a nice looking Web UI from the source code. Also, the generated spec will be transformed into Markdown, using go-swagger and then being using on the community's website documentation.

It's paramount important to keep the gin handler function's godoc documentation up-to-date, to always have accurate API documentation. Whenever you change, add or enhance an API endpoint, please update the godocs.

You don't require any extra tools on your machine, all Swagger tooling is automatically fetched by standard Go tools.

Gin-Handler API documentation guideline

Here's a typical example of how annotations for Swagger documentation look like...

--- server/api/user.go ---
//	@Summary		Get a user
//	@Description	Returns a user with the specified login name. Requires admin rights.
//	@Router			/users/{login} [get]
//	@Produce		json
//	@Success		200	{object}	User
//	@Tags			Users
//	@Param			Authorization	header	string	true	"Insert your personal access token"	default(Bearer <personal access token>)
//	@Param			login			path	string	true	"the user's login name"
// @Param   foobar  query   string false "optional foobar parameter"
//	@Param	  page    query	int		false	"for response pagination, page offset number"	default(1)
//	@Param   perPage query	int		false	"for response pagination, max items per page"	default(50)
--- server/model/user.go ---
type User struct {
  ID int64 `json:"id" xorm:"pk autoincr 'user_id'"`
// ...
} //	@name User

These guidelines aim to have consistent wording in the swagger doc:

  • first word after @Summary and @Summary are always uppercase
  • @Summary has no . (dot) at the end of the line
  • model structs shall use custom short names, to ease life for API consumers, using @name
  • @Success object or array declarations shall be short, this means the actual model.User struct must have a @name annotation, so that the model can be renderend in Swagger
  • when pagination is used, @Parame page and @Parame perPage must be added manually
  • @Param Authorization is almost always present, there are just a few un-protected endpoints

There are many examples in the server/api package, which you can use a blueprint. More enhanced information you can find here https://github.com/swaggo/swag/blob/master/README.md#declarative-comments-format

Manual code generation

generate the server's Go code containing the Swagger
make generate-swagger
update the Markdown in the ./docs folder
make docs
go run github.com/swaggo/swag/cmd/swag@latest fmt -g server/api/z.go

WARNING, known issue: using swag v1.18.12 , there's a bug when running the fmt command, which makes the swagger generator failing, because it can't find the models/structs/types anymore. To fix it, please replace // @name\tModelName with // @name ModelName, which means, replace the tab (\t) with a space ( ). See https://github.com/swaggo/swag/pull/1594 == once this is merged and released, the mentioned issue is obsolete.