Skip to content

feat(metadata): use a custom Attribute extends ApiResource (#5076) #5175

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 2 commits into from
Nov 10, 2022

Conversation

yobrx
Copy link
Contributor

@yobrx yobrx commented Nov 9, 2022

Q A
Branch? main
Tickets #5076
License MIT
Doc PR

Use a custom Attribute extending ApiResource class, see #5076

#[RestfulApi(
    routePrefix: '/live-chat',
)]
#[ORM\Entity]
class MyEntity
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue(strategy: 'AUTO')]
    private $id;

    public function __construct()
    {
    }

    public function getId()
    {
        return $this->id;
    }
}
use ApiPlatform\Metadata\ApiResource;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class RestfulApi extends ApiResource
{
    public function __construct(
        ?string $routePrefix = null,
    ) {
        parent::__construct(
            routePrefix: $routePrefix,
            normalizationContext: ['groups' => ['restful_read']],
            denormalizationContext: ['groups' => ['restful_write']],
            openapiContext: ['tags' => ['LiveChat']],
            order: ['id' => 'DESC'],
            security: 'is_granted("ROLE_OPERATOR")'
        );
    }
}

Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

LGTM!

@yobrx
Copy link
Contributor Author

yobrx commented Nov 9, 2022

Wait, many behat tests are broken... :(

@yobrx
Copy link
Contributor Author

yobrx commented Nov 9, 2022

Behat tests are fixed !

It was broken because the name "ApiResource" was compared strictly in "AttributesResourceMetadataCollectionFactory.php"
Fix here ;
https://github.com/api-platform/core/compare/857438d850df2da14861dced02740348a4d80c97..7e0b746726d083ff7245b83a406172e0871d0192

@yobrx yobrx marked this pull request as ready for review November 9, 2022 22:45
@soyuka
Copy link
Member

soyuka commented Nov 10, 2022

This is nice thanks! I'm wondering if we shouldn't support this as a bug fix instead as it should've been possible on 3.0. Could you target 3.0?

@yobrx yobrx changed the base branch from main to 3.0 November 10, 2022 08:55
@yobrx
Copy link
Contributor Author

yobrx commented Nov 10, 2022

This is nice thanks! I'm wondering if we shouldn't support this as a bug fix instead as it should've been possible on 3.0. Could you target 3.0?

Done !

@soyuka soyuka merged commit a527504 into api-platform:3.0 Nov 10, 2022
@soyuka
Copy link
Member

soyuka commented Nov 10, 2022

thanks @yobrx

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.

3 participants