Skip to content

Fix: Index out of bounds panic in SwaggerUIHandler for files without extension#3217

Open
NitinKumar004 wants to merge 5 commits intogofr-dev:developmentfrom
NitinKumar004:fix/swagger-ui-index-out-of-bounds
Open

Fix: Index out of bounds panic in SwaggerUIHandler for files without extension#3217
NitinKumar004 wants to merge 5 commits intogofr-dev:developmentfrom
NitinKumar004:fix/swagger-ui-index-out-of-bounds

Conversation

@NitinKumar004
Copy link
Copy Markdown
Contributor

@NitinKumar004 NitinKumar004 commented Mar 27, 2026

Summary

  • Replaced strings.Split + direct index access with filepath.Ext to safely extract the file extension.
  • Returns a defined error (errMissingFileExtension) when the filename has no extension, instead of panicking with index out of bounds.
  • Added unit test for the no-extension scenario.

Fixes #3214

Test plan

  • Added TestSwaggerUIHandler_NoFileExtension unit test
  • Existing swagger tests still pass
  • Linter passes with 0 issues

…without extension

Validate file extension using filepath.Ext before accessing it.
Previously, splitting on "." and accessing index [1] would panic
for filenames without a dot.

Fixes gofr-dev#3214
@NitinKumar004 NitinKumar004 force-pushed the fix/swagger-ui-index-out-of-bounds branch from 21f8cfb to f8508c4 Compare March 27, 2026 12:04

ext := filepath.Ext(fileName)
if ext == "" {
return nil, errMissingFileExtension
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The error errMissingFileExtension is a plain errors.New(), which GoFr maps to 500 Internal Server Error. A missing file extension is a client error — the request is malformed. It
should return 400 Bad Request or 404 Not Found

…IHandler

Replace plain errors.New() with gofrHTTP.ErrorEntityNotFound so the
framework correctly maps it to 404 Not Found instead of 500 Internal
Server Error. A missing file extension is a client error, not a server error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: Index out of bounds panic in SwaggerUIHandler for files without extension

2 participants