Skip to content

Commit

Permalink
Merge pull request #148 from nextcloud/fix/request-body-usage
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Aug 5, 2024
2 parents 91baaba + e87edc7 commit 89600bc
Show file tree
Hide file tree
Showing 8 changed files with 1,459 additions and 73 deletions.
98 changes: 58 additions & 40 deletions generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ foreach ($routes as $scope => $scopeRoutes) {
$pathParameters[] = $parameter;
}

$queryParameters = [];
$bodyParameters = [];
foreach ($route->controllerMethod->parameters as $parameter) {
$alreadyInPath = false;
Expand All @@ -664,7 +665,11 @@ foreach ($routes as $scope => $scopeRoutes) {
}
}
if (!$alreadyInPath) {
$bodyParameters[] = $parameter;
if (in_array(strtolower($route->verb), ['put', 'post', 'patch'])) {
$bodyParameters[] = $parameter;
} else {
$queryParameters[] = $parameter;
}
}
}

Expand Down Expand Up @@ -764,9 +769,9 @@ foreach ($routes as $scope => $scopeRoutes) {
if (count($security) > 0) {
$operation["security"] = $security;
}
if (count($bodyParameters) > 0 || count($pathParameters) > 0 || $route->isOCS) {

if (count($bodyParameters) > 0) {
$requiredBodyParameters = [];
$parameters = [];

foreach ($bodyParameters as $bodyParameter) {
$required = !$bodyParameter->type->nullable && !$bodyParameter->type->hasDefaultValue;
Expand All @@ -775,48 +780,61 @@ foreach ($routes as $scope => $scopeRoutes) {
}
}

if (count($bodyParameters) > 0) {
$required = count($requiredBodyParameters) > 0;

$schema = [
"type" => "object",
];
if ($required) {
$schema["required"] = $requiredBodyParameters;
}
$schema["properties"] = [];
foreach ($bodyParameters as $bodyParameter) {
$schema["properties"][$bodyParameter->name] = $bodyParameter->type->toArray();
}
$required = count($requiredBodyParameters) > 0;

$operation["requestBody"] = [
"required" => $required,
"content" => [
"application/json" => [
"schema" => $schema,
],
],
];
$schema = [
"type" => "object",
];
if ($required) {
$schema["required"] = $requiredBodyParameters;
}
$schema["properties"] = [];
foreach ($bodyParameters as $bodyParameter) {
$schema["properties"][$bodyParameter->name] = $bodyParameter->type->toArray();
}

$parameters = array_merge($parameters, $pathParameters);
if ($route->isOCS) {
$parameters[] = [
"name" => "OCS-APIRequest",
"in" => "header",
"description" => "Required to be true for the API request to pass",
"required" => true,
"schema" => [
"type" => "boolean",
"default" => true,
$operation["requestBody"] = [
"required" => $required,
"content" => [
"application/json" => [
"schema" => $schema,
],
];
}
],
];
}

if (count($parameters) > 0) {
$operation["parameters"] = $parameters;
$parameters = $pathParameters;
foreach ($queryParameters as $queryParameter) {
$parameter = [
"name" => $queryParameter->name . ($queryParameter->type->type === "array" ? "[]" : ""),
"in" => "query",
];
if ($queryParameter->docType !== null && $queryParameter->docType->description !== "") {
$parameter["description"] = Helpers::cleanDocComment($queryParameter->docType->description);
}
if (!$queryParameter->type->nullable && !$queryParameter->type->hasDefaultValue) {
$parameter["required"] = true;
}
$parameter["schema"] = $queryParameter->type->toArray(true);

$parameters[] = $parameter;
}
if ($route->isOCS) {
$parameters[] = [
"name" => "OCS-APIRequest",
"in" => "header",
"description" => "Required to be true for the API request to pass",
"required" => true,
"schema" => [
"type" => "boolean",
"default" => true,
],
];
}
if (count($parameters) > 0) {
$operation["parameters"] = $parameters;
}

$operation["responses"] = $mergedResponses;

$scopePaths[$scope] ??= [];
Expand Down Expand Up @@ -981,7 +999,7 @@ foreach ($scopePaths as $scope => $paths) {
}

if (count($scopedSchemas) === 0) {
$scopedSchemas = new \stdClass();
$scopedSchemas = new stdClass();
} else {
ksort($scopedSchemas);
}
Expand All @@ -992,7 +1010,7 @@ foreach ($scopePaths as $scope => $paths) {
$pathsCount = count($openapiScope['paths']);
if ($pathsCount === 0) {
// Make sure the paths array is always a dictionary
$openapiScope['paths'] = new \stdClass();
$openapiScope['paths'] = new stdClass();
}

$startExtension = strrpos($out, '.');
Expand Down
4 changes: 2 additions & 2 deletions src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public static function parse(string $context, array $definitions, ClassMethod $m
}

if (str_starts_with($type->name, 'OCS') && str_ends_with($type->name, 'Exception')) {
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(type: "array", maxItems: 0), null);
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(context: $context, type: "array", maxItems: 0), null);
} else {
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(type: "string"), null);
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(context: $context, type: "string"), null);
}
}
}
Expand Down
Loading

0 comments on commit 89600bc

Please sign in to comment.