-
Notifications
You must be signed in to change notification settings - Fork 125
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
Response Body Returning String when using ApiResponser Trait #540
Comments
@arislanhaikal This is Scramble's issue. I will fix! |
public function index(Requests $request): JsonResource
{
// more code including `$user`
return $this->returnData($user);
}
public function returnData($user): JsonResource
{
// some more code
return new UsersResource($user);
} Above code is also returning the similar format (OP) in scramble. Just drop here just in case |
I am also facing the same issue for my response:
|
@aashirhaq, I have no idea why. Please share some code examples. Otherwise I won't be able to help |
@valpuia604 that code that you've excluded matters. First of all make sure you use the latest version. If it doesn't work, include enough code so I can reproduce. |
@romalytvynenko I am using Here is my full code: Controller file:
And, the respond method is below:
Here is PaymentMethod resource file code:
Response is Generating from above code is here:
|
Hi I've updated to /**
* @unauthenticated
*/
public function index(LoginRequest $request): JsonResponse|JsonResource
{
$user = User::where('username', $request->validated('username'))
->first();
if (! $user || ! Hash::check($request->validated('password'), $user->password)) {
return $this->returnError(__('auth.failed'));
}
return $this->loadUser($user);
}
/**
* Why not include in `index` instead of calling below method?
*
* Well I have another login method which check a little bit different
* So try to follow DRY
*/
public function loadUser($user): JsonResponse|JsonResource
{
if (! $user->is_active) {
return $this->returnError(__('messages.acc_disabled'));
}
$user['token'] = $user->createToken('auth_token')->plainTextToken;
return new UsersResource($user);
}
public function returnError($messageDescription): JsonResponse
{
return response()->json([
'message' => [
'title' => __('messages.error'),
'description' => $messageDescription,
],
'status' => false,
], 422);
}
// Login Request
public function rules(): array
{
return [
'username' => 'required',
'password' => 'required',
];
} |
@aashirhaq your example is not supported yet, as it is too complex: type is being built conditionally. For now, Scramble doesn't support that. |
Digging deeper and it's not about another method calls, it's only return type |
Still not working inside trait method calls |
@aashirhaq Please create reproduction repository on GitHub. Otherwise, I won't be able to help. |
Hi @romalytvynenko, i make simple example for this issue on github laravel-scramble-example . Can you check please! |
@aashirhaq Thanks. I'm on it, but it may take some time |
Hello, Roman, I encountered a similar problem with rendering the response body as a string. However, there seems to be something mystical going on. Code to Reproduce: final class ApiResponse
{
public static function json(
mixed $data = [],
string $message = null,
$status = Response::HTTP_OK
): JsonResponse {
$success = match (true) {
$status >= 200 && $status < 300 => true,
default => false,
};
return new JsonResponse([
'success' => $success,
'message' => $message,
'data' => $data,
], $status);
}
} Observed Behavior |
Hello All
I use scamble version 0.11.13 with Laravel 11.
When I build an API, I always create an ApiResponser Trait to keep my API response consistent.
Such as:
I create ApiResponser.php trait
and then I used in Controller.php
In another controller, example AuthController.php
The expected Response Body in scramble is
But response body in scramble is string
Is there something wrong with me? Or how should I return API responses with a consistent format?
Thank you
The text was updated successfully, but these errors were encountered: