Skip to content

feat: add option to restore request body after reading#307

Merged
inhere merged 1 commit intogookit:masterfrom
almas-x:master
Aug 26, 2025
Merged

feat: add option to restore request body after reading#307
inhere merged 1 commit intogookit:masterfrom
almas-x:master

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Aug 26, 2025

This pull request introduces a new global option to control whether the HTTP request body should be automatically restored after being read. This enhancement increases the flexibility of the validation process by allowing users to enable or disable request body restoration, ensuring that the body remains available for further processing by subsequent middleware or handlers.

Key Changes:

  • Adds a configurable global option for restoring the request body after validation.
  • Improves compatibility with middleware and custom logic that may require access to the original request body.
  • Provides users with fine-grained control over request lifecycle management.

This update addresses goravel/goravel Issue #334.

@inhere inhere requested a review from Copilot August 26, 2025 06:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new global configuration option to control whether HTTP request bodies are restored after being read during validation. This enhancement allows the request body to remain available for subsequent middleware or handlers that may need to access the original request data.

Key changes:

  • Added RestoreRequestBody field to GlobalOption struct with default value false
  • Modified FromRequest function to conditionally restore the request body after reading for JSON content
  • Added comprehensive test coverage to verify both enabled and disabled restoration behavior

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
validate.go Added RestoreRequestBody option to GlobalOption struct and implemented body restoration logic in FromRequest function
validate_test.go Added test function to verify request body restoration functionality works correctly in both enabled and disabled states

@inhere inhere merged commit aff7898 into gookit:master Aug 26, 2025
17 of 18 checks passed
@inhere inhere added the enhancement New feature or request label Aug 26, 2025
@almas-x almas-x deleted the master branch August 26, 2025 07:28
@almas-x almas-x restored the master branch August 26, 2025 07:32
@almas-x
Copy link
Contributor Author

almas-x commented Sep 3, 2025

Hi, @inhere Is there any plan to release a new version soon? Our project depends on this package, and we’d like to include this change in our upcoming release.

Really appreciate your work!

@inhere
Copy link
Member

inhere commented Sep 3, 2025

@almas-x OK, New version will be released this week.

@ccoVeille
Copy link

Is there a reason to always restore it and don't have a setting for it ?

@almas-x
Copy link
Contributor Author

almas-x commented Sep 3, 2025

Is there a reason to always restore it and don't have a setting for it ?

The HTTP request body in Go is typically read-only once by default. The configuration option is designed to make users aware of situations where they explicitly restore the body, as automatically restoring it by default could unintentionally disrupt the original behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants