From 87203a5d0df63f786ef21006fc29bf6451e2800d Mon Sep 17 00:00:00 2001 From: JIeJaitt <498938874@qq.com> Date: Tue, 1 Apr 2025 09:33:28 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9A=20Doc:=20Add=20caution=20note=20ab?= =?UTF-8?q?out=20custom=20constraints=20overriding=20built-in=20constraint?= =?UTF-8?q?s=20in=20routing=20guide?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/guide/routing.md | 2 ++ middleware/requestid/requestid.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/guide/routing.md b/docs/guide/routing.md index 0c8dc6b2..8953dc08 100644 --- a/docs/guide/routing.md +++ b/docs/guide/routing.md @@ -250,7 +250,9 @@ app.Get("/:test?", func(c fiber.Ctx) error { Custom constraints can be added to Fiber using the `app.RegisterCustomConstraint` method. Your constraints have to be compatible with the `CustomConstraint` interface. +:::caution Attention, custom constraints can now override built-in constraints. If a custom constraint has the same name as a built-in constraint, the custom constraint will be used instead. This allows for more flexibility in defining route parameter constraints. +::: It is a good idea to add external constraints to your project once you want to add more specific rules to your routes. For example, you can add a constraint to check if a parameter is a valid ULID. diff --git a/middleware/requestid/requestid.go b/middleware/requestid/requestid.go index 6f8d5bf9..ef67e6f2 100644 --- a/middleware/requestid/requestid.go +++ b/middleware/requestid/requestid.go @@ -55,14 +55,14 @@ func New(config ...Config) fiber.Handler { // - context.Context: Retrieves request ID from context values func FromContext(c any) string { switch ctx := c.(type) { - case context.Context: - if rid, ok := ctx.Value(requestIDKey).(string); ok { - return rid - } case fiber.Ctx: if rid, ok := ctx.Locals(requestIDKey).(string); ok { return rid } + case context.Context: + if rid, ok := ctx.Value(requestIDKey).(string); ok { + return rid + } default: log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c) }