NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

Using @Model inside @OpenApi\Attributes\RequestBody::$ref is not allowed

Open enumag opened this issue 3 years ago • 5 comments

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 avatar May 02 '22 14:05 enumag

@enumag You can use:

#[OA\Put(requestBody: new OA\RequestBody(content: new OA\JsonContent(ref: new Model(type: MyControllerRequest::class))))]

javer avatar May 03 '22 17:05 javer

Okay that works. The exception message is really confusing. Besides the annotation was already unnecessarily long and the attributes make it even longer.

enumag avatar May 06 '22 06:05 enumag

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 avatar May 08 '22 13:05 GuilhemN

@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?

bobvandevijver avatar May 13 '22 13:05 bobvandevijver

@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?

You are right. Also psalm angry about Model in attachables.

maxlen avatar Jun 30 '22 09:06 maxlen