Since there isn't an (easy) way to do reflection of the response method, we need to use attribute or phpDoc.
The attribute this time is #[OpenApi\Response] and as second parameter accepts the JSON example (in case of phpDoc) or an array example (in the case of attribute)
Copy /**
* @OpenApi\Response(statusCode=200, response=App\Models\Example)
* @OpenApi\Response(statusCode=404, response={
* "message": "not found",
* "nrs": [{"issue": "#123"}],
* "metadata": {"node": {"server": "1"}}
* })
*/
public function index(Request $request): JsonResponse
{
try {
// ...
} catch (CustomException $e) {
return response()->json([
'message' => $e->getMessage(),
'issues' => $e->getIssues(),
'metadata' => [
'node' => [
'server' => $request->host(),
]
]
]);
}
}
// ... or
#[OpenApi\Response(statusCode: 404, response: [
'message' => 'not found',
'nrs' => [['issue' => '#123']],
'metadata' => ['node' => ['server' => '1']],
])]
public function index(Request $request): JsonResponse
{
try {
// ...
} catch (CustomException $e) {
return response()->json([
'message => $e->getMessage(),
'issues' => $e->getIssues(),
'metadata' => [
'node' => [
'server-1' => $request->host(),
]
]
]);
}
}