sonata-project / SonataMediaBundle

Symfony SonataMediaBundle
https://docs.sonata-project.org/projects/SonataMediaBundle
MIT License
451 stars 495 forks source link

ClassificationBundle\Model\CategoryInterface does not exist while using API Platform #2399

Closed kerattila closed 5 months ago

kerattila commented 1 year ago

Environment

Sonata packages

show

``` $ composer show --latest 'sonata-project/*' Direct dependencies required in composer.json: sonata-project/admin-bundle 4.22.5 4.25.0 The missing Symfony A... sonata-project/block-bundle 4.20.0 4.21.0 Symfony SonataBlockBu... sonata-project/doctrine-extensions 1.18.1 2.3.0 Doctrine2 behavioral ... sonata-project/doctrine-orm-admin-bundle 4.11.0 4.13.0 Integrate Doctrine OR... sonata-project/media-bundle 4.7.0 4.10.0 Symfony SonataMediaBu... sonata-project/translation-bundle 3.1.1 3.3.0 SonataTranslationBundle sonata-project/user-bundle 5.6.2 5.9.0 Symfony SonataUserBundle Transitive dependencies not required in composer.json: sonata-project/cache 2.2.0 2.2.0 Cache library Package sonata-project/cache is abandoned, you should avoid using it. No replacement was suggested. sonata-project/exporter 3.1.1 3.1.1 Lightweight Exporter ... sonata-project/form-extensions 1.19.0 1.19.1 Symfony form extensions sonata-project/twig-extensions 2.0.0 2.1.0 Sonata twig extensions ```

Symfony packages

show

``` $ composer show --latest 'symfony/*' Direct dependencies required in composer.json: symfony/asset v5.4.21 v6.3.0 Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files symfony/browser-kit v5.4.21 v6.3.0 Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically symfony/cache v6.2.8 v6.3.0 Provides extended PSR-6, PSR-16 (and tags) implementations symfony/console v5.4.22 v6.3.0 Eases the creation of beautiful and testable command line interfaces symfony/css-selector v5.4.21 v6.3.0 Converts CSS selectors to XPath expressions symfony/debug-bundle v5.4.21 v6.3.0 Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework symfony/doctrine-bridge v5.4.22 v6.3.0 Provides integration for Doctrine with various Symfony components symfony/dotenv v5.4.22 v6.3.0 Registers environment variables from a .env file symfony/expression-language v5.4.21 v6.3.0 Provides an engine that can compile and evaluate expressions symfony/flex v1.19.5 v2.3.1 Composer plugin for Symfony symfony/form v5.4.22 v6.3.0 Allows to easily create, process and reuse HTML forms symfony/framework-bundle v5.4.22 v6.3.0 Provides a tight integration between Symfony components and the Symfony full-stack framework symfony/http-kernel v5.4.22 v6.3.0 Provides a structured process for converting a Request into a Response symfony/intl v5.4.22 v6.3.0 Provides a PHP replacement layer for the C intl extension that includes additional data from the ICU library symfony/maker-bundle v1.48.0 v1.48.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. symfony/monolog-bundle v3.8.0 v3.8.0 Symfony MonologBundle symfony/process v5.4.22 v6.3.0 Executes commands in sub-processes symfony/property-access v5.4.22 v6.3.0 Provides functions to read and write from/to an object or array using a simple string notation symfony/property-info v5.4.22 v6.3.0 Extracts information about PHP class' properties using metadata of popular sources symfony/proxy-manager-bridge v5.4.21 v6.3.0 Provides integration for ProxyManager with various Symfony components symfony/security-acl v3.1.2 v3.3.2 Symfony Security Component - ACL (Access Control List) symfony/security-bundle v5.4.22 v6.3.0 Provides a tight integration of the Security component into the Symfony full-stack framework symfony/serializer v5.4.22 v6.3.0 Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON. symfony/stopwatch v5.4.21 v6.3.0 Provides a way to profile code symfony/thanks v1.2.10 v1.2.10 Encourages sending ⭐ and 💵 to fellow PHP package maintainers (not limited to Symfony components)! symfony/translation v5.4.22 v6.3.0 Provides tools to internationalize your application symfony/twig-bridge v5.4.22 v6.3.0 Provides integration for Twig with various Symfony components symfony/twig-bundle v5.4.21 v6.3.0 Provides a tight integration of Twig into the Symfony full-stack framework symfony/validator v5.4.22 v6.3.0 Provides tools to validate values symfony/var-dumper v5.4.22 v6.3.0 Provides mechanisms for walking through any arbitrary PHP variable symfony/web-link v5.4.21 v6.3.0 Manages links between resources symfony/web-profiler-bundle v5.4.21 v6.3.0 Provides a development tool that gives detailed information about the execution of any request symfony/workflow v5.4.21 v6.3.0 Provides tools for managing a workflow or finite state machine symfony/yaml v5.4.21 v6.3.0 Loads and dumps YAML files Transitive dependencies not required in composer.json: symfony/cache-contracts v3.2.1 v3.3.0 Generic abstractions related to caching symfony/config v5.4.21 v6.3.0 Helps you find, load, combine, autofill and validate configuration values of any kind symfony/dependency-injection v5.4.22 v6.3.0 Allows you to standardize and centralize the way objects are constructed in your application symfony/deprecation-contracts v3.2.1 v3.3.0 A generic function and convention to trigger deprecation notices symfony/dom-crawler v5.4.22 v6.3.0 Eases DOM navigation for HTML and XML documents symfony/error-handler v5.4.21 v6.3.0 Provides tools to manage errors and ease debugging PHP code symfony/event-dispatcher v5.4.22 v6.3.0 Provides tools that allow your application components to communicate with each other by dispatching events and listening to them symfony/event-dispatcher-contracts v3.2.1 v3.3.0 Generic abstractions related to dispatching event symfony/filesystem v5.4.21 v6.3.0 Provides basic utilities for the filesystem symfony/finder v5.4.21 v6.3.0 Finds files and directories via an intuitive fluent interface symfony/http-client v5.4.22 v6.3.0 Provides powerful methods to fetch HTTP resources synchronously or asynchronously symfony/http-client-contracts v2.5.2 v3.3.0 Generic abstractions related to HTTP clients symfony/http-foundation v5.4.22 v6.3.0 Defines an object-oriented layer for the HTTP specification symfony/mailer v5.4.22 v6.3.0 Helps sending emails symfony/mime v5.4.21 v6.3.0 Allows manipulating MIME messages symfony/monolog-bridge v5.4.22 v6.3.0 Provides integration for Monolog with various Symfony components symfony/options-resolver v5.4.21 v6.3.0 Provides an improved replacement for the array_replace PHP function symfony/password-hasher v5.4.21 v6.3.0 Provides password hashing utilities symfony/polyfill-intl-grapheme v1.27.0 v1.27.0 Symfony polyfill for intl's grapheme_* functions symfony/polyfill-intl-icu v1.27.0 v1.27.0 Symfony polyfill for intl's ICU-related data and classes symfony/polyfill-intl-idn v1.27.0 v1.27.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions symfony/polyfill-intl-normalizer v1.27.0 v1.27.0 Symfony polyfill for intl's Normalizer class and related functions symfony/polyfill-mbstring v1.27.0 v1.27.0 Symfony polyfill for the Mbstring extension symfony/polyfill-php72 v1.27.0 v1.27.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions symfony/polyfill-php73 v1.27.0 v1.27.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions symfony/polyfill-php80 v1.27.0 v1.27.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions symfony/polyfill-php81 v1.27.0 v1.27.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions symfony/routing v5.4.22 v6.3.0 Maps an HTTP request to a set of configuration variables symfony/security-core v5.4.22 v6.3.0 Symfony Security Component - Core Library symfony/security-csrf v5.4.21 v6.3.0 Symfony Security Component - CSRF Library symfony/security-guard v5.4.22 v5.4.22 Symfony Security Component - Guard symfony/security-http v5.4.22 v6.3.0 Symfony Security Component - HTTP Integration symfony/service-contracts v2.5.2 v3.3.0 Generic abstractions related to writing services symfony/string v5.4.22 v6.3.0 Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way symfony/translation-contracts v2.5.2 v3.3.0 Generic abstractions related to translation symfony/var-exporter v6.2.8 v6.3.0 Allows exporting any serializable PHP data structure to plain PHP code ```

PHP version

$ php -v
PHP 8.1.17 (cli) (built: Mar 16 2023 22:18:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.17, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.17, Copyright (c), by Zend Technologies
    with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans

Subject

A combination of Sonata Admin, Sonata Media Media Bundle and API Platform, the /api endpoint fails with Class "Sonata\ClassificationBundle\Model\CategoryInterface" does not exist.

Currently there are multiple references to a non-existing class, from an unused package.

List of references: https://github.com/sonata-project/SonataMediaBundle/blob/836b101eeabc8cd877d2e01f09d4b5b4eced0550/src/Model/MediaInterface.php#L128 https://github.com/sonata-project/SonataMediaBundle/blob/836b101eeabc8cd877d2e01f09d4b5b4eced0550/src/Listener/ORM/MediaEventSubscriber.php#L31 https://github.com/sonata-project/SonataMediaBundle/blob/836b101eeabc8cd877d2e01f09d4b5b4eced0550/src/Model/Media.php#L81

Direct dependencies required in composer.json:
api-platform/core v2.7.12 v3.1.12 Build a fully-featured hypermedia or GraphQL API in minutes!

From my point of view, if return type is ?object then also the annotations/comments should be object (eg. /* @var object|null */)

Minimal repository with the bug

Don't have yet but could provide if it's necessary.

Steps to reproduce

Install Sonata Admin, Sonata Media Bundle and API Platform, navigate to the Swagger interface and observe the error.

Expected results

Have a working API Platform Swagger interface.

Actual results

I see an exception on the Swagger interface with the following message: Class "Sonata\ClassificationBundle\Model\CategoryInterface" does not exist

show

``` ReflectionException: Class "Sonata\ClassificationBundle\Model\CategoryInterface" does not exist at vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:234 at ReflectionClass->__construct('Sonata\\ClassificationBundle\\Model\\CategoryInterface') (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:234) at ApiPlatform\JsonSchema\SchemaFactory->buildDefinitionName('Sonata\\ClassificationBundle\\Model\\CategoryInterface', 'jsonld', 'Sonata\\ClassificationBundle\\Model\\CategoryInterface', null, array()) (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:79) at ApiPlatform\JsonSchema\SchemaFactory->buildSchema('Sonata\\ClassificationBundle\\Model\\CategoryInterface', 'jsonld', 'output', null, object(Schema), array(), false) (vendor/api-platform/core/src/Hydra/JsonSchema/SchemaFactory.php:74) at ApiPlatform\Hydra\JsonSchema\SchemaFactory->buildSchema('Sonata\\ClassificationBundle\\Model\\CategoryInterface', 'jsonld', 'output', null, object(Schema), array(), false) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:155) at ApiPlatform\JsonSchema\TypeFactory->getClassType('Sonata\\ClassificationBundle\\Model\\CategoryInterface', 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:88) at ApiPlatform\JsonSchema\TypeFactory->makeBasicType(object(Type), 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:75) at ApiPlatform\JsonSchema\TypeFactory->getType(object(Type), 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:216) at ApiPlatform\JsonSchema\SchemaFactory->buildPropertySchema(object(Schema), 'Media.jsonld', 'category', object(ApiProperty), array(), 'jsonld') (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:140) at ApiPlatform\JsonSchema\SchemaFactory->buildSchema('App\\Application\\Sonata\\MediaBundle\\Entity\\Media', 'jsonld', 'output', null, object(Schema), array(), false) (vendor/api-platform/core/src/Hydra/JsonSchema/SchemaFactory.php:74) at ApiPlatform\Hydra\JsonSchema\SchemaFactory->buildSchema('App\\Application\\Sonata\\MediaBundle\\Entity\\Media', 'jsonld', 'output', null, object(Schema), array(), false) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:155) at ApiPlatform\JsonSchema\TypeFactory->getClassType('App\\Application\\Sonata\\MediaBundle\\Entity\\Media', 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:88) at ApiPlatform\JsonSchema\TypeFactory->makeBasicType(object(Type), 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/TypeFactory.php:75) at ApiPlatform\JsonSchema\TypeFactory->getType(object(Type), 'jsonld', null, array(), object(Schema)) (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:216) at ApiPlatform\JsonSchema\SchemaFactory->buildPropertySchema(object(Schema), 'Area.jsonld', 'image', object(ApiProperty), array(), 'jsonld') (vendor/api-platform/core/src/JsonSchema/SchemaFactory.php:140) at ApiPlatform\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Area', 'jsonld', 'output', object(Get), object(Schema), array(), false) (vendor/api-platform/core/src/Hydra/JsonSchema/SchemaFactory.php:74) at ApiPlatform\Hydra\JsonSchema\SchemaFactory->buildSchema('App\\Entity\\Area', 'jsonld', 'output', object(Get), object(Schema), null, false) (vendor/api-platform/core/src/OpenApi/Factory/OpenApiFactory.php:181) at ApiPlatform\OpenApi\Factory\OpenApiFactory->collectPaths(object(ApiResource), object(ResourceMetadataCollection), object(Paths), object(ArrayObject)) (vendor/api-platform/core/src/OpenApi/Factory/OpenApiFactory.php:103) at ApiPlatform\OpenApi\Factory\OpenApiFactory->__invoke(array('base_url' => '/')) (vendor/api-platform/core/src/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php:66) at ApiPlatform\Symfony\Bundle\SwaggerUi\SwaggerUiAction->__invoke(object(Request)) (vendor/api-platform/core/src/Core/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php:136) at ApiPlatform\Core\Bridge\Symfony\Bundle\Action\SwaggerUiAction->__invoke(object(Request)) (vendor/symfony/http-kernel/HttpKernel.php:163) at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1) (vendor/symfony/http-kernel/HttpKernel.php:75) at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true) (vendor/symfony/http-kernel/Kernel.php:202) at Symfony\Component\HttpKernel\Kernel->handle(object(Request)) (public/index.php:44) ```

More informations

As a workaround, this can be handled by overriding the category property like this:

    /**
     * @var object|null
     */
    protected ?object $category = null;

I can provide a PR if the the proposed solution is doable.

jordisala1991 commented 1 year ago

I woul say that we can try removing the annotation for the Model classes but not for all the project. Can you try the PR? It will probably fail on phpstan and psalm, let's see if it is fixable.

VincentLanglet commented 1 year ago

Hi @kerattila,

We does not support any object as category, but only CategoryInterface. So the phpdoc is true.

I would say that we can try removing the annotation for the Model classes but not for all the project. Can you try the PR? It will probably fail on phpstan and psalm, let's see if it is fixable.

We could try

@var object 
@phpstan-var CategoryInterface

But if it does not work, I would say the issue in on Swagger which shouldnt hard fail on such situation with optional feature/dependencies. Phpdoc is not "real" code, it should be allowed to write what we want on it ; that's why attributes was invented for "interpreted phpdoc".

VincentLanglet commented 1 year ago

Does updating the phpdoc to

@var object 
@phpstan-var CategoryInterface

solve your issue @kerattila ?

hzlatarski commented 11 months ago

Any chance the issue to be fixed with the next release?

VincentLanglet commented 11 months ago

Any chance the issue to be fixed with the next release?

Open source is easy. If you want something to be fixed, fix it ;)

I didn't even got a response from my previous question, I won't spend more time on this. This is more a API Platform issue which crash because of PHPDoc.

github-actions[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

UrbiJr commented 2 months ago

Hey @VincentLanglet , idk where did the OP go, or if he gave up on it (reasonable). But to answer you:

That said, I'm obviously encountering the error as well and I find it funny that 1 year later we still there.

VincentLanglet commented 2 months ago
  • open source is easy yes, and indeed the OP proposed a fix that you could have already implemented

That said, I'm obviously encountering the error as well and I find it funny that 1 year later we still there.

Feel free to work on a PR or to sponsor maintainers which are working on these repository on they free time. You find funny that 1 year later the error is still here ? It means no-one care to contribute one this. That's how open source is "easy".

  • it's definitely not an API Platform issue, since it's throwing the error because of a non-existing reference of the class CategoryInterface which should be "optional", but is actually required inside your Sonata\MediaBundle\Model\Media class

I still believe that API Platform has issue since it crash on PHP annotation, which is are only comment, and should be trusted like typing or PHP Atributes. That's the whole reason for PHP Attribute introductions. Comments are comments, not real code.

UrbiJr commented 2 months ago

No hard feelings towards maintainers that will always have my respect, I just found a little misleading your statement "Open source is easy. If you want something to be fixed, fix it". Not everyone is actually able to fix any problem. That's also the reason why not everyone can maintain a repo.

That said, I forgot to add the

@var object 
@phpstan-var CategoryInterface

annotation above getCategory and setCategory when extending BaseMedia. Now it actually works. So for everyone reading this, search for all the CategoryInterface references in Media and for each one add the said annotation when extending the class.

VincentLanglet commented 2 months ago

That said, I forgot to add the

@var object 
@phpstan-var CategoryInterface

annotation above getCategory and setCategory when extending BaseMedia. Now it actually works.

Can you try https://github.com/sonata-project/SonataMediaBundle/pull/2463 to see if this branch solve the issue then ? Or do I have to add @var object.

UrbiJr commented 2 months ago

@VincentLanglet just tried and it works like a charm. Thanks!!

VincentLanglet commented 2 months ago

@VincentLanglet just tried and it works like a charm. Thanks!!

You're welcome.

Not everyone is actually able to fix any problem. That's also the reason why not everyone can maintain a repo.

I disagree. All was explained in https://github.com/sonata-project/SonataMediaBundle/issues/2399#issuecomment-1647575596 ; just nobody care to take the time to open the PR (like often in open source).

Everybody can become a contributor, it's not reserved for some "better developers".