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

Attributes removed since 0.19.2 if method argument has an attribute with attributes in them and arrays and some new lines #2281

Open
AndreasA opened this issue Nov 24, 2023 · 0 comments

Comments

@AndreasA
Copy link
Contributor

AndreasA commented Nov 24, 2023

Since 0.19.2 it seems that prettier removes attributes, if an argument also has attributes that are more complex. See examples below. Especially relevant with PHP 8.2 setting and even newer prettier versions.

Not providing playground example as playground pretter plugin version is too old.

Might be related to: #2101

Prettier 2.8.8 However, also tried the latest prettier version with 0.21.0

PHP Plugin 0.19.6 However, also tried 0.19.2 and the latest 0.21.0 and to confirm it works with 0.19.1

# Options (if any):
--no-options

Input:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: Request::METHOD_POST)]
    public function save(
        #[
            MapRequestPayload(
                serializationContext: [
                    AbstractNormalizer::CALLBACKS => [
                        'abc' => 'trim',
                    ],
                ],
            ),
        ]
        BarDto $data,
    ): Response {
        return $this->json(['foo']);
    }
}

Output:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    public function save(
        #[
            MapRequestPayload(
                serializationContext: [
                    AbstractNormalizer::CALLBACKS => [
                        'abc' => 'trim',
                    ],
                ],
            ),
        ]
        BarDto $data,
    ): Response {
        return $this->json(['foo']);
    }
}

Expected behavior:
That the Route attribute is not removed but formatted accordingly.

What is interesting is that the following works fine, though code wise it is the same:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: Request::METHOD_POST)]
    public function save(
        #[MapRequestPayload(serializationContext: [AbstractNormalizer::CALLBACKS => ['abc' => 'trim',],],), ]
        BarDto $data,
    ): Response {
        return $this->json(['foo']);
    }
}
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

No branches or pull requests

1 participant