Skip to content

HealthNormalizer loads health.json from %kernel.project_dir% instead of the bundle → health endpoint 500s #6

Description

@eluhr

Summary

GET /api/.../api_configurations/{id}/health returns HTTP 500 (json_decode(): Argument #1 ($json) must be of type string, false given) on a stock install, because HealthNormalizer loads its response-validation schema (health.json) from the application's project dir instead of from the bundle — even though the schema file ships inside the bundle.

  • dmstr/api-configuration-bundle: 0.2.0
  • Symfony 7.4, API Platform 4.x, PHP 8.4

Root cause

src/Normalizer/HealthNormalizer.php:

public function __construct(
    #[Autowire('%kernel.project_dir%')]
    string $projectDir,
) {
    // resolves to <app>/src/Entity/ApiConfiguration/health.json
    $this->schemaPath = $projectDir . '/src/Entity/ApiConfiguration/health.json';
}

private function validate(array $data): void
{
    $schema = json_decode(file_get_contents($this->schemaPath)); // <-- file_get_contents() → false
    ...
}

%kernel.project_dir% is the consuming application's directory, so the lookup misses the schema that ships in the bundle at src/Entity/ApiConfiguration/health.json. file_get_contents() then returns false, and json_decode(false) throws under PHP 8 → HTTP 500.

Trimmed stack trace

"json_decode(): Argument #1 ($json) must be of type string, false given"
  vendor/dmstr/api-configuration-bundle/src/Normalizer/HealthNormalizer.php:174  (validate)
  vendor/dmstr/api-configuration-bundle/src/Normalizer/HealthNormalizer.php:62   (normalize)
  vendor/dmstr/api-configuration-bundle/src/State/ApiConfigurationHealthProvider.php:94
  ...
  → HTTP 500

Steps to reproduce

  1. Install the bundle in a Symfony + API Platform app (do not create src/Entity/ApiConfiguration/health.json in the app).
  2. Create an ApiConfiguration.
  3. GET /api/.../api_configurations/{id}/health → 500.

Expected

The bundle ships src/Entity/ApiConfiguration/health.json, so the normalizer should load it relative to the bundle, e.g.:

$this->schemaPath = \dirname(__DIR__) . '/Entity/ApiConfiguration/health.json';

(or expose it as a configurable/overridable path). Additionally, guarding file_get_contents() against false would turn a missing schema into a clear error instead of a TypeError.

Workaround

Copy the bundle's src/Entity/ApiConfiguration/health.json into the consuming app at the same relative path (<app>/src/Entity/ApiConfiguration/health.json).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions