Repository navigation
fix: send class instances as JSON instead of octet-stream - #956
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
getContentType only treated objects whose constructor is Object or Array as JSON. Class instances got application/octet-stream, so the gRPC serializer threw on Buffer.from() and the HTTP serializer sent "[object Object]". Objects without a prototype made getType throw. Treat every non-binary object as JSON (or a CloudEvent) and keep octet-stream for buffers, typed arrays, ArrayBuffer and Blob. Signed-off-by: rida <ghreizyrida1@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #956 +/- ##
==========================================
- Coverage 85.48% 85.46% -0.03%
==========================================
Files 160 160
Lines 5884 5889 +5
Branches 1222 1225 +3
==========================================
+ Hits 5030 5033 +3
- Misses 664 665 +1
- Partials 190 191 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
When no content type is given, the SDK picks one with
getContentType(src/utils/Client.util.ts), which switches ondata.constructor.name. Only"Object"and"Array"were treated as JSON. Any other object fell through toapplication/octet-stream, and the serializers can't send an object as raw bytes:new Order(1)over gRPCapplication/octet-stream, thenBuffer.from(order)throwsThe first argument must be of type string or an instance of Buffer...application/json,{"id":1}new Order(1)over HTTPapplication/octet-stream, body sent as[object Object]application/json,{"id":1}Object.create(null)TypeError: Cannot read properties of undefined (reading 'name')ingetTypeapplication/jsonapplication/octet-streamapplication/cloudevents+jsonBuffer, typed arrays,DataView,ArrayBuffer,Blobapplication/octet-streamChanges:
defaultbranch, returnapplication/octet-streamonly for binary data (ArrayBuffer,ArrayBuffer.isView,SharedArrayBuffer,Blob). Any other non-null object goes through the same JSON / CloudEvent check as plain objects.getTypeno longer throws for objects without a prototype (o?.constructor?.name).Issue reference
No issue was filed; the problem is described above.
Checklist
Tests:
test/unit/utils/Client.util.test.ts: the existing "unnamed class instance" case now expectsapplication/json. It used to assertapplication/octet-stream, the value that made the serializer throw. Added cases for a named class instance, an object without a prototype, a CloudEvent class instance, andUint8Array/DataView/Blobstaying binary.test/unit/utils/Serializer.util.test.ts:serializeGrpcandserializeHttpwith a class instance produce JSON.Without the change, 6 of these tests fail. With it,
NODE_ENV=test npx jest test/unitpasses: 89 suites, 2579 tests. ESLint and Prettier are clean on the changed files.