Skip to content

fix: send class instances as JSON instead of octet-stream - #956

Open
rida-ghreizy wants to merge 1 commit into
dapr:mainfrom
rida-ghreizy:fix/content-type-class-instances
Open

rida-ghreizy wants to merge 1 commit into
dapr:mainfrom
rida-ghreizy:fix/content-type-class-instances

Conversation

@rida-ghreizy

Copy link
Copy Markdown

Description

When no content type is given, the SDK picks one with getContentType (src/utils/Client.util.ts), which switches on data.constructor.name. Only "Object" and "Array" were treated as JSON. Any other object fell through to application/octet-stream, and the serializers can't send an object as raw bytes:

class Order { constructor(public id: number) {} }
await client.pubsub.publish("pubsub", "orders", new Order(1)); // or state.save, invoker.invoke, ...
Data Before After
new Order(1) over gRPC application/octet-stream, then Buffer.from(order) throws The first argument must be of type string or an instance of Buffer... application/json, {"id":1}
new Order(1) over HTTP application/octet-stream, body sent as [object Object] application/json, {"id":1}
Object.create(null) TypeError: Cannot read properties of undefined (reading 'name') in getType application/json
class instance with CloudEvent fields application/octet-stream application/cloudevents+json
Buffer, typed arrays, DataView, ArrayBuffer, Blob application/octet-stream unchanged
plain objects, arrays, strings, numbers, booleans unchanged unchanged

Changes:

  • In the default branch, return application/octet-stream only for binary data (ArrayBuffer, ArrayBuffer.isView, SharedArrayBuffer, Blob). Any other non-null object goes through the same JSON / CloudEvent check as plain objects.
  • getType no longer throws for objects without a prototype (o?.constructor?.name).
  • Update the doc comment.

Issue reference

No issue was filed; the problem is described above.

Checklist

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

Tests:

  • test/unit/utils/Client.util.test.ts: the existing "unnamed class instance" case now expects application/json. It used to assert application/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, and Uint8Array / DataView / Blob staying binary.
  • test/unit/utils/Serializer.util.test.ts: serializeGrpc and serializeHttp with a class instance produce JSON.

Without the change, 6 of these tests fail. With it, NODE_ENV=test npx jest test/unit passes: 89 suites, 2579 tests. ESLint and Prettier are clean on the changed files.

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>
@rida-ghreizy
rida-ghreizy requested review from a team as code owners November 27, 2026 01:27
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.46%. Comparing base (257a7b0) to head (30f9094).

Files with missing lines Patch % Lines
src/utils/Client.util.ts 88.88% 0 Missing and 1 partial ⚠️
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     
Flag Coverage Δ
e2e 21.51% <0.00%> (-0.02%) ⬇️
unit 85.29% <88.88%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

1 participant