Describe the bug
In json-server@1.0.0-beta.6 and later, query parameters consisting entirely of digits are coerced into Number types for strict equality checking.
However, the randomId() function (src/random-id.ts) uses randomBytes(2).toString('hex'). There is an approximate 15% chance that a generated 4-character hex string will consist entirely of digits (e.g., "1234", "8092").
When this happens, the newly created item cannot be queried via URL parameters.
For example, querying ?id=1234 will coerce the query to a Number 1234, which strictly does not match the stored String "1234".
Steps to reproduce
- Run
json-server (version >= 1.0.0-beta.6).
POST multiple new items to an endpoint until one is assigned an ID containing only digits (e.g., "id": "4952").
- Attempt to
GET that item using a query parameter: /items?id=4952.
- The server returns an empty array
[].
Expected behavior
The server should return the item that has the auto-generated string ID "4952".
Actual behavior
The server returns [] because the query parameter is parsed as the Number 4952, and 4952 !== "4952".
Possible Solution
- Modify
randomId() to ensure it always contains at least one letter (e.g., prefixing with a character like "id-" + randomBytes(2).toString('hex')).
- Or, adjust the query parsing logic to handle string/number fallbacks for IDs.
Describe the bug
In
json-server@1.0.0-beta.6and later, query parameters consisting entirely of digits are coerced intoNumbertypes for strict equality checking.However, the
randomId()function (src/random-id.ts) usesrandomBytes(2).toString('hex'). There is an approximate 15% chance that a generated 4-character hex string will consist entirely of digits (e.g.,"1234","8092").When this happens, the newly created item cannot be queried via URL parameters.
For example, querying
?id=1234will coerce the query to a Number1234, which strictly does not match the stored String"1234".Steps to reproduce
json-server(version >= 1.0.0-beta.6).POSTmultiple new items to an endpoint until one is assigned an ID containing only digits (e.g.,"id": "4952").GETthat item using a query parameter:/items?id=4952.[].Expected behavior
The server should return the item that has the auto-generated string ID
"4952".Actual behavior
The server returns
[]because the query parameter is parsed as theNumber4952, and4952 !== "4952".Possible Solution
randomId()to ensure it always contains at least one letter (e.g., prefixing with a character like"id-" + randomBytes(2).toString('hex')).