Repository navigation
fix: parse JSON data of binary MQTT messages - #683
Open
rida-ghreizy wants to merge 1 commit into
Open
rida-ghreizy wants to merge 1 commit into
rida-ghreizy wants to merge 1 commit into
Conversation
The HTTP and Kafka bindings parse binary event data with a JSON content type, but the MQTT binding returned the payload as received, so JSON data came back as a string or Buffer. Parse string and Buffer payloads when the content type is application/json, keeping other payloads as they are. Signed-off-by: rida <ghreizyrida1@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed Changes
In binary mode, the HTTP and Kafka bindings parse the event data when the content type is
application/json. The MQTT binding passed the payload through unchanged. An MQTT client receives the payload as bytes (aBuffer) or a string, so an event sent with JSON data came back withdataas a string or a Buffer instead of an object:This PR adds
extractBinaryDatato the MQTT binding, mirroring the Kafka one. When the event'sdatacontenttype, or the PUBLISHContent Typeif that is missing, starts withapplication/json, and the payload is a string or a Buffer, it is parsed as JSON. Payloads that are already objects, other content types, and text that isn't valid JSON are returned unchanged, so existing callers that pass objects don't change.Description
New tests in
test/integration/mqtt_tests.ts:Without the change the first test fails. With it, the full suite passes (
npx mocha --exit --require ts-node/register ./test/integration/**/*.ts: 236 passing), and ESLint is clean on the changed files.