Skip to content

Return an initialized empty Variation when an image variation fails - #48

Open
kcirkovic wants to merge 1 commit into
netgen:masterfrom
kcirkovic:fix-empty-image-variation
Open

kcirkovic wants to merge 1 commit into
netgen:masterfrom
kcirkovic:fix-empty-image-variation

Conversation

@kcirkovic

Copy link
Copy Markdown

Problem

ImageRuntime::getImageVariation() (behind ng_image_alias()) catches InvalidVariationException, SourceImageNotFoundException and InvalidArgumentException, logs the error and returns new Variation().

Since Ibexa 5 the properties of Ibexa\Contracts\Core\Variation\Values\Variation are typed (protected string $uri, ...) and are not initialized on a bare new Variation(). Reading any of them throws:

Error: Typed property Ibexa\Contracts\Core\Variation\Values\Variation::$uri must not be accessed before initialization

So the common template pattern

<img src="{{ ng_image_alias(content.fields.image, 'large').uri }}">

fails for a single missing or broken image, and the whole page returns a 500. The fallback was meant to prevent exactly that, and it did until Ibexa 4, where the properties were untyped and read as null.

Two details make this hard to guard against in templates:

  • It is a PHP Error, not a Twig error, so it also happens in prod with strict_variables off.
  • ValueObject::__isset() only checks property_exists(), so ?? '' and is defined do not help.

We hit it on a Media Site based project (Ibexa 5.0, Site API 7.0.3). One logo image failed to resolve and the homepage went down. The Media Site templates use this pattern in many places (hero, about, full views, gallery grid).

Fix

Return a Variation with all properties initialized to empty values (uri, fileName, dirPath and mimeType set to '', fileSize to 0, lastModified to the Unix epoch). Templates render an empty value again, as before Ibexa 5, and the error is still logged. The return type and the public API do not change.

I chose this over returning null (like Ibexa's own ibexa_image_alias()) because null would change the method's contract. With strict_variables on, it would also turn every ng_image_alias(...).uri in dev into a Twig error.

Tests

ImageRuntimeTest is new, as there was no test for ImageRuntime before. It covers:

  • the happy path (the handler's variation is returned unchanged)
  • each of the three caught exceptions: the error is logged, and every Variation property can be read

The failure cases fail on master with the error above and pass with this change. The full unit suite passes (808 tests). The one deprecation in the suite also appears on master.

I also checked it end to end on the project: with a source image missing, the page returned 500 before and 200 after.

🤖 Generated with Claude Code

ImageRuntime::getImageVariation() catches variation errors, logs them and
returns `new Variation()`. Since Ibexa 5 the properties of Variation are
typed, so reading any of them on that object throws "Typed property ...
must not be accessed before initialization". A template like
`ng_image_alias(field, 'large').uri` therefore fails with an Error for a
single missing or broken image, and takes the whole page down with a 500,
also in prod where strict_variables is off.

Return a Variation with all properties initialized to empty values, so
templates render an empty value again, as they did before Ibexa 5, while
the error is still logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kcirkovic
kcirkovic requested a review from pspanja November 24, 2026 15:53
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