-
-
Notifications
You must be signed in to change notification settings - Fork 2k
π₯ feat: Add OpenAPI middleware #3702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gaby
wants to merge
33
commits into
main
Choose a base branch
from
2025-08-21-14-48-18
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
ed61350
test: assert raw openapi spec matches fixture
gaby 0cded2b
refactor: split route media types
gaby 886ed13
feat: add route tags and deprecation
gaby 60d4596
test: expand openapi middleware coverage
gaby 149c053
test: cover openapi helpers
gaby ddbaa0f
feat(openapi): add operation helpers
gaby 4ab58fd
refactor: use maps.Copy for metadata cloning
gaby 9b1a022
docs: clarify openapi type reference
gaby 2b4fa75
test: normalize openapi json assertions
gaby 2bab831
chore: address openapi review feedback
gaby 3c33f84
fix: scope openapi handler to its resolved path
gaby 7da513d
Adjust OpenAPI default responses
gaby abe75b9
Extend OpenAPI helpers with schema refs and examples
gaby 87d1f44
Merge branch 'main' into 2025-08-21-14-48-18
gaby 868cf03
Merge branch 'main' into 2025-08-21-14-48-18
gaby ac3bc5c
Merge branch 'main' into 2025-08-21-14-48-18
gaby 615d8e9
Merge branch 'main' into 2025-08-21-14-48-18
gaby abcc817
Merge branch 'main' into 2025-08-21-14-48-18
gaby 603b4ab
Merge branch 'main' into 2025-08-21-14-48-18
gaby d092785
π bug: fix merge conflicts and auto-generated HEAD routes in OpenAPI β¦
Claude 0c16991
π₯ feat: merge parallel benchmarks from main into router_test.go
Claude 272d61e
π§Ή chore: fix lint issues and optimize struct alignment
Claude 1ecf5d1
Merge branch 'main' into 2025-08-21-14-48-18
gaby b76e04d
π§Ή chore: address review comments - clone Tags, filter middleware routβ¦
Copilot 9158f56
π§Ή chore: fix deprecated utils.ToLower lint warning
Copilot d342624
β
test: add auto-HEAD exclusion test for OpenAPI middleware
Copilot 4e33e7d
Merge branch 'main' into 2025-08-21-14-48-18
gaby 49ee7a2
fix: add OpenAPI methods to domainRouter, defensive-copy Tags slice, β¦
Copilot 38a32b9
refactor: simplify Tags() defensive copy
Copilot 9946f64
π security: harden OpenAPI middleware - fix path generation, add nil β¦
Claude 8dcfd7c
π§ͺ test: improve openapi middleware coverage to 93.1%
Claude e3918d1
Merge branch 'main' into 2025-08-21-14-48-18
gaby 057e9eb
Merge branch 'main' into 2025-08-21-14-48-18
gaby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| --- | ||
| id: openapi | ||
| --- | ||
|
|
||
| # OpenAPI | ||
|
|
||
| OpenAPI middleware for [Fiber](https://github.com/gofiber/fiber) that generates an OpenAPI specification based on the routes registered in your application. | ||
|
|
||
| ## Signatures | ||
|
|
||
| ```go | ||
| func New(config ...Config) fiber.Handler | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| Import the middleware package that is part of the Fiber web framework | ||
|
|
||
| ```go | ||
| import ( | ||
| "github.com/gofiber/fiber/v3" | ||
| "github.com/gofiber/fiber/v3/middleware/openapi" | ||
| ) | ||
| ``` | ||
|
|
||
| After you initiate your Fiber app, you can use the following possibilities: | ||
|
|
||
| ```go | ||
| // Initialize default config. Register the middleware *after* all routes | ||
| // so that the spec includes every handler. | ||
| app.Use(openapi.New()) | ||
|
|
||
| // Or extend your config for customization | ||
| app.Use(openapi.New(openapi.Config{ | ||
| Title: "My API", | ||
| Version: "1.0.0", | ||
| ServerURL: "https://example.com", | ||
| })) | ||
|
|
||
| // Customize metadata for specific operations | ||
| app.Use(openapi.New(openapi.Config{ | ||
| Operations: map[string]openapi.Operation{ | ||
| "GET /users": { | ||
| Summary: "List users", | ||
| Description: "Returns all users", | ||
| MediaType: fiber.MIMEApplicationJSON, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| // Routes may optionally document themselves using Summary, Description and MediaType | ||
| app.Get("/users", listUsers). | ||
| Summary("List users"). | ||
| Description("List all users"). | ||
| MediaType(fiber.MIMEApplicationJSON) | ||
|
|
||
| // If not specified, routes default to an empty summary and description and a | ||
| // "text/plain" response media type. | ||
| ``` | ||
|
|
||
| Each documented route automatically includes a `200` response with the description `OK` to satisfy the minimum OpenAPI requirements. | ||
|
|
||
| `CONNECT` routes are ignored because the OpenAPI specification does not define a `connect` operation. | ||
|
|
||
| ## Config | ||
|
|
||
| | Property | Type | Description | Default | | ||
| |:------------|:------------------------|:----------------------------------------------------------------|:------------------:| | ||
| | Next | `func(fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` | | ||
| | Title | `string` | Title is the title for the generated OpenAPI specification. | `"Fiber API"` | | ||
| | Version | `string` | Version is the version for the generated OpenAPI specification. | `"1.0.0"` | | ||
| | Description | `string` | Description is the description for the generated specification. | `""` | | ||
| | ServerURL | `string` | ServerURL is the server URL used in the generated specification.| `""` | | ||
| | Path | `string` | Path is the route where the specification will be served. | `"/openapi.json"` | | ||
| | Operations | `map[string]Operation` | Per-route metadata keyed by `METHOD /path`. | `nil` | | ||
|
|
||
| ## Default Config | ||
|
|
||
| ```go | ||
| var ConfigDefault = Config{ | ||
| Next: nil, | ||
| Title: "Fiber API", | ||
| Version: "1.0.0", | ||
| Description: "", | ||
| ServerURL: "", | ||
| Path: "/openapi.json", | ||
| Operations: nil, | ||
gaby marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| ### Operation | ||
|
|
||
| ```go | ||
| type Operation struct { | ||
| OperationID string | ||
| Summary string | ||
| Description string | ||
| Tags []string | ||
| Deprecated bool | ||
| MediaType string | ||
| } | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package openapi | ||
|
|
||
| import ( | ||
| "github.com/gofiber/fiber/v3" | ||
| ) | ||
|
|
||
| // Config defines the config for middleware. | ||
| type Config struct { | ||
| // Next defines a function to skip this middleware when returned true. | ||
| // | ||
| // Optional. Default: nil | ||
| Next func(c fiber.Ctx) bool | ||
|
|
||
| // Title is the title for the generated OpenAPI specification. | ||
| // | ||
| // Optional. Default: "Fiber API" | ||
| Title string | ||
|
|
||
| // Version is the version for the generated OpenAPI specification. | ||
| // | ||
| // Optional. Default: "1.0.0" | ||
| Version string | ||
|
|
||
| // Description is the description for the generated OpenAPI specification. | ||
| // | ||
| // Optional. Default: "" | ||
| Description string | ||
|
|
||
| // ServerURL is the server URL used in the generated specification. | ||
| // | ||
| // Optional. Default: "" | ||
| ServerURL string | ||
|
|
||
| // Path is the route where the specification will be served. | ||
| // | ||
| // Optional. Default: "/openapi.json" | ||
| Path string | ||
|
|
||
| // Operations allows providing per-route metadata keyed by | ||
| // "METHOD /path" (e.g. "GET /users"). | ||
| // | ||
| // Optional. Default: nil | ||
| Operations map[string]Operation | ||
| } | ||
|
|
||
| // ConfigDefault is the default config. | ||
| var ConfigDefault = Config{ | ||
| Next: nil, | ||
| Title: "Fiber API", | ||
| Version: "1.0.0", | ||
| Description: "", | ||
| ServerURL: "", | ||
| Path: "/openapi.json", | ||
| Operations: nil, | ||
| } | ||
|
|
||
| func configDefault(config ...Config) Config { | ||
| if len(config) < 1 { | ||
| return ConfigDefault | ||
| } | ||
|
|
||
| cfg := config[0] | ||
|
|
||
| if cfg.Next == nil { | ||
| cfg.Next = ConfigDefault.Next | ||
| } | ||
| if cfg.Title == "" { | ||
| cfg.Title = ConfigDefault.Title | ||
| } | ||
| if cfg.Version == "" { | ||
| cfg.Version = ConfigDefault.Version | ||
| } | ||
| if cfg.Description == "" { | ||
| cfg.Description = ConfigDefault.Description | ||
| } | ||
| if cfg.ServerURL == "" { | ||
| cfg.ServerURL = ConfigDefault.ServerURL | ||
| } | ||
| if cfg.Path == "" { | ||
| cfg.Path = ConfigDefault.Path | ||
| } | ||
| if cfg.Operations == nil { | ||
| cfg.Operations = ConfigDefault.Operations | ||
| } | ||
|
|
||
| return cfg | ||
| } | ||
|
|
||
| // Operation configures metadata for a single route in the generated spec. | ||
| type Operation struct { | ||
| OperationID string | ||
| Summary string | ||
| Description string | ||
| Tags []string | ||
| Deprecated bool | ||
| // MediaType defines the media type for the 200 response. | ||
| MediaType string | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.