Skip to content
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

Add Config.FileContent #1069

Closed
wants to merge 1 commit into from
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
31 changes: 22 additions & 9 deletions swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type Config struct {
// Optional. Default: ./swagger.json
FilePath string

// FileContent for the content of the swagger.json or swagger.yaml file.
// If provided, FilePath will not be read.
//
// Optional. Default: nil
FileContent []byte

// Path combines with BasePath for the full UI path
//
// Optional. Default: docs
Expand Down Expand Up @@ -85,16 +91,20 @@ func New(config ...Config) fiber.Handler {
}
}

// Verify Swagger file exists
if _, err := os.Stat(cfg.FilePath); os.IsNotExist(err) {
panic(fmt.Errorf("%s file does not exist", cfg.FilePath))
}
rawSpec := cfg.FileContent
if len(rawSpec) == 0 {
// Verify Swagger file exists
_, err := os.Stat(cfg.FilePath)
if os.IsNotExist(err) {
Copy link
Member

Choose a reason for hiding this comment

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

Use errors.Is(err, fs.ErrNotExist) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is existing code. I tried to minimize the changes. Do you still want me to change it?

panic(fmt.Errorf("%s file does not exist", cfg.FilePath))
}

// Read Swagger Spec into memory
rawSpec, err := os.ReadFile(cfg.FilePath)
if err != nil {
log.Fatalf("Failed to read provided Swagger file (%s): %v", cfg.FilePath, err.Error())
panic(err)
// Read Swagger Spec into memory
rawSpec, err = os.ReadFile(cfg.FilePath)
if err != nil {
log.Fatalf("Failed to read provided Swagger file (%s): %v", cfg.FilePath, err.Error())
panic(err)
}
gaby marked this conversation as resolved.
Show resolved Hide resolved
}

// Validate we have valid JSON or YAML
Expand All @@ -105,6 +115,9 @@ func New(config ...Config) fiber.Handler {

if errJSON != nil && errYAML != nil {
log.Fatalf("Failed to parse the Swagger spec as JSON or YAML: JSON error: %s, YAML error: %s", errJSON, errYAML)
if len(cfg.FileContent) != 0 {
panic(fmt.Errorf("Invalid Swagger spec: %s", string(rawSpec)))
}
gaby marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Errorf("Invalid Swagger spec file: %s", cfg.FilePath))
}

Expand Down
233 changes: 233 additions & 0 deletions swagger/swagger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package swagger

import (
_ "embed"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -10,6 +11,12 @@ import (
"github.com/stretchr/testify/require"
)

//go:embed swagger.json
var swaggerJSON []byte

//go:embed swagger.yaml
var swaggerYAML []byte

func performRequest(method, target string, app *fiber.App) *http.Response {
r := httptest.NewRequest(method, target, nil)
resp, _ := app.Test(r, -1)
Expand Down Expand Up @@ -259,3 +266,229 @@ func TestNew(t *testing.T) {
require.Equal(t, "success", string(bodyBytes))
})
}

func TestNewWithFileContent(t *testing.T) {
t.Run("Endpoint check with only custom path", func(t *testing.T) {
app := fiber.New()

cfg := Config{
Path: "custompath",
FileContent: swaggerJSON,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/custompath", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with only custom basepath", func(t *testing.T) {
app := fiber.New()

cfg := Config{
BasePath: "/api/v1",
FileContent: swaggerJSON,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/api/v1/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/api/v1/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with custom config", func(t *testing.T) {
app := fiber.New()

cfg := Config{
BasePath: "/",
FilePath: "swagger.json",
FileContent: swaggerJSON,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with custom path", func(t *testing.T) {
app := fiber.New()

cfg := Config{
BasePath: "/",
FilePath: "swagger.json",
Path: "swagger",
FileContent: swaggerJSON,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/swagger", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with custom config and yaml spec", func(t *testing.T) {
app := fiber.New()

cfg := Config{
BasePath: "/",
FilePath: "./swagger.yaml",
FileContent: swaggerYAML,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.yaml", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with custom path and yaml spec", func(t *testing.T) {
app := fiber.New()

cfg := Config{
BasePath: "/",
FilePath: "swagger.yaml",
Path: "swagger",
FileContent: swaggerYAML,
}
app.Use(New(cfg))

w1 := performRequest("GET", "/swagger", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.yaml", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Endpoint check with empty custom config", func(t *testing.T) {
app := fiber.New()

cfg := Config{FileContent: swaggerJSON}

app.Use(New(cfg))

w1 := performRequest("GET", "/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)
})

t.Run("Swagger file content not specified", func(t *testing.T) {
app := fiber.New()

cfg := Config{
FilePath: "./docs/swagger.json",
}

require.Panics(t, func() {
app.Use(New(cfg))
}, "content not specified")
})

t.Run("Endpoint check with multiple Swagger instances", func(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
BasePath: "/api/v1",
FileContent: swaggerJSON,
}))

app.Use(New(Config{
BasePath: "/api/v2",
FileContent: swaggerJSON,
}))

w1 := performRequest("GET", "/api/v1/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/api/v1/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/api/v2/docs", app)
require.Equal(t, 200, w3.StatusCode)

w4 := performRequest("GET", "/api/v2/swagger.json", app)
require.Equal(t, 200, w4.StatusCode)

w5 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w5.StatusCode)
})

t.Run("Endpoint check with custom routes", func(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
BasePath: "/api/v1",
FileContent: swaggerJSON,
}))

app.Get("/api/v1/tasks", func(c *fiber.Ctx) error {
return c.SendString("success")
})

app.Get("/api/v1", func(c *fiber.Ctx) error {
return c.SendString("success")
})

w1 := performRequest("GET", "/api/v1/docs", app)
require.Equal(t, 200, w1.StatusCode)

w2 := performRequest("GET", "/api/v1/swagger.json", app)
require.Equal(t, 200, w2.StatusCode)

w3 := performRequest("GET", "/notfound", app)
require.Equal(t, 404, w3.StatusCode)

// Verify we can send request to handler with the same BasePath as the middleware
w4 := performRequest("GET", "/api/v1/tasks", app)
bodyBytes, err := io.ReadAll(w4.Body)
require.NoError(t, err)
require.Equal(t, 200, w4.StatusCode)
require.Equal(t, "success", string(bodyBytes))

// Verify handler in BasePath still works
w5 := performRequest("GET", "/api/v1", app)
bodyBytes, err = io.ReadAll(w5.Body)
require.NoError(t, err)
require.Equal(t, 200, w5.StatusCode)
require.Equal(t, "success", string(bodyBytes))

w6 := performRequest("GET", "/api/v1/", app)
bodyBytes, err = io.ReadAll(w6.Body)
require.NoError(t, err)
require.Equal(t, 200, w6.StatusCode)
require.Equal(t, "success", string(bodyBytes))
})
Comment on lines +270 to +493
Copy link
Contributor

Choose a reason for hiding this comment

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

Add negative test cases to ensure robust error handling, such as providing invalid JSON/YAML in FileContent.

Would you like me to help by adding these test cases?

}
Loading