Pure OpenAPI 3.2 generator for Go.
Parses Go code comments to generate OpenAPI 3.2 specification JSON.
- Global Annotations: Configure Info, Servers, Tags, Security in
main.go. - API Annotations: Define Router, Params, Request Body, Responses in handler functions.
- Type Resolution: Automatically converts Go structs to OpenAPI Schemas.
- Generics Support: Full support for Go 1.18+ generic types (e.g.,
Response[User]). - Standard Library Parsing: Supports standard library types like
time.Time,os.File.
Current Version: 0.1.0 (Development)
- Global Annotation Parsing
- API Operation Parsing
- Schema Generation (Generics support included)
- CLI Tool
Install the command-line tool globally:
go install github.com/promonkeyli/goas/cmd/goas@latestAdd as a dependency to your project:
go get github.com/promonkeyli/goasIf installed globally, run directly:
goas -dir ./cmd,./internal -output ./apiOr run via go run in your project:
go run github.com/promonkeyli/goas/cmd/goas -dir ./cmd,./internal -output ./apiYou can also integrate goas directly into your Go code (e.g., in a generator script):
package main
import (
"log"
"github.com/promonkeyli/goas/pkg/goas"
)
func main() {
config := goas.Config{
Dirs: []string{"./cmd", "./internal"},
Output: "./api",
}
if err := goas.Run(config); err != nil {
log.Fatalf("Failed to generate OpenAPI: %v", err)
}
}package main
// @OpenAPI 3.2.0
// @Title Pet Store API
// @Version 1.0.0
// @Description Supports user management and file uploads.
// @Server http://localhost:8080/v1 name=dev Development
// @SecurityScheme ApiKeyAuth apiKey header X-API-KEY
func main() {
// ...
}package handler
// Response Generic Response
type Response[T any] struct {
Code int `json:"code"`
Message string `json:"message"`
Data T `json:"data"`
}
// User Model
type User struct {
ID int `json:"id" desc:"User ID"`
Name string `json:"name" desc:"Username"`
}
// GetUser Get User Info
// @Summary Get User Info
// @Tags user
// @Param id path int true "User ID"
// @Success 200 {object} Response[User] "Success"
// @Router /users/{id} [get]
func GetUser() {
// ...
}-dir: Comma-separated list of directories to scan (recursive).-output: Output directory foropenapi.json.
- GOAS Annotation Specification: Detailed guide on using goas annotations.
- OpenAPI 3.2.0 Field Reference: Quick reference for OpenAPI 3.2.0 objects and fields.
- OpenAPI Specification 3.2.0: Full OpenAPI 3.2.0 Specification.