fix(types): preserve unknown enum integer values in codecs - #657
fix(types): preserve unknown enum integer values in codecs#657Varun-S10 wants to merge 1 commit into
Conversation
🧪 Code CoverageNo coverage changes. Generated by coverage-comment.yml |
|
Hello @Varun-S10, Thank you for looking into this and proposing a fix. The issue is real but the fix proposed in this PR is difficult to maintain. The changes made to the A more sustainable approach would be to create codecs file that would reexport from original What do you think about such approach? |
Description
Thank you for opening a Pull Request!
Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
CONTRIBUTINGGuide.fix:which represents bug fixes, and correlates to a SemVer patch.feat:represents a new feature, and correlates to a SemVer minor.feat!:, orfix!:,refactor!:, etc., which represent a breaking change (indicated by the!) and will result in a SemVer major.Fixes #640 🦕
What was the bug?
In the proto3 JSON specification, enums (such as
TaskStateandRole) must remain forward-compatible. When an SDK receives an unknown or newly added numeric enum value from a newer peer (e.g.state: 99orrole: 42), it must preserve that integer value so it can be safely passed through proxies, gateways, and relays without data loss.Previously in
@a2a-js/sdk:taskStateFromJSONandroleFromJSONmapped any unrecognized numeric value toUNRECOGNIZED(-1).taskStateToJSONandroleToJSONthen serialized this into the literal string"UNRECOGNIZED".a2a-pythonpeer received{"state": "UNRECOGNIZED"}, it threw aParseError: Invalid enum value UNRECOGNIZED. The original numeric value99was destroyed, breaking interoperability.How is it fixed?
src/types/pb/a2a.ts:taskStateFromJSONandroleFromJSONdefault switch cases to preserve unknown integer numbers (typeof object === "number" ? object : UNRECOGNIZED).taskStateToJSONandroleToJSONto returnstring | numberso that unknown enum integers are emitted directly as numbers instead of"UNRECOGNIZED".src/client/transports/rest_transport.ts&src/samples/cli.ts:taskStateToJSONinString(...)when formatting URL query parameters and CLI logs.test/types/enums.spec.ts:TaskStateandRolecovering known string names, known numbers, unknown numeric integer preservation, and message round-tripping.Verification
state: 99,role: 42) round-trip cleanly as integers (99,42) instead of collapsing into"UNRECOGNIZED"."TASK_STATE_WORKING","ROLE_USER").test/types/enums.spec.tspassed (10/10).npm test) passed (69/69test files,1,497tests).npm run lint) passed with 0 errors.npm run build && npm run test-build) passed cleanly.