core icon indicating copy to clipboard operation
core copied to clipboard

Use PHP8 return type instead of annotations

Open dennismetz opened this issue 1 year ago • 8 comments

API Platform version(s) affected: I currently use api-platform/symfony in version 4.0.6

Description
The PHP8 return data type of the method is ignored. Only the old notation for annotations is taken into account. You then get an error message when you call the whole thing via the API platform: Unexpected non-iterable value for to-many relation.

How to reproduce
I call a collection in my entity and only want to return a single item or null from it.

	#[Groups(['Vehicles', 'readVehicle'])]
	public function getDriver(): null|VehiclesDriver // null|VehiclesDriver is completly ignored
	{
		if (0 < count($this->getVehiclesDriver()))
			foreach ($this->getVehiclesDriver() as $driver)
				return $driver;
		return null;
	}

Possible Solution
Currently you can fix the error by returning the return type in the annotation.

	/**
	 * @return VehiclesDriver|null
	 */
	#[Groups(['Vehicles', 'readVehicle'])]
	public function getDriver(): null|VehiclesDriver
	{
		if (0 < count($this->getVehiclesDriver()))
			foreach ($this->getVehiclesDriver() as $driver)
				return $driver;
		return null;
	}

dennismetz avatar Nov 05 '24 10:11 dennismetz

I expect this is an issue from symfony? But it's weird, what property info extractor are registered on your project?

soyuka avatar Nov 06 '24 15:11 soyuka

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.

stale[bot] avatar Jan 05 '25 16:01 stale[bot]

Sorry for the late reply. We have only implemented the standard Symfony/Doctrine/API Platform features.

We had the problem again today, only with a lot more “add” and “get” methods.

dennismetz avatar Feb 04 '25 15:02 dennismetz

Could you help tracking this by checking values at https://github.com/api-platform/core/blob/main/src/Metadata/Property/Factory/PropertyInfoPropertyMetadataFactory.php#L48-L59 ? (remove your cache if you don't hit these lines)

I need to know if this needs fixing in symfony or in our codebase, thanks!

soyuka avatar Feb 05 '25 05:02 soyuka

I will definitely get to the foreach:

Image Image

I hope this helps.

dennismetz avatar Feb 05 '25 07:02 dennismetz

I just tested again whether I can get into the foreach, but I can't get in at the moment.

Image Image

EDIT: I have done the tests with and without the annotations, but both lead to the same result.

With the annotations, however, the call works in the frontend without the error message “Unexpected non-iterable value for to-many relation”.

dennismetz avatar Feb 05 '25 07:02 dennismetz

since https://github.com/symfony/symfony/pull/57617 got merged, can you try https://github.com/api-platform/core/pull/6947 ?

soyuka avatar Feb 06 '25 09:02 soyuka

I have just tested it, but I still get the error when I remove the annotations. With the annotations it works.

dennismetz avatar Feb 06 '25 11:02 dennismetz