Skip to content

fix(hydra): stream totalItems as an integer - #8459

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/hydra-stream-total-items-integer
Aug 16, 2026
Merged

fix(hydra): stream totalItems as an integer#8459
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/hydra-stream-total-items-integer

Conversation

@soyuka

@soyuka soyuka commented Aug 16, 2026

Copy link
Copy Markdown
Member

One of three PRs finishing the 4.3 CI unblock started in #8458. This is the only one that changes runtime behaviour, which is why it is split out.

Problem

Hydra\Collection::$totalItems was declared float (src/Hydra/Collection.php:34). symfony/json-streamer 8.1.4 (released 2026-08-01) began encoding with JSON_PRESERVE_ZERO_FRACTION — see upstream commit 57ff6c1 "[JsonStreamer] Use readable JSON encode defaults", now at vendor/symfony/json-streamer/Write/PhpGenerator.php:43-44. A float 10.0 therefore serialises as 10.0 instead of 10.

That output contradicts two contracts this project publishes for the same field:

  • src/Hydra/JsonSchema/SchemaFactory.php:172-173'type' => 'integer', 'minimum' => 0
  • src/JsonLd/HydraContext.php:606-609'range' => 'xsd:integer'

It also diverged from the non-streamed path: src/Hydra/Serializer/CollectionNormalizer.php:66 passes the same value through plain json_encode (no PRESERVE_ZERO_FRACTION), emitting 10. The streamed and normalised representations of one resource disagreed.

Fix

Narrow the property to int and cast at the assignment site in src/Hydra/State/JsonStreamerProcessor.php:94. The float originates from PaginatorInterface::getTotalItems(): float (src/State/Pagination/PaginatorInterface.php:36), which is unchanged.

Hydra\Collection is marked @internal (src/Hydra/Collection.php:21), so narrowing the property type is not a BC break.

Effect

Streamed JSON-LD collections emit "hydra:totalItems": 10 rather than 10.0, matching the published JSON Schema, the JSON-LD context, and the non-streamed output.

Hydra\Collection declared $totalItems as float, so symfony/json-streamer 8.1.4 --
which now encodes with JSON_PRESERVE_ZERO_FRACTION -- emitted 10.0 instead of 10.

That contradicts our own contracts: the JSON Schema declares totalItems as
"integer" and the JSON-LD context declares it xsd:integer. It also diverged from
the non-streamed CollectionNormalizer, which emits an integer for the same
resource.

Collection is @internal, so narrowing the property is not a BC break.
@soyuka
soyuka merged commit 1a53ca2 into api-platform:4.3 Aug 16, 2026
102 of 112 checks passed
@soyuka
soyuka deleted the fix/hydra-stream-total-items-integer branch August 16, 2026 15:12
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