[Bug]: PUT request is showing POST body parameters from Request rules
What happened?
It looks like there is a bug in the way body params are rendered in the PUT doc page.
In POST and PUT pages, the list of body params are retrieved from the request validation rules.
When we share the same FormRequest class in POST and PUT requests, PUT page is showing the body params of the POST request.
How to reproduce the bug
Add this FormRequest class in both store(CustomerRequest $request) and update(CustomerRequest $request).
Expected result:
POST page shows 2 required body params: name and other
PUT page shows 1 required body param: name
Actual result:
POST & PUT pages are showing 2 required body params: name and other
class CustomerRequest extends FormRequest
{
public function rules(): array
{
$rules = [
'name' => ['required', 'string'],
];
if ($this->isMethod('POST')) {
$rules['other'] = ['required', 'string'];
}
return $rules;
}
}
class CustomerController extends Controller
{
public function store(CustomerRequest $request): CustomerResource
{
return CustomerResource::make(Customer::create($request->validated()));
}
public function update(CustomerRequest $request, Customer $customer): CustomerResource
{
$customer->update($request->validated());
return CustomerResource::make($customer);
}
}
Package Version
0.12.19
PHP Version
8.2
Laravel Version
12
Which operating systems does with happen with?
No response
Notes
I have put some var_dump in this method and I can see that POST and PUT params are properly extracted.
But for some reason the PUT param are not displayed properly on the PUT page.
I think I found the reason of the issue.
By sharing the same FormRequest in both endpoints, the open API json generated contains a ref to the same schema: #/components/schemas/CustomerRequest
Is there any chance the schema can be renamed?
Eg: #/components/schemas/PostCustomerRequest and #/components/schemas/PutCustomerRequest if they are different?
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CustomerRequest"
}
}
}
},
@Seb33300 This is really a great catch. I also like your suggested solution. Not sure when, but I'll make this happen.