-
Notifications
You must be signed in to change notification settings - Fork 10
fix: coerce integerValue fields to JS number when reading documents
#48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
93ab784
fix: coerce `integerValue` fields to JS `number` when reading documents
besart-finsweet ee83d47
Merge branch 'master' of github.com:finsweet/fireworkers into fix/int…
besart-finsweet 57d0151
fix: `nullValue` fields to native JS types when reading documents
besart-finsweet 2cac6f2
fix: update test descriptions and include nullValue handling in raw R…
besart-finsweet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| 'fireworkers': minor | ||
| --- | ||
|
|
||
| fix: coerce `integerValue` fields to JS `number` when reading documents. | ||
|
|
||
| Firestore's REST API serializes `integerValue` as a string to preserve int64 precision. Previously, fireworkers returned the raw string; it now coerces to `number` to match the Firebase Admin SDK. | ||
|
|
||
| **BREAKING for consumers relying on `integerValue` fields being returned as strings.** This only affects data written by other clients (Admin SDK, console, other languages) — fireworkers writes all numbers as `doubleValue`, so round-trips within fireworkers were never affected. Values beyond `Number.MAX_SAFE_INTEGER` (2^53 − 1) will lose precision; if you need full int64 support, read the raw document via the REST API directly. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { beforeAll, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { clearFirestore, initDb, TEST_PROJECT_ID } from '../tests/unit/helpers'; | ||
| import { extract_fields_from_document } from './fields'; | ||
| import { get } from './get'; | ||
| import type { DB, Document } from './types'; | ||
|
|
||
| const EMULATOR_HOST = process.env.FIRESTORE_EMULATOR_HOST ?? '127.0.0.1:8080'; | ||
|
|
||
| /** | ||
| * Writes a raw Firestore document to the emulator, bypassing fireworkers so | ||
| * we can produce an `integerValue` field (fireworkers always writes numbers | ||
| * as `doubleValue`). | ||
| */ | ||
| const writeRawDocument = async ( | ||
| collection: string, | ||
| documentId: string, | ||
| fields: Document['fields'] | ||
| ): Promise<void> => { | ||
| const url = `http://${EMULATOR_HOST}/v1/projects/${TEST_PROJECT_ID}/databases/(default)/documents/${collection}?documentId=${documentId}`; | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ fields }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to seed document: ${response.status} ${await response.text()}`); | ||
| } | ||
| }; | ||
|
|
||
| describe('extract_fields_from_document', () => { | ||
| it('coerces integerValue strings to JS numbers', () => { | ||
| const document: Document = { | ||
| name: 'projects/test/databases/(default)/documents/todos/abc', | ||
| fields: { | ||
| updatedTimestamp: { integerValue: '1730404244' }, | ||
| }, | ||
| }; | ||
|
|
||
| const extracted = extract_fields_from_document<{ updatedTimestamp: number }>(document); | ||
|
|
||
| expect(extracted.fields.updatedTimestamp).toBe(1730404244); | ||
| expect(typeof extracted.fields.updatedTimestamp).toBe('number'); | ||
| }); | ||
|
|
||
| it('maps each primitive type to the correct JS type', () => { | ||
| const document: Document = { | ||
| name: 'projects/test/databases/(default)/documents/mix/doc', | ||
| fields: { | ||
| int: { integerValue: '42' }, | ||
| double: { doubleValue: 3.14 }, | ||
| str: { stringValue: 'hello' }, | ||
| bool: { booleanValue: true }, | ||
| missing: { nullValue: 'NULL_VALUE' }, | ||
| }, | ||
| }; | ||
|
|
||
| const extracted = extract_fields_from_document<{ | ||
| int: number; | ||
| double: number; | ||
| str: string; | ||
| bool: boolean; | ||
| missing: null; | ||
| }>(document); | ||
|
|
||
| expect(extracted.fields.int).toBe(42); | ||
| expect(typeof extracted.fields.int).toBe('number'); | ||
| expect(extracted.fields.double).toBe(3.14); | ||
| expect(extracted.fields.str).toBe('hello'); | ||
| expect(extracted.fields.bool).toBe(true); | ||
| expect(extracted.fields.missing).toBe('NULL_VALUE'); | ||
| }); | ||
|
besart-finsweet marked this conversation as resolved.
|
||
|
|
||
| it('coerces integerValue nested inside arrayValue and mapValue', () => { | ||
| const document: Document = { | ||
| name: 'projects/test/databases/(default)/documents/nested/doc', | ||
| fields: { | ||
| list: { | ||
| arrayValue: { | ||
| values: [{ integerValue: '1' }, { integerValue: '2' }], | ||
| }, | ||
| }, | ||
| meta: { | ||
| mapValue: { | ||
| fields: { | ||
| count: { integerValue: '99' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const extracted = extract_fields_from_document<{ | ||
| list: number[]; | ||
| meta: { count: number }; | ||
| }>(document); | ||
|
|
||
| expect(extracted.fields.list).toEqual([1, 2]); | ||
| expect(typeof extracted.fields.list[0]).toBe('number'); | ||
| expect(extracted.fields.meta.count).toBe(99); | ||
| expect(typeof extracted.fields.meta.count).toBe('number'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('get with integerValue seeded via raw REST (emulator)', () => { | ||
| let db: DB; | ||
|
|
||
| beforeAll(async () => { | ||
| db = await initDb(); | ||
| }); | ||
| beforeEach(clearFirestore); | ||
|
|
||
| it('returns integerValue fields as JS numbers', async () => { | ||
| await writeRawDocument('todos', 'seeded', { | ||
| updatedTimestamp: { integerValue: '1730404244' }, | ||
| count: { integerValue: '42' }, | ||
| title: { stringValue: 'seeded externally' }, | ||
| }); | ||
|
|
||
| const doc = await get<{ | ||
| updatedTimestamp: number; | ||
| count: number; | ||
| title: string; | ||
| }>(db, 'todos', 'seeded'); | ||
|
|
||
| expect(doc.fields.updatedTimestamp).toBe(1730404244); | ||
| expect(typeof doc.fields.updatedTimestamp).toBe('number'); | ||
| expect(doc.fields.count).toBe(42); | ||
| expect(typeof doc.fields.count).toBe('number'); | ||
| expect(doc.fields.title).toBe('seeded externally'); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.