Generate a fully compliant, extendable OpenAPI Specification document to enable you to make use of the Swagger Tooling to generate RESTful server stubs and clients.
To generate a ready-to-use server implementation of the OpenAPI Specification generated by entoas
have a look
at elk.
Download the module:
go get -u entgo.io/contrib/entoas
Setup code generation with entc
package: and register the entoas
extension:
- Create a new Go file named
ent/entc.go
and paste the following content:
// +build ignore
package main
import (
"log"
"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
"entgo.io/contrib/entoas"
)
func main() {
ex, err := entoas.NewExtension()
if err != nil {
log.Fatalf("creating entoas extension: %v", err)
}
err = entc.Generate("./schema", &gen.Config{}, entc.Extensions(ex))
if err != nil {
log.Fatalf("running ent codegen: %v", err)
}
}
- Edit the
ent/generate.go
file to execute theent/entc.go
file:
package ent
//go:generate go run -mod=mod entc.go
- Run the code generator:
go generate ./...
Observe, that in addition to the files Ent would normally generate, another file called openapi.json
was generated.
For information about configuring the generator head over to the godoc or Ent documentation.
This PR introduced a slight change in the API. entoas
now uses ogen
s OAS
struct definitions to create the OAS document. This change eases Specification mutations and also enables users to
use ogen
to generate both Go client and server implementations for the OAS.