Skip to content

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Oct 31, 2025

Summary

  • rename the upstream Swagger generator middleware to swaggerdocgen across package, module path, and directory names
  • refresh documentation, workflow badges, and go.work entries to reference swaggerdocgen and note the rename in English
  • retarget the Swagger generator workflow so CI watches the new module location

Testing

  • GOPROXY=direct go test ./... (v3/swaggerdocgen) (fails: restricted network when fetching golang.org sources)
  • go test ./... (v3/swaggerui)

https://chatgpt.com/codex/tasks/task_e_6904abffa6448326a0e1654ec815e31d

Summary by CodeRabbit

  • New Features

    • Added two middleware modules: a configurable Swaggo middleware (rich UI configuration and default handler) and a Swagger UI middleware.
  • Documentation

    • Updated README navigation, links, and workflow badges; added usage and configuration docs for both modules.
  • Tests

    • Added end-to-end HTTP tests covering UI pages, assets, JSON doc endpoint, redirects, and proxy-prefix behavior.

@ReneWerner87 ReneWerner87 requested a review from a team as a code owner October 31, 2025 13:04
@ReneWerner87 ReneWerner87 requested review from efectn, gaby and sixcolors and removed request for a team October 31, 2025 13:04
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 31, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Splits the original swagger middleware into two packages: a new swaggo implementation (full configuration, template, handler, tests) and a renamed swaggerui package (formerly swagger). README entries and workflow badges updated to reference both modules. No exported APIs in existing packages were removed beyond renames.

Changes

Cohort / File(s) Summary
Root README updates
README.md, v3/README.md
Replaced single swagger entry with two entries (swaggo, swaggerui); updated links and workflow badges.
swaggerui migration
v3/swaggerui/README.md, v3/swaggerui/swagger.go, v3/swaggerui/swagger_test.go
Renamed package from swaggerswaggerui; updated package declarations, README examples, imports, and tests to use swaggerui.
swaggo docs
v3/swaggo/README.md
Added documentation for new swaggo middleware: usage, signatures, compatibility, and examples.
swaggo configuration
v3/swaggo/config.go
New exported Config struct and related types (FilterConfig, SyntaxHighlightConfig, OAuthConfig), ConfigDefault, and configDefault() merging helper.
swaggo template
v3/swaggo/index.go
Added indexTmpl constant containing the HTML/JS template used to initialize Swagger UI with configurable options and conditional injections.
swaggo handler
v3/swaggo/swagger.go
Added New(config ...Config) fiber.Handler and HandlerDefault; serves index, doc.json, static assets, handles X-Forwarded-Prefix, redirects, content-type, and error cases.
swaggo tests
v3/swaggo/swagger_test.go
End-to-end tests for HTML/JSON/asset serving, 301 redirect for root, 404 handling, and X-Forwarded-Prefix proxy redirect behavior.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Swaggo as Swaggo Middleware
    participant Swag as Swag Registry
    participant FS as Static FS

    Client->>Swaggo: GET /swag/index.html
    Swaggo->>Swaggo: Render indexTmpl with Config
    Swaggo-->>Client: 200 text/html

    Client->>Swaggo: GET /swag/doc.json
    Swaggo->>Swag: ReadDoc()
    Swag-->>Swaggo: Swagger JSON
    Swaggo-->>Client: 200 application/json

    Client->>Swaggo: GET /swag/static/<asset>
    Swaggo->>FS: Open asset
    FS-->>Swaggo: bytes / error
    Swaggo-->>Client: 200 (asset) / 404

    Client->>Swaggo: GET /swag/ (with X-Forwarded-Prefix)
    Swaggo->>Swaggo: Resolve prefix
    Swaggo-->>Client: 301 redirect to /prefix/swag/index.html
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Areas to focus:
    • v3/swaggo/config.go — large exported config surface, JSON tags, default merging logic.
    • v3/swaggo/index.go — long HTML/JS template with conditional blocks.
    • v3/swaggo/swagger.go — routing, filesystem handling, content-type, and X-Forwarded-Prefix logic.
    • Rename effects in v3/swaggerui/* — ensure imports/tests reference updated package.

Suggested labels

✏️ Feature, ❗ BreakingChange

Suggested reviewers

  • gaby
  • sixcolors
  • efectn

Poem

🐰 I hopped through code and split the way,
Two packages now to serve the day.
Swaggo crafts pages, assets, and JSON true,
SwaggerUI renamed to keep the view.
Tests and docs sorted — a carrot for you! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "introduce swaggo and rename swagger to swaggerui" directly and accurately reflects the main changes in the changeset. The raw summary confirms that the PR introduces a new swaggo middleware package (with new config.go, index.go, swagger.go, README.md, and tests), and renames the existing swagger package to swaggerui (with package declarations updated in swagger.go and swagger_test.go). The title is concise, clear, and specific enough that a teammate reviewing the history would understand the primary changes without ambiguity.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/2025-10-31-13-04-48

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request implements a significant refactoring by renaming the existing swagger module to swaggerdocgen and introducing a new swaggerui module. This change aims to enhance clarity and modularity by explicitly distinguishing between the responsibilities of generating API documentation and serving the Swagger UI. The updates are comprehensive, affecting module paths, file structures, documentation, and CI configurations to ensure consistency and proper functionality across the codebase.

Highlights

  • Module Renaming: The swagger module has been renamed to swaggerdocgen to more accurately reflect its primary function of generating Swagger documentation.
  • Functional Split: The responsibilities of the original swagger module have been split: documentation generation is now handled by swaggerdocgen, while serving the Swagger UI is managed by a new, distinct swaggerui module.
  • Extensive Updates: All relevant references, including package paths, directory names, go.work entries, and documentation (READMEs), have been comprehensively updated to reflect the new module names and structure.
  • CI Workflow Retargeting: The continuous integration workflows have been adjusted to correctly monitor and test the newly named swaggerdocgen module location.
Ignored Files
  • Ignored by pattern: .github/workflows/** (2)
    • .github/workflows/test-swaggerdocgen.yml
    • .github/workflows/test-swaggerui.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively renames the swagger module to swaggerdocgen and separates the swaggerui functionality. The changes are consistently applied across documentation, module paths, and configurations. I've identified a few minor issues, primarily typos in documentation and comments, along with a potential bug in the swaggerdocgen implementation that could introduce side effects. Additionally, I've suggested a small refactoring for improved code clarity. Overall, this is a well-executed refactoring.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
v3/swaggerdocgen/swagger_test.go (1)

90-124: Consider using testify/assert for consistency.

The tests use custom assertion logic (e.g., t.Fatalf with manual comparisons). While this works correctly, the codebase typically uses the testify/assert package for assertions, which would make the tests more readable and consistent.

Example refactor using testify/assert:

+import (
+	"github.com/stretchr/testify/assert"
+)

 for _, tt := range tests {
 	t.Run(tt.name, func(t *testing.T) {
 		req, err := http.NewRequest(http.MethodGet, tt.url, nil)
-		if err != nil {
-			t.Fatal(err)
-		}
+		assert.NoError(t, err)
 
 		resp, err := app.Test(req)
-		if err != nil {
-			t.Fatal(err)
-		}
+		assert.NoError(t, err)
 
-		if resp.StatusCode != tt.statusCode {
-			t.Fatalf(`StatusCode: got %v - expected %v`, resp.StatusCode, tt.statusCode)
-		}
+		assert.Equal(t, tt.statusCode, resp.StatusCode)
 
 		if tt.contentType != "" {
 			ct := resp.Header.Get("Content-Type")
 			mediaType, _, err := mime.ParseMediaType(ct)
-			if err != nil {
-				t.Fatalf("invalid content type %q: %v", ct, err)
-			}
-			if mediaType != tt.contentType {
-				t.Fatalf(`Content-Type: got %s - expected %s`, mediaType, tt.contentType)
-			}
+			assert.NoError(t, err, "invalid content type %q", ct)
+			assert.Equal(t, tt.contentType, mediaType)
 		}
 
 		if tt.location != "" {
 			location := resp.Header.Get("Location")
-			if location != tt.location {
-				t.Fatalf(`Location: got %s - expected %s`, location, tt.location)
-			}
+			assert.Equal(t, tt.location, location)
 		}
 	})
 }

Based on learnings

v3/swaggerdocgen/config.go (1)

281-319: Consider documenting boolean field default behavior.

The configDefault function applies defaults for string and slice fields but cannot apply defaults for boolean fields like DeepLinking and ShowMutatedRequest due to Go's inability to distinguish between explicitly-set false and zero-value false.

This means if a user passes a partial config like Config{Title: "My API"}, boolean fields will be false instead of the documented defaults (DeepLinking: true, ShowMutatedRequest: true from ConfigDefault).

While this is a common limitation in Go APIs, consider adding a comment to document this behavior, or alternatively use pointer types (*bool) for optional boolean fields to distinguish "not set" from "explicitly false".

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ea13a99 and fd9dc76.

⛔ Files ignored due to path filters (10)
  • .github/workflows/test-swaggerdocgen.yml is excluded by !**/*.yml
  • .github/workflows/test-swaggerui.yml is excluded by !**/*.yml
  • go.work is excluded by !**/*.work, !**/*.work
  • v3/swaggerdocgen/go.mod is excluded by !**/*.mod
  • v3/swaggerdocgen/go.sum is excluded by !**/*.sum, !**/*.sum
  • v3/swaggerui/go.mod is excluded by !**/*.mod
  • v3/swaggerui/go.sum is excluded by !**/*.sum, !**/*.sum
  • v3/swaggerui/swagger.json is excluded by !**/*.json
  • v3/swaggerui/swagger.yaml is excluded by !**/*.yaml
  • v3/swaggerui/swagger_missing.json is excluded by !**/*.json
📒 Files selected for processing (10)
  • README.md (1 hunks)
  • v3/README.md (1 hunks)
  • v3/swaggerdocgen/README.md (1 hunks)
  • v3/swaggerdocgen/config.go (1 hunks)
  • v3/swaggerdocgen/index.go (1 hunks)
  • v3/swaggerdocgen/swagger.go (1 hunks)
  • v3/swaggerdocgen/swagger_test.go (1 hunks)
  • v3/swaggerui/README.md (3 hunks)
  • v3/swaggerui/swagger.go (1 hunks)
  • v3/swaggerui/swagger_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📚 Learning: 2025-02-07T10:18:09.439Z
Learnt from: SadikSunbul
Repo: gofiber/recipes PR: 2797
File: email-verification/main.go:26-31
Timestamp: 2025-02-07T10:18:09.439Z
Learning: In the gofiber/recipes repository, recipes should focus on demonstrating specific features clearly without production-level configurations like advanced error handling, graceful shutdown, or security middleware, as they are meant to be educational examples.

Applied to files:

  • v3/README.md
📚 Learning: 2025-10-16T07:15:26.529Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:15:26.529Z
Learning: In Fiber v3, net/http handlers (http.Handler, http.HandlerFunc, or raw func(http.ResponseWriter, *http.Request)) can be passed directly to routing methods like app.Get(), app.Post(), etc. The framework automatically detects and wraps them internally via toFiberHandler/collectHandlers. The github.com/gofiber/fiber/v3/middleware/adaptor package is legacy and should not be suggested for tests or code using native net/http handler support.

Applied to files:

  • v3/swaggerdocgen/swagger.go
  • v3/swaggerui/README.md
📚 Learning: 2024-07-26T21:00:12.902Z
Learnt from: sixcolors
Repo: gofiber/fiber PR: 2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.

Applied to files:

  • v3/swaggerdocgen/swagger_test.go
📚 Learning: 2025-10-16T07:19:52.418Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:19:52.418Z
Learning: In the Fiber codebase, the linter does not allow `require` assertions from within HTTP handlers (including net/http-style handlers). Use `t.Fatalf`, `t.Errorf`, or similar `testing.T` methods for error handling inside handler functions instead.

Applied to files:

  • v3/swaggerdocgen/swagger_test.go
🧬 Code graph analysis (3)
v3/swaggerdocgen/swagger.go (1)
v3/swaggerdocgen/config.go (1)
  • Config (8-186)
v3/swaggerdocgen/swagger_test.go (1)
v3/swaggerdocgen/swagger.go (2)
  • New (27-109)
  • HandlerDefault (24-24)
v3/swaggerdocgen/config.go (1)
v3/swaggerui/swagger.go (2)
  • Config (19-68)
  • ConfigDefault (71-78)
🔇 Additional comments (18)
v3/swaggerui/swagger.go (1)

1-1: LGTM! Package rename executed correctly.

The package declaration has been successfully renamed from swagger to swaggerui, aligning with the PR's objective to clearly distinguish between the doc generator and UI components.

v3/swaggerui/swagger_test.go (1)

1-1: LGTM! Test package rename aligns with the main package.

The test package declaration has been correctly updated to swaggerui, maintaining consistency with the main package rename.

v3/README.md (1)

38-39: Documentation update looks good.

The middleware list has been correctly updated to include both swaggerdocgen (with the helpful "formerly swagger" note) and swaggerui, along with their respective workflow badges.

v3/swaggerdocgen/README.md (1)

1-144: Documentation looks comprehensive and well-structured.

The README clearly documents:

  • The rename from swagger to swaggerdocgen with a prominent warning
  • Complete installation and usage instructions
  • Configuration options with detailed descriptions
  • References to the config.go file for complete implementation details

This will help users migrate from the old package name smoothly.

v3/swaggerdocgen/swagger_test.go (2)

43-125: Good test coverage for core functionality.

The tests appropriately cover:

  • HTML index rendering with correct content type
  • JSON doc serving
  • Static asset serving (favicon)
  • Redirect behavior
  • 404 handling

127-164: Good test for proxy prefix handling.

The test validates that the X-Forwarded-Prefix header correctly influences redirect behavior, which is important for deployments behind reverse proxies.

v3/swaggerui/README.md (1)

1-154: Documentation comprehensively updated for the package rename.

The README has been thoroughly updated to reflect the swaggerui package name, including:

  • Updated package identifiers and titles
  • Corrected import paths throughout all examples
  • Updated API references (Config, New)
  • Updated workflow badge references
  • Maintained all example code with proper swaggerui namespace

This ensures users will have clear, accurate documentation for the renamed package.

README.md (1)

40-41: No issues found — workflow files exist as referenced.

Both workflow files (test-swaggerdocgen.yml and test-swaggerui.yml) referenced in the README.md are present in .github/workflows/ and correctly configured for the respective packages.

v3/swaggerdocgen/index.go (2)

14-24: No XSS concerns—CustomStyle and CustomScript are properly typed and safe.

The code correctly uses template.CSS and template.JS types with Go's html/template package (imported at swagger.go line 6). These special types are recognized by html/template and are not subject to escaping, which is the idiomatic Go pattern for safely injecting raw CSS and JavaScript into HTML templates. The type system itself ensures only valid CSS and JS content can be assigned to these fields.


57-57: Verify safe serialization of config object.

Line 57 serializes the entire config object with config = {{.}}. Ensure this doesn't inadvertently expose sensitive configuration details or create injection vulnerabilities.

Verify that:

  1. The config struct is properly serialized to JSON
  2. No sensitive data (credentials, internal paths, etc.) is included in the config
  3. The serialization properly escapes JavaScript-sensitive characters
v3/swaggerdocgen/swagger.go (5)

1-24: LGTM!

Package structure, imports, and constants are well-organized. The HandlerDefault initialization is appropriate.


27-48: LGTM!

The initialization logic is sound:

  • Template parsing with appropriate panic for init-time failures
  • Correct use of sync.Once to capture the base prefix once per handler instance
  • Proper handling of forwarded prefix for reverse proxy scenarios

50-68: LGTM!

The routing logic correctly handles:

  • Config cloning (shallow copy is sufficient since only the URL string field is modified)
  • Index template rendering
  • Dynamic doc generation via swag.ReadDoc
  • Permanent redirect from root to index

69-107: LGTM!

Static file serving implementation is solid:

  • Proper path normalization and validation
  • Correct error handling for missing files and directories
  • Appropriate Content-Type detection from file extension
  • Resource cleanup with deferred close

The use of io.ReadAll is acceptable for Swagger UI assets, which are typically small.


111-134: Concatenation logic for multiple X-Forwarded-Prefix headers is undocumented and untested.

The verification confirms your concern. The function iterates and concatenates all X-Forwarded-Prefix header values (line 119-126), but:

  • X-Forwarded-Prefix is a de-facto non-standard header with implementation-specific behavior, not defined by any RFC standard
  • Only one test case exists (single header value "/custom/path/"), with no coverage for multiple headers
  • No inline comments explain why concatenation was chosen over alternatives (using first/last value)

The behavior for multi-proxy scenarios remains untested. Consider either:

  1. Adding tests for multiple header values to document expected behavior
  2. Adding a code comment explaining the concatenation choice
  3. Reconsidering the logic if concatenation isn't the intended behavior
v3/swaggerdocgen/config.go (3)

1-186: LGTM!

The Config struct is comprehensive and well-documented:

  • Appropriate use of template.JS and template.CSS types for safe code injection
  • Clear default values documented for each field
  • Proper JSON tags with strategic use of omitempty and json:"-"

188-215: LGTM!

The helper configuration types provide flexibility:

  • FilterConfig.Value() properly handles both boolean and string filter modes
  • SyntaxHighlightConfig.Value() correctly returns the config object when activated or false when disabled

This design aligns with Swagger UI's configuration API.


256-278: LGTM!

The ConfigDefault variable provides sensible defaults for Swagger UI configuration, including appropriate plugins, presets, and UI behavior settings.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fd9dc76 and c121057.

⛔ Files ignored due to path filters (4)
  • .github/workflows/test-swaggo.yml is excluded by !**/*.yml
  • go.work is excluded by !**/*.work, !**/*.work
  • v3/swaggo/go.mod is excluded by !**/*.mod
  • v3/swaggo/go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (7)
  • README.md (1 hunks)
  • v3/README.md (1 hunks)
  • v3/swaggo/README.md (1 hunks)
  • v3/swaggo/config.go (1 hunks)
  • v3/swaggo/index.go (1 hunks)
  • v3/swaggo/swagger.go (1 hunks)
  • v3/swaggo/swagger_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • README.md
🧰 Additional context used
🧠 Learnings (6)
📚 Learning: 2025-02-07T10:18:09.439Z
Learnt from: SadikSunbul
Repo: gofiber/recipes PR: 2797
File: email-verification/main.go:26-31
Timestamp: 2025-02-07T10:18:09.439Z
Learning: In the gofiber/recipes repository, recipes should focus on demonstrating specific features clearly without production-level configurations like advanced error handling, graceful shutdown, or security middleware, as they are meant to be educational examples.

Applied to files:

  • v3/README.md
📚 Learning: 2024-11-23T19:50:41.765Z
Learnt from: norri
Repo: gofiber/recipes PR: 2701
File: clean-code/app/server/server.go:14-16
Timestamp: 2024-11-23T19:50:41.765Z
Learning: In the `clean-code` example at `clean-code/app/server/server.go` using the Go Fiber framework, it's acceptable to omit production-level features like context usage and graceful shutdown handling to keep the example simple.

Applied to files:

  • v3/README.md
📚 Learning: 2024-11-15T07:56:21.623Z
Learnt from: ReneWerner87
Repo: gofiber/fiber PR: 3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.

Applied to files:

  • v3/swaggo/README.md
  • v3/swaggo/swagger_test.go
📚 Learning: 2025-10-16T07:15:26.529Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:15:26.529Z
Learning: In Fiber v3, net/http handlers (http.Handler, http.HandlerFunc, or raw func(http.ResponseWriter, *http.Request)) can be passed directly to routing methods like app.Get(), app.Post(), etc. The framework automatically detects and wraps them internally via toFiberHandler/collectHandlers. The github.com/gofiber/fiber/v3/middleware/adaptor package is legacy and should not be suggested for tests or code using native net/http handler support.

Applied to files:

  • v3/swaggo/swagger.go
📚 Learning: 2024-07-26T21:00:12.902Z
Learnt from: sixcolors
Repo: gofiber/fiber PR: 2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.

Applied to files:

  • v3/swaggo/swagger_test.go
📚 Learning: 2025-10-16T07:19:52.418Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:19:52.418Z
Learning: In the Fiber codebase, the linter does not allow `require` assertions from within HTTP handlers (including net/http-style handlers). Use `t.Fatalf`, `t.Errorf`, or similar `testing.T` methods for error handling inside handler functions instead.

Applied to files:

  • v3/swaggo/swagger_test.go
🧬 Code graph analysis (2)
v3/swaggo/swagger.go (1)
v3/swaggo/config.go (1)
  • Config (8-186)
v3/swaggo/swagger_test.go (1)
v3/swaggo/swagger.go (2)
  • New (27-109)
  • HandlerDefault (24-24)
🪛 LanguageTool
v3/swaggo/README.md

[grammar] ~14-~14: Use a hyphen to join words.
Context: ...go/swag](https://github.com/swaggo/swag) generated documentation, and exposes hel...

(QB_NEW_EN_HYPHEN)

🔇 Additional comments (4)
v3/README.md (1)

38-39: Rename entry looks consistent

Links and workflow badges now track the swaggo / swaggerui split as expected.

v3/swaggo/README.md (1)

1-144: README covers the rename and API clearly

Good job documenting the new swaggo module, default handler, and configuration knobs so consumers can migrate smoothly.

v3/swaggo/swagger_test.go (1)

50-163: Great end-to-end coverage

Appreciate the table-driven checks plus proxy-prefix scenario; this gives strong confidence in the handler.

v3/swaggo/swagger.go (1)

26-129: Handler flow looks solid

Template parsing, prefix detection, doc serving, and asset fallback all read clean—once the template serialization fix lands, this will be ready.

@ReneWerner87 ReneWerner87 changed the title chore: rename swagger generator module to swaggerdocgen introduce swaggo and rename swagger to swaggerui Oct 31, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
v3/swaggo/swagger.go (1)

50-53: Consider deep copying the config or making URL modification thread-safe.

The shallow copy at line 50 means slices and pointers in Config (e.g., Plugins, Presets, OAuth) are shared across all requests. While only the URL string field is modified here (which is safe), this pattern could lead to race conditions if future code modifies slice or pointer fields in cfgCopy.

Consider one of these approaches:

Option 1: Create a new Config struct for the modified URL

-cfgCopy := cfg
-if len(cfgCopy.URL) == 0 {
-	cfgCopy.URL = path.Join(prefix, defaultDocURL)
-}
+url := cfg.URL
+if len(url) == 0 {
+	url = path.Join(prefix, defaultDocURL)
+}
+cfgCopy := cfg
+cfgCopy.URL = url

Option 2: Add a comment documenting the limitation

+// Shallow copy is safe since we only modify the URL string field
 cfgCopy := cfg
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8d4367c and 6943d26.

⛔ Files ignored due to path filters (1)
  • .github/workflows/golangci-lint.yml is excluded by !**/*.yml
📒 Files selected for processing (1)
  • v3/swaggo/swagger.go (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-16T07:15:26.529Z
Learnt from: grivera64
Repo: gofiber/fiber PR: 3807
File: adapter_test.go:118-144
Timestamp: 2025-10-16T07:15:26.529Z
Learning: In Fiber v3, net/http handlers (http.Handler, http.HandlerFunc, or raw func(http.ResponseWriter, *http.Request)) can be passed directly to routing methods like app.Get(), app.Post(), etc. The framework automatically detects and wraps them internally via toFiberHandler/collectHandlers. The github.com/gofiber/fiber/v3/middleware/adaptor package is legacy and should not be suggested for tests or code using native net/http handler support.

Applied to files:

  • v3/swaggo/swagger.go
🧬 Code graph analysis (1)
v3/swaggo/swagger.go (1)
v3/swaggo/config.go (1)
  • Config (8-186)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: lint (swaggerui)
  • GitHub Check: lint (swaggo)
🔇 Additional comments (4)
v3/swaggo/swagger.go (4)

1-17: LGTM!

Package declaration and imports are appropriate for a Swagger UI middleware. All necessary dependencies are included.


19-24: LGTM!

Constants and default handler initialization follow common Fiber middleware patterns.


55-68: LGTM!

Request routing for index, doc.json, and root redirect is well-implemented. The use of utils.CopyString prevents holding references to Fiber's internal buffers, and the permanent redirect (301) for root paths is appropriate.


70-74: LGTM on security measures!

The path cleaning with path.Clean and the directory check provide good protection against directory traversal attacks and prevent directory listing.

Also applies to: 98-100

Comment on lines +83 to +88
defer func() {
cerr := file.Close()
if cerr != nil && err == nil {
err = cerr
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Fix: Deferred close error is silently lost.

The deferred function attempts to capture file.Close() errors by assigning to err, but this assignment has no effect because the handler function func(c fiber.Ctx) error does not use named return values. Close errors will be silently ignored.

Apply this diff to properly propagate close errors using named returns:

-return func(c fiber.Ctx) error {
+return func(c fiber.Ctx) (err error) {
 	once.Do(func() {
 		basePrefix = strings.ReplaceAll(c.Route().Path, "*", "")
 	})

And update the template parsing error variable to avoid shadowing:

-	index, err := template.New("swagger_index.html").Parse(indexTmpl)
-	if err != nil {
-		panic(fmt.Errorf("fiber: swagger middleware error -> %w", err))
+	index, parseErr := template.New("swagger_index.html").Parse(indexTmpl)
+	if parseErr != nil {
+		panic(fmt.Errorf("fiber: swagger middleware error -> %w", parseErr))
 	}

And at line 63:

 		case defaultDocURL:
-			var doc string
-			if doc, err = swag.ReadDoc(cfgCopy.InstanceName); err != nil {
+			doc, err := swag.ReadDoc(cfgCopy.InstanceName)
+			if err != nil {
 				return err
 			}

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In v3/swaggo/swagger.go around lines 83 to 88 (and update at line 63), the
deferred file.Close() error is being assigned to a non-existent named return
variable so close errors are lost; change the handler signature to use a named
return (e.g., func(c fiber.Ctx) (err error)) and in the defer assign cerr to err
only when err == nil to propagate Close errors, and also rename the template
parsing error variable at line 63 to avoid shadowing the named return (use a
different local variable name like tplErr) so you don't accidentally shadow the
handler's err.

@ReneWerner87 ReneWerner87 merged commit 5f6bea9 into main Nov 1, 2025
8 checks passed
@ReneWerner87 ReneWerner87 deleted the codex/2025-10-31-13-04-48 branch November 1, 2025 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants