Skip to content

Don't parse body when x-raw-body:true - #64

Merged
bir merged 6 commits into
gofoji:mainfrom
davidn:LP-520
Oct 21, 2025
Merged

Don't parse body when x-raw-body:true#64
bir merged 6 commits into
gofoji:mainfrom
davidn:LP-520

Conversation

@davidn

@davidn davidn commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

Currently, if you define a body in openapi Foji will always parse it. This prevents handlers from doing different parsing than the spec, which is sometimes desired. This commit adds a x-raw-body extension to prevent parsing and instead pass r.Body directly through to the handler.

x-raw-request currently passes the http.Request through to the handler
as-is. This works well when there is no body defined in the openapi
spec, which is how it has mostly been used to date. However when a body
is defined in the openapi spec the body is parsed as per the spec.
Beause r.Body() can only be read one time, this makes the raw request
useless for any purposes that would access this body.

This commit brings x-raw-request in line with the intended behaviour of
leaving request processing to the handler by omitting the body parsing.

This is a BREAKING CHANGE for any code that uses x-raw-request:true and
a defined body, as the signature and responsibility of the user code
changes.
@codecov

codecov Bot commented Aug 4, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@597aa7a). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #64   +/-   ##
=======================================
  Coverage        ?   16.02%           
=======================================
  Files           ?       42           
  Lines           ?     3227           
  Branches        ?        0           
=======================================
  Hits            ?      517           
  Misses          ?     2699           
  Partials        ?       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Per https://lavajira.atlassian.net/browse/LP-520?focusedCommentId=55950
it was by design for x-raw-request to preserve body parsing. This
request thus makes a separate x-raw-body extension that prevents body
parsing.

In order to allow x-raw-body without x-raw-request, the body
io.ReadCloser is now passed in as a parameter.
@davidn davidn changed the title LP-520: Don't parse body when x-raw-request:true LP-520: Don't parse body when x-raw-body:true Aug 8, 2025
Comment thread foji/openapi/handler.go.tpl
@davidn davidn changed the title LP-520: Don't parse body when x-raw-body:true Don't parse body when x-raw-body:true Aug 8, 2025
Comment thread tests/example/openapi.yaml
@bir
bir merged commit 6c052ee into gofoji:main Oct 21, 2025
4 checks passed
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.

3 participants