Skip to content
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

[Bug]: MapRequestPayload doesn't handle type with scalar values #2462

Open
Arkounay opened this issue Mar 24, 2025 · 6 comments · May be fixed by #2467
Open

[Bug]: MapRequestPayload doesn't handle type with scalar values #2462

Arkounay opened this issue Mar 24, 2025 · 6 comments · May be fixed by #2467
Labels

Comments

@Arkounay
Copy link

Arkounay commented Mar 24, 2025

Version

5.0.0

Description

Hi,

When using MapRequestPayload with a scalar type value, for example "int"

#[Route('/route', methods: ['PATCH'])]
public function patchRoute(#[MapRequestPayload(type: 'int')] array $ids): Response
{
// ...
}

it triggers the following exception:

Schema of type "\int" can't be generated, no describer supports it. Class "\int" does not exist, did you forget a use statement, or typed it wrong?  

in src/Model/ModelRegistry.php (line 131)

JSON OpenApi

No response

Additional context

No response

@Arkounay Arkounay added the bug label Mar 24, 2025
@mario-fehr
Copy link

mario-fehr commented Mar 25, 2025

@Arkounay I am pretty sure that this has nothing to do with the bundle but is an error message directly from Symfony.
MapRequestPayload only can handle classes.

@Arkounay
Copy link
Author

Hmm I don't think so? if you send a payload in my example with an array [1, 2, 3] it will work well and be interpreted correctly by symfony, and if you send [1, "str"] it will throw a type error (which is expected)
Type works with scalar values since symfony 7.1
The error is only thrown when Nelmio tries to generate the documentation

@mario-fehr
Copy link

mario-fehr commented Mar 27, 2025

Interesting did not know that works. And doku does not mention it. Even the exception message
throw new NearMissValueResolverException(sprintf('Please set the $type argument of the #[%s] attribute to the type of the objects in the expected array.', MapRequestPayload::class)); talks about an object.

I made a reproducer.

@DjordyKoert what would be a good way to check that so we don't try to create a Model from scalar types.

@Arkounay
Copy link
Author

Arkounay commented Mar 27, 2025

Thanks for the reproducer @mario-fehr 👍

If this can help some people, a quick workaround I did for now is this and it seems to be working in my case

#[AutoconfigureTag('nelmio_api_doc.model_describer')]
class NativeTypeDescriber implements ModelDescriberInterface
{
    public function describe(Model $model, \OpenApi\Annotations\Schema $schema): void
    {
        $type = $model->getType()->getClassName();
        if ($type === 'int') {
            $type = 'integer';
        }
        $schema->type = $type;
    }

    public function supports(Model $model): bool
    {
        return $model->getType()->getClassName() === 'int' || $model->getType()->getClassName() === 'string';
    }
}

@DjordyKoert
Copy link
Collaborator

Type works with scalar values since symfony 7.1 The error is only thrown when Nelmio tries to generate the documentation

Nelmio does not yet support this, I made a draft PR #2467 with a semi-functional fix.

@Arkounay
Copy link
Author

Wow cool, thank you @DjordyKoert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants