-
Notifications
You must be signed in to change notification settings - Fork 126
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
Array typing doesn't allow multiple array item types #601
Comments
This would be the expected implementation! |
@dnkmdg I started implementing this, but noticed Laravel Data itself does not support this. Under the hood, Laravel Data just keeps the first type of the provided union type. Here is my test setup: class WithUnionInArrayData extends \Spatie\LaravelData\Data
{
/**
* @param array<FooData|BarData> $something
*/
public function __construct(
public array $something,
) {}
}
class FooData extends \Spatie\LaravelData\Data
{
public function __construct(
public string $foo,
) {}
}
class BarData extends \Spatie\LaravelData\Data
{
public function __construct(
public string $bar,
) {}
} And now when I'm trying to create the object just with the "FooData": $data = WithUnionInArrayData::from([
'something' => [
['foo' => 'a'],
],
]);
dump($data); WithUnionInArrayData�{#1621
#_additional: []
#_dataContext: null
+something: array:1 [
0 => FooData�{#1638
#_additional: []
#_dataContext: null
+foo: "a"
}
]
} But if I try to create the $data = WithUnionInArrayData::from([
'something' => [
['foo' => 'a'],
['bar' => 'b'],
],
]); I get the error that happens because LaravelData is trying to create
And this is expected, due to internally Laravel Data got only So am I missing something and you have been able to make it work, or indeed this is not possible on Laravel Data level? |
Fixed in 0.11.24 (Laravel Data however indeed works as described) |
Re-opened due to missing property promotion inference |
I had a similar issue with a return type like this:
|
@cosmastech can you please share more context? Is it a comment in your controller's method? |
Were having the same issue described, but even without the array aspect... essentially anything with a multiple type response adds the first response, and ignores the rest. This... /**
* Verify a user using their email address.
* @unauthenticated
*
* @param Request $request
* @param string $verificationId
*
* @return UserVerificationResource|JsonResponse
*/
public function email(Request $request, string $verificationId): UserVerificationResource|JsonResponse
{
$userVerification = UserVerification::with(['user'])
->where('type', 'email')
->where('id', $verificationId)
->first();
if (
!$userVerification ||
$userVerification->expires->isPast() ||
$userVerification->attempts >= $this->authSettings->max_verify_attempts
) {
return response()->json(
[
'meta' => [
'status' => '410',
'message' => 'Invalid User Verification code',
],
],
410
);
}
return new UserVerificationResource($userVerification);
} Sadly results in only the UserVerificationResource showing in the docs or export, however if you swap the declarations for the return or typecast, then you get the JsonResponse listed, and no UserVerificationResource. What is strange, is that if I remove the DocBlock AND the return Typehint, then the output is exactly as expected. |
When annotating a return type as
array<TypeOne|TypeTwo>
only the first type is selected, and the rest ignored.This also causes the following types to be ignored from the Schema generation.
My assumption is that the array type currently isn't set to allow "anyOf" type of definitions.
Example
Definition:
Response example
Schemas
The text was updated successfully, but these errors were encountered: