Skip to content

Commit 89600bc

Browse files
Merge pull request #148 from nextcloud/fix/request-body-usage
2 parents 91baaba + e87edc7 commit 89600bc

File tree

8 files changed

+1459
-73
lines changed

8 files changed

+1459
-73
lines changed

generate-spec

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ foreach ($routes as $scope => $scopeRoutes) {
654654
$pathParameters[] = $parameter;
655655
}
656656

657+
$queryParameters = [];
657658
$bodyParameters = [];
658659
foreach ($route->controllerMethod->parameters as $parameter) {
659660
$alreadyInPath = false;
@@ -664,7 +665,11 @@ foreach ($routes as $scope => $scopeRoutes) {
664665
}
665666
}
666667
if (!$alreadyInPath) {
667-
$bodyParameters[] = $parameter;
668+
if (in_array(strtolower($route->verb), ['put', 'post', 'patch'])) {
669+
$bodyParameters[] = $parameter;
670+
} else {
671+
$queryParameters[] = $parameter;
672+
}
668673
}
669674
}
670675

@@ -764,9 +769,9 @@ foreach ($routes as $scope => $scopeRoutes) {
764769
if (count($security) > 0) {
765770
$operation["security"] = $security;
766771
}
767-
if (count($bodyParameters) > 0 || count($pathParameters) > 0 || $route->isOCS) {
772+
773+
if (count($bodyParameters) > 0) {
768774
$requiredBodyParameters = [];
769-
$parameters = [];
770775

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

778-
if (count($bodyParameters) > 0) {
779-
$required = count($requiredBodyParameters) > 0;
780-
781-
$schema = [
782-
"type" => "object",
783-
];
784-
if ($required) {
785-
$schema["required"] = $requiredBodyParameters;
786-
}
787-
$schema["properties"] = [];
788-
foreach ($bodyParameters as $bodyParameter) {
789-
$schema["properties"][$bodyParameter->name] = $bodyParameter->type->toArray();
790-
}
783+
$required = count($requiredBodyParameters) > 0;
791784

792-
$operation["requestBody"] = [
793-
"required" => $required,
794-
"content" => [
795-
"application/json" => [
796-
"schema" => $schema,
797-
],
798-
],
799-
];
785+
$schema = [
786+
"type" => "object",
787+
];
788+
if ($required) {
789+
$schema["required"] = $requiredBodyParameters;
790+
}
791+
$schema["properties"] = [];
792+
foreach ($bodyParameters as $bodyParameter) {
793+
$schema["properties"][$bodyParameter->name] = $bodyParameter->type->toArray();
800794
}
801795

802-
$parameters = array_merge($parameters, $pathParameters);
803-
if ($route->isOCS) {
804-
$parameters[] = [
805-
"name" => "OCS-APIRequest",
806-
"in" => "header",
807-
"description" => "Required to be true for the API request to pass",
808-
"required" => true,
809-
"schema" => [
810-
"type" => "boolean",
811-
"default" => true,
796+
$operation["requestBody"] = [
797+
"required" => $required,
798+
"content" => [
799+
"application/json" => [
800+
"schema" => $schema,
812801
],
813-
];
814-
}
802+
],
803+
];
804+
}
815805

816-
if (count($parameters) > 0) {
817-
$operation["parameters"] = $parameters;
806+
$parameters = $pathParameters;
807+
foreach ($queryParameters as $queryParameter) {
808+
$parameter = [
809+
"name" => $queryParameter->name . ($queryParameter->type->type === "array" ? "[]" : ""),
810+
"in" => "query",
811+
];
812+
if ($queryParameter->docType !== null && $queryParameter->docType->description !== "") {
813+
$parameter["description"] = Helpers::cleanDocComment($queryParameter->docType->description);
814+
}
815+
if (!$queryParameter->type->nullable && !$queryParameter->type->hasDefaultValue) {
816+
$parameter["required"] = true;
818817
}
818+
$parameter["schema"] = $queryParameter->type->toArray(true);
819+
820+
$parameters[] = $parameter;
821+
}
822+
if ($route->isOCS) {
823+
$parameters[] = [
824+
"name" => "OCS-APIRequest",
825+
"in" => "header",
826+
"description" => "Required to be true for the API request to pass",
827+
"required" => true,
828+
"schema" => [
829+
"type" => "boolean",
830+
"default" => true,
831+
],
832+
];
819833
}
834+
if (count($parameters) > 0) {
835+
$operation["parameters"] = $parameters;
836+
}
837+
820838
$operation["responses"] = $mergedResponses;
821839

822840
$scopePaths[$scope] ??= [];
@@ -981,7 +999,7 @@ foreach ($scopePaths as $scope => $paths) {
981999
}
9821000

9831001
if (count($scopedSchemas) === 0) {
984-
$scopedSchemas = new \stdClass();
1002+
$scopedSchemas = new stdClass();
9851003
} else {
9861004
ksort($scopedSchemas);
9871005
}
@@ -992,7 +1010,7 @@ foreach ($scopePaths as $scope => $paths) {
9921010
$pathsCount = count($openapiScope['paths']);
9931011
if ($pathsCount === 0) {
9941012
// Make sure the paths array is always a dictionary
995-
$openapiScope['paths'] = new \stdClass();
1013+
$openapiScope['paths'] = new stdClass();
9961014
}
9971015

9981016
$startExtension = strrpos($out, '.');

src/ControllerMethod.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public static function parse(string $context, array $definitions, ClassMethod $m
8484
}
8585

8686
if (str_starts_with($type->name, 'OCS') && str_ends_with($type->name, 'Exception')) {
87-
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(type: "array", maxItems: 0), null);
87+
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "application/json", new OpenApiType(context: $context, type: "array", maxItems: 0), null);
8888
} else {
89-
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(type: "string"), null);
89+
$responses[] = new ControllerMethodResponse($docNode->value->type, $statusCode, "text/plain", new OpenApiType(context: $context, type: "string"), null);
9090
}
9191
}
9292
}

0 commit comments

Comments
 (0)