Skip to content

Commit d3979dc

Browse files
authored
Fix AuthErrorHandler behavior and improve validation logic (#44)
* Add test to verify AuthErrorHandler does not disable validation or OpenAPI docs * Fix logic for restoring defaults in New function when only error handlers are set * Update AuthErrorHandler comment to include 5xx errors in configuration * Clarify comment in New function regarding default restoration for error handlers * Update AuthErrorHandler comment to clarify handling of 5xx errors
1 parent 75885f3 commit d3979dc

3 files changed

Lines changed: 47 additions & 5 deletions

File tree

custom_validation_error_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,44 @@ func TestValidationErrorHandlerImpliesValidationEnabled(t *testing.T) {
255255
assert.NoError(t, err)
256256
assert.Equal(t, fiber.StatusOK, resp.StatusCode, "OpenAPI docs should be enabled by default when only ValidationErrorHandler is configured")
257257
}
258+
259+
// TestAuthErrorHandlerOnlyDoesNotDisableDefaults verifies that setting only AuthErrorHandler
260+
// does not accidentally disable EnableValidation or EnableOpenAPIDocs (both default to true).
261+
func TestAuthErrorHandlerOnlyDoesNotDisableDefaults(t *testing.T) {
262+
app := fiber.New()
263+
264+
oapi := New(app, Config{
265+
AuthErrorHandler: func(c *fiber.Ctx, err *AuthError) error {
266+
return c.Status(err.StatusCode).JSON(fiber.Map{"custom": true})
267+
},
268+
})
269+
270+
type TestInput struct {
271+
Name string `json:"name" validate:"required"`
272+
}
273+
type TestOutput struct {
274+
Message string `json:"message"`
275+
}
276+
277+
Post[TestInput, TestOutput, struct{}](
278+
oapi,
279+
"/test",
280+
func(c *fiber.Ctx, input TestInput) (TestOutput, struct{}) {
281+
return TestOutput{Message: "ok"}, struct{}{}
282+
},
283+
OpenAPIOptions{},
284+
)
285+
286+
// Validation should still be enabled (default true)
287+
req := httptest.NewRequest("POST", "/test", bytes.NewReader([]byte(`{}`)))
288+
req.Header.Set("Content-Type", "application/json")
289+
resp, err := app.Test(req)
290+
assert.NoError(t, err)
291+
assert.Equal(t, fiber.StatusBadRequest, resp.StatusCode, "Validation should be enabled when only AuthErrorHandler is configured")
292+
293+
// OpenAPI docs should still be enabled (default true)
294+
req = httptest.NewRequest("GET", "/docs", nil)
295+
resp, err = app.Test(req)
296+
assert.NoError(t, err)
297+
assert.Equal(t, fiber.StatusOK, resp.StatusCode, "OpenAPI docs should be enabled when only AuthErrorHandler is configured")
298+
}

fiberoapi.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func New(app *fiber.App, config ...Config) *OApiApp {
5252
}
5353
// If no explicit config, keep the defaults (true, true, false)
5454

55-
// Special case: if ValidationErrorHandler is set and boolean fields seem to be using zero values,
56-
// restore defaults since it makes no sense to have a validation error handler without validation
55+
// Heuristic: when only handler(s) are provided and all booleans are at zero value,
56+
// assume the caller didn't intend to disable validation/docs — restore defaults.
5757
otherExplicitConfig := provided.EnableAuthorization ||
5858
provided.AuthService != nil ||
5959
provided.SecuritySchemes != nil ||
@@ -63,9 +63,10 @@ func New(app *fiber.App, config ...Config) *OApiApp {
6363

6464
// Only restore defaults if ALL boolean fields are false (suggesting they weren't explicitly set)
6565
allBooleansAreFalse := !provided.EnableValidation && !provided.EnableOpenAPIDocs && !provided.EnableAuthorization
66+
hasOnlyHandlers := (provided.ValidationErrorHandler != nil || provided.AuthErrorHandler != nil) && !otherExplicitConfig
6667

67-
if provided.ValidationErrorHandler != nil && !otherExplicitConfig && allBooleansAreFalse {
68-
// ValidationErrorHandler is the only explicit config, so restore defaults for boolean fields
68+
if hasOnlyHandlers && allBooleansAreFalse {
69+
// Only handler(s) are set, so restore defaults for boolean fields
6970
cfg.EnableValidation = true // Keep validation enabled - the handler needs it
7071
cfg.EnableOpenAPIDocs = true // Keep docs enabled - default behavior
7172
}

types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Config struct {
7373
SecuritySchemes map[string]SecurityScheme // OpenAPI security schemes
7474
DefaultSecurity []map[string][]string // Default security requirements
7575
ValidationErrorHandler ValidationErrorHandler // Custom handler for validation errors
76-
AuthErrorHandler AuthErrorHandler // Custom handler for auth errors (401/403)
76+
AuthErrorHandler AuthErrorHandler // Custom handler for auth errors (401/403/5xx)
7777
}
7878

7979
// OpenAPIOptions represents options for OpenAPI operations

0 commit comments

Comments
 (0)