-
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
Returning newly created models (either as is or in resource) should result in 201
response status
#132
Comments
the
|
hello! I also have trouble returning the correct Response data and Status code together.
/**
* @response ZonesRecordResource
*/
public function store(Request $request, string $domain)
{
// ...
/**
* @status 201
* @body ZonesRecordResource
*/
return (new ZonesRecordResource($record))->response()->setStatusCode(201);
} Did I miss something on the docs? |
@SRWieZ looks like a bug to me! Will fix |
@SRWieZ fixed the issue of resource being not properly wrapped when specified in Also improved type inference a bit and now using Please upgrade to https://github.com/dedoc/scramble/releases/tag/v0.11.1 |
@shamil8124 you should use /**
* @operationId bulkCreateTests
*/
public function bulkStore(TestRequest $request)
{
/**
* @status 201
* @body AnonymousResourceCollection<LengthAwarePaginator<TestResource>>
*/
return (TestResource::collection($this->createTests($request)))->toResponse()->setStatusCode(201);
} But in /**
* @operationId bulkCreateTests
*/
public function bulkStore(TestRequest $request)
{
return (TestResource::collection($this->createTests($request)))->toResponse()->setStatusCode(201);
} |
Thanks @romalytvynenko ! Tried but one last thing:
|
Can you show the controller's code and the result? |
Hi, is there a fix or workaround for this? The documentation still shows a 200 instead of a 201 for me, despite trying the suggestions above. Here's my controller method setup: /**
* Create an item
*
* @response ItemResource
*/
public function store(ItemStoreRequest $request): JsonResponse
{
return response()->json(new ItemResource(
$this->service->create($request->validated())
), 201);
} I've tried annotating |
@maxechendu which Scramble version are you using? Also please remove the |
Never mind. Removing the |
Yes, that was my mistake. Thank you 😃 |
Conv from Discord with context:
I have an issue concerning my responses returning a 201 status code, which is documented as returning a 200 code.
This is because I'm returning a JsonResource instance; it checks if $model->wasRecentlyCreated === true to define the status code. See: https://github.com/laravel/framework/blob/9.x/src/Illuminate/Http/Resources/Json/ResourceResponse.php#L117
How could we do with Scramble to figure this out? It seems complicated, but maybe you've already thought about it.
@antoine
Hey! I just tried Scramble. It works well! I have an issue concerning my responses returning a 201 status code, which is documented as returning a 200 code. This is because I'm returning a JsonResource instance; it checks if $model->wasRecentlyCreated === true to define the status code. See: https://github.com/laravel/framework/blob/9.x/src/Illuminate/Http/Resources/Json/ResourceResponse.php#L117 How could we do with Scramble to figure this out? It seems complicated, but maybe you've already thought about it.
romalyt — 01/15/2023 6:45 PM
Hey hey!
Yeah, I thought about this. My idea was to check if a model you pass to JsonResource is created by using new, ::create, or ::make methods and if so, document a response with 201 status.
I'm wondering, how do you create models you pass to JsonResource?
P.S.: As a quick fix, you always can help Scramble to document what is needed 😻 (https://scramble.dedoc.co/usage/response#response-description)
/** @status 201 */
return new TodoItemResource($todoItem);
antoine — 01/15/2023 6:50 PM
Well, it depends, for some clients, I just do Model::create() in the controller. But for other clients having more complex projets (or bigger teams), I call a service class (or repository) in charge of creating the model
The text was updated successfully, but these errors were encountered: