NelmioApiDocBundle
NelmioApiDocBundle copied to clipboard
Using @Model inside @OpenApi\Attributes\RequestBody::$ref is not allowed
I tried using attributes instead of annotations so I changed:
@\OpenApi\Annotations\Put(requestBody = @\OpenApi\Annotations\RequestBody(@Model(type = MyControllerRequest::class)))
to
#[\OpenApi\Attributes\Put(requestBody: new \OpenApi\Attributes\RequestBody(new Model(type: MyControllerRequest::class)))]
and got this error:
Uncaught PHP Exception InvalidArgumentException: "Using @Model inside @OpenApi\Attributes\RequestBody::$ref is not allowed. You should use ::$ref with @Property, @Parameter, @Schema, @Items but within @Response or @RequestBody you should put @Model directly at the root of the annotation : `@Response(..., @Model(...))`." at /var/www/vendor/nelmio/api-doc-bundle/OpenApiPhp/ModelRegister.php line 57
I have no idea what I'm supposed to do here. I don't see any way to pass Model directly into Put.
@enumag You can use:
#[OA\Put(requestBody: new OA\RequestBody(content: new OA\JsonContent(ref: new Model(type: MyControllerRequest::class))))]
Okay that works. The exception message is really confusing. Besides the annotation was already unnecessarily long and the attributes make it even longer.
The issue is that in the OA\RequestBody attribute, the $ref parameter comes first in the constructor so this is where the @Model is injected.
This error message was indeed adapted for annotations but the proposed solution should be reworded for attributes...
You could inject it instead as follow:
#[OA\Put(requestBody: new OA\RequestBody(attachables: [ new Model(type: MyControllerRequest::class) ] ))]
@GuilhemN I'm currently using the content parameters instead of the attachables, because each attachable must be an OpenApi\Attributes\Attachable, which the Model is not. In the implementation content and attachables are merged, which is why it works for me.
In short, should the Model attribute extend the Attachable class?
@GuilhemN I'm currently using the
contentparameters instead of theattachables, because each attachable must be anOpenApi\Attributes\Attachable, which theModelis not. In the implementationcontentandattachablesare merged, which is why it works for me.In short, should the Model attribute extend the Attachable class?
You are right. Also psalm angry about Model in attachables.