fix(plugin-fetch,plugin-axios): parse int64 responses without losing precision - #810
Closed
stijnvanhulle wants to merge 2 commits into
Closed
fix(plugin-fetch,plugin-axios): parse int64 responses without losing precision#810stijnvanhulle wants to merge 2 commits into
stijnvanhulle wants to merge 2 commits into
Conversation
…precision JSON.parse always rounds an integer past Number.MAX_SAFE_INTEGER to the nearest representable number, so a format: int64 field lost precision on the response side even after #797 coerced it to z.coerce.bigint() - the value was already wrong by the time Zod saw it (#786). Add a bigint-safe parseJson to the shared serializers template: it defers to the native JSON.parse for ordinary payloads and only walks the text itself when an unsafe-length digit run is present, reading an out-of-range integer literal straight into a bigint. Both plugin-fetch's parseResponse/parseEvent and plugin-axios's transformResponse/parseEvent now use it.
Regenerated every example and test snapshot after wiring parseJson into plugin-fetch and plugin-axios's response parsing (previous commit). Every changed file is a copied client.ts/serializers.ts under a .kubb output folder.
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
🦋 Changeset detectedLatest commit: 2fe5d44 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
Size Change: 0 B Total Size: 311 kB ℹ️ View Unchanged
|
More templates
@kubb/plugin-axios
@kubb/plugin-cypress
@kubb/plugin-faker
@kubb/plugin-fetch
@kubb/plugin-mcp
@kubb/plugin-msw
@kubb/plugin-react-query
@kubb/plugin-redoc
@kubb/plugin-swr
@kubb/plugin-ts
@kubb/plugin-vue-query
@kubb/plugin-zod
commit: |
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.
🎯 Changes
Follow-up to #797 and #800, closing the gap axel-havukangas-futurice flagged in the last comment on #786.
#797 made
format: int64fields printz.coerce.bigint()so a response validates at all, but the value it coerces from is already wrong:JSON.parserounds any integer pastNumber.MAX_SAFE_INTEGERto the nearest representablenumberbefore Zod ever sees it.z.coerce.bigint()then faithfully converts that already-rounded number to abigintwith the wrong value.Both clients now read the response body through a bigint-safe
parseJson(added to the sharedserializers.tstemplate):JSON.parse.bigintinstead of through the lossy intermediatenumber.Wired into:
plugin-fetch'sparseResponse(both the'json'branch and the opportunistic-parse fallback) and its SSEparseEvent.plugin-axios'stransformResponse(axios's own default, withJSON.parseswapped forparseJson) and its SSEparseEvent.Every example and test snapshot under a
.kubboutput folder is regenerated; the only diffs are the copiedclient.ts/serializers.tstemplates.✅ Checklist
pnpm run test.🚀 Release Impact
Closes the precision-loss gap in #786.
Generated by Claude Code
Generated by Claude Code