Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,14 @@
}
$operation['security'] = $security;

if ($bodyParameters !== []) {
$uploadedFiles = [];
if (strtolower($route->verb) === 'post') {
$uploadedFiles = $route->controllerMethod->uploadedFiles;
} elseif ($route->controllerMethod->uploadedFiles !== []) {
Logger::warning($route->name, 'Uploaded files are only available in POST requests');
}

if ($bodyParameters !== [] || $uploadedFiles !== []) {
$requiredBodyParameters = [];

foreach ($bodyParameters as $bodyParameter) {
Expand All @@ -847,13 +854,28 @@
$schema['properties'][$bodyParameter->name] = $bodyParameter->type->toArray($schemas);
}

$content = [];
if ($bodyParameters !== []) {
$content['application/json'] = [
'schema' => $schema,
];
}
if ($uploadedFiles !== []) {
$multipartSchema = $schema;
foreach ($uploadedFiles as $uploadedFile) {
$multipartSchema['properties'][$uploadedFile] = [
'type' => 'string',
'format' => 'binary',
];
}
$content['multipart/form-data'] = [
'schema' => $multipartSchema,
];
}

$operation['requestBody'] = [
'required' => $required,
'content' => [
'application/json' => [
'schema' => $schema,
],
],
'content' => $content,
];
}

Expand Down
16 changes: 15 additions & 1 deletion src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace OpenAPIExtractor;

use PhpParser\Node\Arg;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
Expand Down Expand Up @@ -289,6 +291,7 @@ class ControllerMethod {
* @param OpenApiType[] $returns
* @param array<int, string> $responseDescription
* @param string[] $description
* @param list<string> $uploadedFiles
*/
public function __construct(
public array $parameters,
Expand All @@ -298,6 +301,7 @@ public function __construct(
public array $description,
public ?string $summary,
public bool $isDeprecated,
public array $uploadedFiles,
) {
}

Expand Down Expand Up @@ -610,6 +614,7 @@ public static function parse(string $context,
}

$codeRequestHeaders = [];
$uploadedFiles = [];
foreach ($nodeFinder->findInstanceOf($method->getStmts(), MethodCall::class) as $methodCall) {
if ($methodCall->var instanceof PropertyFetch
&& $methodCall->var->var instanceof Variable
Expand Down Expand Up @@ -654,6 +659,15 @@ public static function parse(string $context,

$parameters[] = new ControllerMethodParameter($context, $definitions, $name, $type);
}
if ($methodCall->name->name === 'getUploadedFile') {
$arg = $methodCall->args[0] ?? null;
if (!$arg instanceof Arg || !$arg->value instanceof String_) {
Logger::warning($context, 'getUploadedFile() is not called with a string literal, so the uploaded file can not be documented.');
continue;
}

$uploadedFiles[] = $arg->value->value;
}
}
}

Expand Down Expand Up @@ -713,7 +727,7 @@ public static function parse(string $context,
Logger::error($context, 'Unused request header descriptions: ' . implode(', ', $unusedRequestHeaders));
}

return new ControllerMethod($parameters, $attributeRequestHeaders, $responses, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated);
return new ControllerMethod($parameters, $attributeRequestHeaders, $responses, $responseDescriptions, $methodDescription, $methodSummary, $isDeprecated, array_values(array_unique($uploadedFiles)));
}

private static function cleanHeaderName(string $header): string {
Expand Down
2 changes: 2 additions & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
['name' => 'Settings#samePathPost', 'url' => '/api/{apiVersion}/same-path', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#requestHeader', 'url' => '/api/{apiVersion}/request-header', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#requestParams', 'url' => '/api/{apiVersion}/request-params', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#uploadedFiles', 'url' => '/api/{apiVersion}/uploaded-files', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#uploadedFileOnly', 'url' => '/api/{apiVersion}/uploaded-file-only', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#publicPageAnnotation', 'url' => '/api/{apiVersion}/public-page/annotation', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#publicPageAttribute', 'url' => '/api/{apiVersion}/public-page/attribute', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#mergedResponses', 'url' => '/api/{apiVersion}/merged-responses', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,35 @@ public function requestParams(): DataResponse {
return new DataResponse();
}

/**
* A method with uploaded files and a parameter
*
* @param string $name Name of the upload
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: Files uploaded
*/
public function uploadedFiles(string $name): DataResponse {
$this->request->getUploadedFile('file');
$this->request->getUploadedFile('attachment');
$this->request->getUploadedFile('file');

return new DataResponse();
}

/**
* A method with only an uploaded file
*
* @return DataResponse<Http::STATUS_OK, list<empty>, array{}>
*
* 200: File uploaded
*/
public function uploadedFileOnly(): DataResponse {
$this->request->getUploadedFile('file');

return new DataResponse();
}

/**
* A public page with annotation.
*
Expand Down
Loading
Loading