Skip to content

Breaking change in ValidatorPropertyMetadataFactory: validation group callable now invoked with null #7241

@binxiaoff

Description

@binxiaoff

API Platform version(s) affected: 4.1.16 and 4.1.17

Description
Hi team,

The recent PR #7184 introduced a change in the ApiPlatform\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory class, where user-defined validation group callables are now invoked with a null argument.

This is a breaking change for existing projects. Previously, the callback did not need to support a nullable parameter.

There are two concerns here:

  1. This change seems unnecessary. Since validation groups are meant to be dynamically determined from the input data, it’s inherently impossible to compute them meaningfully when no actual data is available.
  2. It introduces unsafe behavior. Invoking user code with unexpected arguments, especially null, without an explicit contract is dangerous and error-prone.

How to reproduce

#[ApiResource(
    validationContext: [
        'groups' => [
            SomeResource::class,
            'getValidationGroups',
        ],
    ]
)]
class SomeResource
{
    // Before: worked fine
    public static function getValidationGroups(self $someResource): array
    {
        // ...
    }

    // After: now $someEntity must support null
    public static function getValidationGroups(?self $someResource): array
    {
        // ...
    }
}

Possible Solution
Please consider reverting this change.

Thanks for your work on API Platform!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions