NelmioApiDocBundle 4 how upload file in attributes

983 Views Asked by At

I have a problem with uploading files using nelmio version 4 with arguments, the documentation is quite poor and I can't find a solution.

You can't use in: 'formData' which worked in earlier versions, I also tried zwagger examples and it didn't work either. Is there anyone able to help

I have this example #[OA\RequestBody( content: new OA\MediaType( mediaType:"multipart/form-data", ) )] but not work, I have the impression that the MediaType is not displayed

1

There are 1 best solutions below

3
vodevel On

Something like this:

#[OA\RequestBody(
    content: [
        new OA\MediaType(
            mediaType: 'multipart/form-data',
            schema: new OA\Schema(properties: [
                new OA\Property(
                    property: 'avatar',
                    type: 'file',
                ),
            ])
        ),
    ]
)]

or from model:

use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes\MediaType;
use OpenApi\Attributes\RequestBody;
use OpenApi\Attributes\Schema;


#[RequestBody(
    required: true,
    content: [
        new MediaType(
            mediaType: 'multipart/form-data',
            schema: new Schema(ref: new Model(type: AvatarRequest::class))
        ),
    ]
)]

###

use OpenApi\Attributes\Property;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints\Image;

class AvatarRequest
{
    #[Property(type: 'file')]
    #[Image(maxSize: '2M', mimeTypes: [
        'image/jpeg',
        'image/png',
        'image/webp',
    ])]
    public UploadedFile $avatar;
}