api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.45k stars 874 forks source link

There is no PropertyInfo extractor supporting the class #3445

Closed yapro closed 4 years ago

yapro commented 4 years ago

API Platform version(s) affected: v2.5.4

Description
I try to implementing a custom operation (without specifying an ID). My situation:

Request: {"foo":"1"}
Response: {"bar":"2"}

I have Post request.

I try to solve my problem as it is written in the https://api-platform.com/docs/core/dto/#specifying-an-input-or-an-output-data-representation

But, I have problem:

image

How to reproduce
https://github.com/yapro/api-platform-test/commit/bc70662c4f06b7769b781114d1d09031e47bb18f

soyuka commented 4 years ago

AnalyticalReportResource has no properties?

yapro commented 4 years ago

@soyuka yes, like you see, all properties in AnalyticalReportInput and AnalyticalReportOutput objects.

soyuka commented 4 years ago

Nope it's not working like this.

yapro commented 4 years ago

@soyuka so the documentation is outdated?

soyuka commented 4 years ago

Having an input and an output never creates properties on a class.

yapro commented 4 years ago

@soyuka like you see, I don't wont to create the AnalyticalReportResource, but api-platform doesn't work without a third class (resource class), am I right?

soyuka commented 4 years ago

Indeed, you need a resource class. You can also use symfony to declare a custom route without using our concepts.

teohhanhui commented 4 years ago

Actually, to me this is a bug. Currently the PropertyInfoPropertyNameCollectionFactory throws an exception when it can't determine any properties. However, that should be considered perfectly normal, if a resource class does not have properties, or does not have properties which can be found by the PropertyInfo component, as other decorator factories can still fill that in.

I've come across this while working on other issues, and I'm not sure what's the rationale behind this.

/cc @dunglas if you remember why...

OskarStark commented 4 years ago

I am using a resource class and using some value objects which only contain private properties and are not resources.

Uncaught PHP Exception ApiPlatform\Core\Exception\RuntimeException: "There is no PropertyInfo extractor supporting the class "App\Domain\Value\Tagdefinition\TagdefinitionUuid"." at /app/vendor/api-platform/core/src/Bridge/Symfony/PropertyInfo/Metadata/Property/PropertyInfoPropertyNameCollectionFactory.php line 46

I can confirm that adding a dummy public $foo; property to the object fixes the error.

As PropertyNameCollection can handle an empty array, why not pass this instead of raising an exception? https://github.com/api-platform/core/blob/f8ccee08710d442f0333530e96b835620947aa82/src/Metadata/Property/PropertyNameCollection.php#L31-L34

in

https://github.com/api-platform/core/blob/f8ccee08710d442f0333530e96b835620947aa82/src/Bridge/Symfony/PropertyInfo/Metadata/Property/PropertyInfoPropertyNameCollectionFactory.php#L42-L50

I am not able to say sth. about the side effects or why this code behaves like this, but I am open to provide a PR if you agree @dunglas @teohhanhui

Cheers

EDIT It was introduces 4 years ago by @dunglas in Fix some Scrutinizer issues

cc @localheinz

dunglas commented 4 years ago

TBH I don't remember the rationale (if any...). If tests pass, it should be safe to change this behavior!

OskarStark commented 4 years ago

Thanks for your feedback!