Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ResponseType): Correctly add empty responses for 204/304 #111

Merged
merged 1 commit into from
Apr 8, 2024
Merged
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
23 changes: 20 additions & 3 deletions src/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,31 @@ 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) {
if ($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{}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could have added a header to sample/test it?

*
* 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
Loading