-
-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(symfony): describe MapUploadedFile property
- Loading branch information
Showing
7 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/RouteDescriber/RouteArgumentDescriber/SymfonyMapUploadedFileDescriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the NelmioApiDocBundle package. | ||
* | ||
* (c) Nelmio | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\RouteDescriber\RouteArgumentDescriber; | ||
|
||
use Nelmio\ApiDocBundle\OpenApiPhp\Util; | ||
use OpenApi\Annotations as OA; | ||
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
final class SymfonyMapUploadedFileDescriber implements RouteArgumentDescriberInterface | ||
{ | ||
public const CONTEXT_ARGUMENT_METADATA = 'nelmio_api_doc_bundle.argument_metadata.'.self::class; | ||
public const CONTEXT_MODEL_REF = 'nelmio_api_doc_bundle.model_ref.'.self::class; | ||
|
||
public function describe(ArgumentMetadata $argumentMetadata, OA\Operation $operation): void | ||
{ | ||
if (!$attribute = $argumentMetadata->getAttributes(MapUploadedFile::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? null) { | ||
return; | ||
} | ||
|
||
$name = $attribute->name ?? $argumentMetadata->getName(); | ||
$body = Util::getChild($operation, OA\RequestBody::class); | ||
|
||
$mediaType = Util::getCollectionItem($body, OA\MediaType::class, [ | ||
'mediaType' => 'multipart/form-data' | ||
]); | ||
|
||
/** @var OA\Schema $schema */ | ||
$schema = Util::getChild($mediaType, OA\Schema::class, [ | ||
'type' => 'object' | ||
]); | ||
|
||
$property = Util::getCollectionItem($schema, OA\Property::class, ['property' => $name]); | ||
Util::modifyAnnotationValue($property, 'type', 'string'); | ||
Util::modifyAnnotationValue($property, 'format', 'binary'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the NelmioApiDocBundle package. | ||
* | ||
* (c) Nelmio | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nelmio\ApiDocBundle\Tests\Functional\Controller; | ||
|
||
use OpenApi\Attributes as OA; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class MapUploadedFileController | ||
{ | ||
#[Route('/article_map_uploaded_file', methods: ['POST'])] | ||
#[OA\Response(response: '200', description: '')] | ||
public function createUploadFromMapUploadedFilePayload( | ||
#[MapUploadedFile] | ||
UploadedFile $upload, | ||
) { | ||
} | ||
|
||
#[Route('/article_map_uploaded_file_nullable', methods: ['POST'])] | ||
#[OA\Response(response: '200', description: '')] | ||
public function createUploadFromMapUploadedFilePayloadNullable( | ||
#[MapUploadedFile] | ||
?UploadedFile $upload, | ||
) { | ||
} | ||
|
||
#[Route('/article_map_uploaded_file_multiple', methods: ['POST'])] | ||
#[OA\Response(response: '200', description: '')] | ||
public function createUploadFromMapUploadedFilePayloadMultiple( | ||
#[MapUploadedFile] | ||
UploadedFile $firstUpload, | ||
#[MapUploadedFile] | ||
UploadedFile $secondUpload, | ||
) { | ||
} | ||
|
||
#[Route('/article_map_uploaded_file_add_to_existing', methods: ['POST'])] | ||
#[OA\RequestBody(content: [ | ||
new OA\MediaType('multipart/form-data', new OA\Schema( | ||
properties: [new OA\Property(property: 'existing', type: 'string', format: 'binary')], | ||
type: 'object', | ||
)), | ||
])] | ||
#[OA\Response(response: '200', description: '')] | ||
public function createUploadFromMapUploadedFileAddToExisting( | ||
#[MapUploadedFile] | ||
?UploadedFile $upload, | ||
) { | ||
} | ||
|
||
#[Route('/article_map_uploaded_file_overwrite', methods: ['POST'])] | ||
#[OA\RequestBody( | ||
description: 'Body if file upload request', | ||
content: [ | ||
new OA\MediaType('multipart/form-data', new OA\Schema( | ||
properties: [new OA\Property( | ||
property: 'upload', | ||
description: 'A file', | ||
)], | ||
type: 'object', | ||
)), | ||
], | ||
)] | ||
#[OA\Response(response: '200', description: '')] | ||
public function createUploadFromMapUploadedFileOverwrite( | ||
#[MapUploadedFile] | ||
?UploadedFile $upload, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.