Skip to content

fix: parse application/*+json request bodies as JSON - #811

Open
fadhlaouir wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
fadhlaouir:fix/parse-structured-json-suffix
Open

fadhlaouir wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
fadhlaouir:fix/parse-structured-json-suffix

Conversation

@fadhlaouir

Copy link
Copy Markdown

Fixes #809

Problem

HTTP functions receive request bodies sent with a structured syntax suffix type, such as application/vnd.api+json (required by JSON:API) or application/merge-patch+json, as a raw Buffer, while the same body sent as application/json arrives parsed. A +json suffix means the payload is JSON (RFC 6839 §3.1).

The default JSON parser is registered without a type, so body-parser only matches application/json, and these requests fall through to the catch-all raw parser.

Change

The JSON parser now gets its own options with type: ['application/json', 'application/*+json'].

  • defaultBodySavingOptions is left unchanged, because the text parser also uses it.
  • The CloudEvents parser is still registered first, so application/cloudevents+json is handled as before.
  • One visible behaviour change: a malformed +json body now gets a 400 response, as a malformed application/json body already does. Before, it reached the function as a raw Buffer.

Testing

The default JSON parser was registered without a type, so body-parser
matched only application/json. Bodies sent with a structured syntax
suffix type, such as application/vnd.api+json (required by JSON:API) or
application/merge-patch+json, fell through to the raw parser and reached
the function as a Buffer.

Give the JSON parser its own options with
type ['application/json', 'application/*+json']. The CloudEvents parser
still runs first for application/cloudevents+json, and the text parser
keeps its existing options.

Fixes GoogleCloudPlatform#809
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.

HTTP functions receive application/*+json bodies (e.g. JSON:API application/vnd.api+json) as a raw Buffer

1 participant