question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApiResource parameters denormalizationContext and normalizationContext doesn't affect

See original GitHub issue

API Platform version(s) affected: 2.6.4 Everything ok when you downgrade to 2.6.3

Description
ApiResource parameters denormalizationContext and normalizationContext doesn’t affect.

How to reproduce
In version 2.6.4 - swagger shows all fields and ignores denormalizationContext and normalizationContext But In versions before that, for example 2.6.3 - everything is ok. Swagger shows considering denormalizationContext and normalizationContext goups

How swagger looks with ApiPlatform version 2.6.3 image

Same code with 2.6.4 image

Original file is there: https://github.com/kadirov/api-starter-kit/blob/master/src/Entity/User.php


#[ApiResource(
    collectionOperations: [
        'get'                => [
            'security'              => "is_granted('ROLE_ADMIN')",
            'normalization_context' => ['groups' => ['users:read']],
        ],
        'post'               => [
            'controller' => UserCreateAction::class,
        ],
        'aboutMe'            => [
            'controller'      => UserAboutMeAction::class,
            'method'          => 'get',
            'path'            => 'users/about_me',
            'openapi_context' => [
                'summary'    => 'Shows info about the authenticated user',
            ],
        ],
        'auth'               => [
            'controller'      => UserAuthAction::class,
            'method'          => 'post',
            'path'            => 'users/auth',
            'openapi_context' => ['summary' => 'Authorization'],
        ],
        'authByRefreshToken' => [
            'controller'      => UserAuthByRefreshTokenAction::class,
            'method'          => 'post',
            'path'            => 'users/auth/refreshToken',
            'openapi_context' => ['summary' => 'Authorization by refreshToken'],
            'input'           => RefreshTokenRequestDto::class,
        ],
        'isUniqueEmail'      => [
            'controller'              => UserIsUniqueEmailAction::class,
            'method'                  => 'post',
            'path'                    => 'users/is_unique_email',
            'openapi_context'         => ['summary' => 'Checks email for uniqueness'],
            'denormalization_context' => ['groups' => ['user:isUniqueEmail:write']],
        ],
    ],
    itemOperations: [
        'changePassword' => [
            'controller'              => UserChangePasswordAction::class,
            'method'                  => 'put',
            'path'                    => 'users/{id}/password',
            'security'                => "object == user || is_granted('ROLE_ADMIN')",
            'openapi_context'         => ['summary' => 'Changes password'],
            'denormalization_context' => ['groups' => ['user:changePassword:write']],
        ],
        'delete'         => [
            'controller' => DeleteAction::class,
            'security'   => "object == user || is_granted('ROLE_ADMIN')",
        ],
        'get'            => [
            'security' => "object == user || is_granted('ROLE_ADMIN')",
        ],
        'put'            => [
            'security'                => "object == user || is_granted('ROLE_ADMIN')",
            'denormalization_context' => ['groups' => ['user:put:write']],
        ],
    ],
    denormalizationContext: ['groups' => ['user:write']],
    normalizationContext: ['groups' => ['user:read', 'users:read']],
)]
#[ApiFilter(OrderFilter::class, properties: ['id', 'createdAt', 'updatedAt', 'email'])]
#[ApiFilter(SearchFilter::class, properties: ['id' => 'exact', 'email' => 'partial'])]
#[UniqueEntity('email', message: 'This email is already used')]

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 */
class User implements
    UserInterface,
    UpdatedAtSettableInterface,
    CreatedAtSettableInterface,
    IsDeletedSettableInterface
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    #[Groups(['users:read'])]
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    #[Assert\Email]
    #[Groups(['users:read', 'user:write', 'user:put:write', 'user:isUniqueEmail:write'])]
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    #[Groups(['user:write', 'user:changePassword:write'])]
    private $password;

    /**
     * @ORM\Column(type="array")
     */
    #[Groups(['user:read'])]
    private $roles = [];

    /**
     * @ORM\Column(type="datetime")
     */
    #[Groups(['user:read'])]
    private $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @Groups({"user:read"})
     */
    #[Groups(['user:read'])]
    private $updatedAt;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isDeleted = false;

   // here getter and setter methods...
}

Possible Solution
Downgrade to 2.6.3

Additional Context
Symfony version 5.2.6 PHP version 8.0.3 OS: tested on windows 10, Fedora 33, docker with Debian 10

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
papylhommecommented, Apr 27, 2021

Same problem here, downgrading to 2.6.3 works

3reactions
rabraghibcommented, Jun 23, 2021

thanks @alanpoulain its fixed as you said 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - ApiResource parameters denormalizationContext and ...
ApiResource parameters denormalizationContext and normalizationContext doesn't affect. How to reproduce. In version 2.6.4 - swagger shows all fields and ignores ...
Read more >
Symfony Api Platform Normalization Context Groups datetime ...
I'm struggle with strange issue when using normalizer with groups. It look like there is a problem with datetime properties.
Read more >
The Serialization Process - API Platform
Add the normalization context and denormalization context attributes to the resource, and specify which groups to use. Here you see that we add...
Read more >
How to build an API? A Developer's Guide to API Platform
Property normalizationContext is responsible for informing which groups should be taken when changing the object into the array.
Read more >
Resource Metadata Factory: Dynamic ApiResource Options
It's job is pretty simple: given an API Resource class - like App\Entity\User ... arguments: ['@App\ApiPlatform\AutoGroupResourceMetadataFactory.inner'] ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found