Skip to content

refactor(metadata): type parameters to list<string>|string #7134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025

Conversation

soyuka
Copy link
Member

@soyuka soyuka commented May 8, 2025

Q A
Branch? 4.1
Tickets Closes #7111
License MIT

Adds a nativeType to parameters, defaults to list<string>|string as this is the default with Symfony query parameters parsing and for HTTP Headers. It also fixes the value extraction accordingly and the validation that may have work partially on a value that's extracted as an array.

TODO:

  • fix doctrine filters

@soyuka soyuka force-pushed the fix/param-validation branch 6 times, most recently from 5fe4473 to 1a8366b Compare May 9, 2025 12:56
@soyuka soyuka force-pushed the fix/param-validation branch from 1a8366b to 0ebb1f0 Compare May 12, 2025 08:03
@soyuka soyuka merged commit b7f0f93 into api-platform:4.1 May 16, 2025
94 checks passed
Comment on lines +79 to +100
if ($value instanceof ParameterNotFound) {
return $value;
}

$isCollectionType = fn ($t) => $t instanceof CollectionType;
$isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;

// type-info 7.2
if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
foreach ($parameter->getNativeType()->getTypes() as $t) {
if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
break;
}
}
}

if ($isCollection) {
$value = \is_array($value) ? $value : [$value];
} elseif ($parameter instanceof HeaderParameter && \is_array($value) && array_is_list($value) && 1 === \count($value)) {
$value = $value[0];
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello 👋

There is a BC break here.

On on resource, we have this (simplified version) :

#[ApiResource(
    shortName: 'PublicDraft',
    operations: [
        new GetCollection(
            parameters: new Parameters([
                new QueryParameter(
                    key: 'triggerUrl',
                    constraints: [new Source()],
                ),
            ])
        ),
    ],
)]

our request looks like this

curl --request GET \
  --url 'https://api.redirection-io.test/app_dev.php/drafts?projectId=d98a60e4-56f4-11ea-8b8d-0242ac150003&triggerUrl=foobar' \
  --header 'Authorization: Bearer 4781249f79bfbdfb1f3c11b2cfc88c5e1a434c9c579b19f5315ec5677051'

And in our validator, we use to receive a string, now we receive a array

We have symfony/type-info: 7.2.5 (but we do not reach line 88), because the $parameter->getNativeType is already a collection :

image

I tried to specify the nativeType, but I got the same result. It looks like it's not used.

nativeType: new BuiltinType(TypeIdentifier::STRING),

Copy link
Contributor

@lyrixx lyrixx May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed #7157, sorry ! and the last release fixed it. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also parameters are experimental :D

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.

2 participants