Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,33 @@ To implement this, check out [the Fiber docs](#impl-fiber).
</td>
</tr>

<tr>
<td>

[Fiber v3](https://github.com/gofiber/fiber)

</td>
<td>
<code>fiber-server</code>
</td>

<td>

For a Fiber v3 server, you will want a configuration file such as:

```yaml
# yaml-language-server: ...
package: api
generate:
fiber-server: true
models: true
output: gen.go
```

To implement this, check out [the Fiber docs](#impl-fiber).

</td>
</tr>

<tr>
<td>
Expand Down Expand Up @@ -1257,6 +1284,135 @@ func main() {
> [!NOTE]
> This doesn't include [validation of incoming requests](#requestresponse-validation-middleware).

### Fiber V3 server
<a name="impl-fibe-v3r"></a>

For instance, let's take this straightforward specification:

```yaml
openapi: "3.0.0"
info:
version: 1.0.0
title: Minimal ping API server
paths:
/ping:
get:
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pong'
components:
schemas:
# base types
Pong:
type: object
required:
- ping
properties:
ping:
type: string
example: pong
```

This then generates code such as:

```go
// Pong defines model for Pong.
type Pong struct {
Ping string `json:"ping"`
}

// ServerInterface represents all server handlers.
type ServerInterface interface {
// (GET /ping)
GetPing(c fiber.Ctx) error
}

// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}

type MiddlewareFunc fiber.Handler

// GetPing operation middleware
func (siw *ServerInterfaceWrapper) GetPing(c fiber.Ctx) error {
return siw.Handler.GetPing(c)
}

// FiberServerOptions provides options for the Fiber server.
type FiberServerOptions struct {
BaseURL string
Middlewares []MiddlewareFunc
}

// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
func RegisterHandlers(router fiber.Router, si ServerInterface) {
RegisterHandlersWithOptions(router, si, FiberServerOptions{})
}
```

To implement this HTTP server, we need to write the following code in our [`api/impl.go`](examples/minimal-server/fiberv3/api/impl.go):

```go
package api

import (
"net/http"

"github.com/gofiber/fiber/v3"
)

// ensure that we've conformed to the `ServerInterface` with a compile-time check
var _ ServerInterface = (*Server)(nil)

type Server struct{}

func NewServer() Server {
return Server{}
}

// (GET /ping)
func (Server) GetPing(ctx fiber.Ctx) error {
resp := Pong{
Ping: "pong",
}

return ctx.
Status(http.StatusOK).
JSON(resp)
}
```

Now we've got our implementation, we can then write the following code to wire it up and get a running server:

```go
import (
"log"

"github.com/gofiber/fiber/v3"
"github.com/oapi-codegen/oapi-codegen/v2/examples/minimal-server/fiberv3/api"
)

func main() {
// create a type that satisfies the `api.ServerInterface`, which contains an implementation of every operation from the generated code
server := api.NewServer()

app := fiber.New()

api.RegisterHandlers(app, server)

// And we serve HTTP until the world ends.
log.Fatal(app.Listen("0.0.0.0:8080"))
}
```

> [!NOTE]
> This doesn't include [validation of incoming requests](#requestresponse-validation-middleware).

### Gin server
<a name="impl-gin"></a>

Expand Down
2 changes: 2 additions & 0 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ func generationTargets(cfg *codegen.Configuration, targets []string) error {
opts.ChiServer = true
case "fiber-server", "fiber":
opts.FiberServer = true
case "fiber-v3-server", "fiber-v3":
opts.FiberV3Server = true
case "server", "echo-server", "echo":
opts.EchoServer = true
case "gin", "gin-server":
Expand Down
4 changes: 4 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"type": "boolean",
"description": "FiberServer specifies whether to generate fiber server boilerplate"
},
"fiber-v3-server": {
"type": "boolean",
"description": "FiberV3Server specifies whether to generate fiber-v3 server boilerplate"
},
"echo-server": {
"type": "boolean",
"description": "EchoServer specifies whether to generate echo server boilerplate"
Expand Down
39 changes: 26 additions & 13 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
module github.com/oapi-codegen/oapi-codegen/v2/examples

go 1.21.0
go 1.23

toolchain go1.24.1

replace github.com/oapi-codegen/oapi-codegen/v2 => ../

require (
github.com/getkin/kin-openapi v0.128.0
github.com/getkin/kin-openapi v0.131.0
github.com/gin-gonic/gin v1.10.0
github.com/go-chi/chi/v5 v5.0.10
github.com/gofiber/fiber/v2 v2.52.4
github.com/google/uuid v1.5.0
github.com/gofiber/fiber/v3 v3.0.0-beta.4
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/kataras/iris/v12 v12.2.6-0.20230908161203-24ba4e8933b9
github.com/labstack/echo/v4 v4.12.0
github.com/lestrrat-go/jwx v1.2.26
github.com/oapi-codegen/echo-middleware v1.0.2
github.com/oapi-codegen/fiber-middleware v1.0.2
github.com/oapi-codegen/fiber-middleware/v2 v2.0.0-00010101000000-000000000000
github.com/oapi-codegen/gin-middleware v1.0.2
github.com/oapi-codegen/iris-middleware v1.0.5
github.com/oapi-codegen/nethttp-middleware v1.0.2
Expand All @@ -32,7 +36,7 @@ require (
github.com/CloudyKit/jet/v6 v6.2.0 // indirect
github.com/Joker/jade v1.1.3 // indirect
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/brotli v1.1.1 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
Expand All @@ -44,6 +48,7 @@ require (
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2/v4 v4.0.2 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
Expand All @@ -52,11 +57,12 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gofiber/schema v1.2.0 // indirect
github.com/gofiber/utils/v2 v2.0.0-beta.7 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -65,7 +71,7 @@ require (
github.com/kataras/pio v0.0.12 // indirect
github.com/kataras/sitemap v0.0.6 // indirect
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand All @@ -83,8 +89,11 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
Expand All @@ -94,26 +103,30 @@ require (
github.com/speakeasy-api/openapi-overlay v0.9.0 // indirect
github.com/tdewolff/minify/v2 v2.12.9 // indirect
github.com/tdewolff/parse/v2 v2.6.8 // indirect
github.com/tinylib/msgp v1.2.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/fasthttp v1.58.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yosssi/ace v0.0.5 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/oapi-codegen/fiber-middleware/v2 => github.com/kerak19/fiber-middleware/v2 v2.0.0-20250323205111-59ed66807d2f

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading