Skip to content

Commit

Permalink
fix(ResponseType): Correctly add empty responses for 204/304
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin authored and nickvergessen committed Apr 8, 2024
1 parent ae84659 commit ca33281
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,29 @@ public static function resolve(string $context, TypeNode $obj): array {
$contentTypes = $contentTypes !== [] ? $contentTypes : [$type != null ? "*/*" : null];

foreach ($statusCodes as $statusCode) {
foreach ($contentTypes as $contentType) {
if ($statusCode === 204 || $statusCode === 304) {
$customHeaders = array_filter(array_keys($headers), static fn (string $header) => str_starts_with(strtolower($header), 'x-'));
if (!empty($customHeaders)) {
Logger::error($context, 'Custom headers are not allowed for responses with status code 204 or 304. Found: '. implode(', ', $customHeaders));
}

$responses[] = new ControllerMethodResponse(
$className,
$statusCode,
$contentType,
$type,
null,
null,
$headers,
);
} else {
foreach ($contentTypes as $contentType) {
$responses[] = new ControllerMethodResponse(
$className,
$statusCode,
$contentType,
$type,
$headers,
);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@
['name' => 'Settings#arrayKeyedParameter', 'url' => '/api/{apiVersion}/array-keyed', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#throwingOCS', 'url' => '/api/{apiVersion}/throwing/ocs', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#throwingOther', 'url' => '/api/{apiVersion}/throwing/other', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#empty204', 'url' => '/api/{apiVersion}/204', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
['name' => 'Settings#empty304', 'url' => '/api/{apiVersion}/304', 'verb' => 'POST', 'requirements' => ['apiVersion' => '(v2)']],
],
];
22 changes: 22 additions & 0 deletions tests/lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,26 @@ public function throwingOCS(): DataResponse {
public function throwingOther(): DataResponse {
throw new NotFoundException();
}

/**
* A route 204 response
*
* @return DataResponse<Http::STATUS_NO_CONTENT, array<empty>, array{}>
*
* 204: No settings
*/
public function empty204(): DataResponse {
return new DataResponse();
}

/**
* A route 304 response
*
* @return DataResponse<Http::STATUS_NOT_MODIFIED, array<empty>, array{}>
*
* 304: No settings
*/
public function empty304(): DataResponse {
return new DataResponse();
}
}
94 changes: 94 additions & 0 deletions tests/openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -2328,6 +2328,100 @@
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/204": {
"post": {
"operationId": "settings-empty204",
"summary": "A route 204 response",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"204": {
"description": "No settings"
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/304": {
"post": {
"operationId": "settings-empty304",
"summary": "A route 304 response",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"304": {
"description": "No settings"
}
}
}
},
"/ocs/v2.php/tests/attribute-ocs/{param}": {
"get": {
"operationId": "routing-attributeocs-route",
Expand Down
94 changes: 94 additions & 0 deletions tests/openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,100 @@
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/204": {
"post": {
"operationId": "settings-empty204",
"summary": "A route 204 response",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"204": {
"description": "No settings"
}
}
}
},
"/ocs/v2.php/apps/notifications/api/{apiVersion}/304": {
"post": {
"operationId": "settings-empty304",
"summary": "A route 304 response",
"description": "This endpoint requires admin access",
"tags": [
"settings"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"parameters": [
{
"name": "apiVersion",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": [
"v2"
],
"default": "v2"
}
},
{
"name": "OCS-APIRequest",
"in": "header",
"description": "Required to be true for the API request to pass",
"required": true,
"schema": {
"type": "boolean",
"default": true
}
}
],
"responses": {
"304": {
"description": "No settings"
}
}
}
},
"/ocs/v2.php/tests/attribute-ocs/{param}": {
"get": {
"operationId": "routing-attributeocs-route",
Expand Down

0 comments on commit ca33281

Please sign in to comment.