fix(hydra): stream totalItems as an integer - #8459
Merged
soyuka merged 1 commit intoAug 16, 2026
Merged
Conversation
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.
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.
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::$totalItemswas declaredfloat(src/Hydra/Collection.php:34).symfony/json-streamer8.1.4 (released 2026-08-01) began encoding withJSON_PRESERVE_ZERO_FRACTION— see upstream commit57ff6c1"[JsonStreamer] Use readable JSON encode defaults", now atvendor/symfony/json-streamer/Write/PhpGenerator.php:43-44. A float10.0therefore serialises as10.0instead of10.That output contradicts two contracts this project publishes for the same field:
src/Hydra/JsonSchema/SchemaFactory.php:172-173—'type' => 'integer','minimum' => 0src/JsonLd/HydraContext.php:606-609—'range' => 'xsd:integer'It also diverged from the non-streamed path:
src/Hydra/Serializer/CollectionNormalizer.php:66passes the same value through plainjson_encode(noPRESERVE_ZERO_FRACTION), emitting10. The streamed and normalised representations of one resource disagreed.Fix
Narrow the property to
intand cast at the assignment site insrc/Hydra/State/JsonStreamerProcessor.php:94. The float originates fromPaginatorInterface::getTotalItems(): float(src/State/Pagination/PaginatorInterface.php:36), which is unchanged.Hydra\Collectionis 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": 10rather than10.0, matching the published JSON Schema, the JSON-LD context, and the non-streamed output.