Skip to content
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

Use PHP8 return type instead of annotations #6776

Open
dennismetz opened this issue Nov 5, 2024 · 1 comment
Open

Use PHP8 return type instead of annotations #6776

dennismetz opened this issue Nov 5, 2024 · 1 comment

Comments

@dennismetz
Copy link

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;
	}
@soyuka
Copy link
Member

soyuka commented Nov 6, 2024

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

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

No branches or pull requests

2 participants