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

Add types to responses #38802

Merged
merged 1 commit into from
Jun 30, 2023
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
10 changes: 9 additions & 1 deletion lib/private/AppFramework/OCS/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Joas Schilling <[email protected]>
* @author Lukas Reschke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -30,6 +31,13 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
abstract class BaseResponse extends Response {
/** @var array */
protected $data;
Expand All @@ -49,7 +57,7 @@ abstract class BaseResponse extends Response {
/**
* BaseResponse constructor.
*
* @param DataResponse $dataResponse
* @param DataResponse<S, T, H> $dataResponse
* @param string $format
* @param string|null $statusMessage
* @param int|null $itemsCount
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V1Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Christoph Wurst <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -25,8 +26,16 @@
namespace OC\AppFramework\OCS;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V1Response extends BaseResponse {
/**
* The V1 endpoint has very limited http status codes basically everything
Expand Down
9 changes: 9 additions & 0 deletions lib/private/AppFramework/OCS/V2Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -24,8 +25,16 @@
namespace OC\AppFramework\OCS;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;

/**
* @psalm-import-type DataResponseType from DataResponse
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends BaseResponse<int, DataResponseType, array<string, mixed>>
*/
class V2Response extends BaseResponse {
/**
* The V2 endpoint just passes on status codes.
Expand Down
15 changes: 8 additions & 7 deletions lib/public/AppFramework/Http/DataDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Julius Härtl <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -30,6 +31,9 @@
* Class DataDisplayResponse
*
* @since 8.1.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DataDisplayResponse extends Response {
/**
Expand All @@ -41,17 +45,14 @@ class DataDisplayResponse extends Response {

/**
* @param string $data the data to display
* @param int $statusCode the Http status code, defaults to 200
* @param array $headers additional key value based headers
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers additional key value based headers
* @since 8.1.0
*/
public function __construct($data = '', $statusCode = Http::STATUS_OK,
$headers = []) {
parent::__construct();
public function __construct(string $data = '', int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
$this->addHeader('Content-Disposition', 'inline; filename=""');
}

Expand Down
15 changes: 12 additions & 3 deletions lib/public/AppFramework/Http/DataDownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author Georg Ehrke <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -23,10 +24,16 @@
*/
namespace OCP\AppFramework\Http;

use OCP\AppFramework\Http;

/**
* Class DataDownloadResponse
*
* @since 8.0.0
* @template S of int
* @template C of string
* @template H of array<string, mixed>
* @template-extends DownloadResponse<int, string, array<string, mixed>>
*/
class DataDownloadResponse extends DownloadResponse {
/**
Expand All @@ -38,12 +45,14 @@ class DataDownloadResponse extends DownloadResponse {
* Creates a response that prompts the user to download the text
* @param string $data text to be downloaded
* @param string $filename the name that the downloaded file should have
* @param string $contentType the mimetype that the downloaded file should have
* @param C $contentType the mimetype that the downloaded file should have
* @param S $status
* @param H $headers
* @since 8.0.0
*/
public function __construct($data, $filename, $contentType) {
public function __construct(string $data, string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
$this->data = $data;
parent::__construct($filename, $contentType);
parent::__construct($filename, $contentType, $status, $headers);
}

/**
Expand Down
26 changes: 15 additions & 11 deletions lib/public/AppFramework/Http/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @author Christoph Wurst <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -30,34 +31,37 @@
* A generic DataResponse class that is used to return generic data responses
* for responders to transform
* @since 8.0.0
* @psalm-type DataResponseType = array|int|float|string|bool|object|null|\stdClass|\JsonSerializable
* @template S of int
* @template-covariant T of DataResponseType
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DataResponse extends Response {
/**
* response data
* @var array|int|float|string|bool|object
* @var T
*/
protected $data;


/**
* @param array|int|float|string|bool|object $data the object or array that should be transformed
* @param int $statusCode the Http status code, defaults to 200
* @param array $headers additional key value based headers
* @param T $data the object or array that should be transformed
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers additional key value based headers
* @since 8.0.0
*/
public function __construct($data = [], $statusCode = Http::STATUS_OK,
array $headers = []) {
parent::__construct();
public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
}


/**
* Sets values in the data json array
* @param array|int|float|string|object $data an array or object which will be transformed
* @psalm-suppress InvalidTemplateParam
* @param T $data an array or object which will be transformed
* @return DataResponse Reference to this object
* @since 8.0.0
*/
Expand All @@ -70,7 +74,7 @@ public function setData($data) {

/**
* Used to get the set parameters
* @return array|int|float|string|bool|object the data
* @return T the data
* @since 8.0.0
*/
public function getData() {
Expand Down
15 changes: 12 additions & 3 deletions lib/public/AppFramework/Http/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Morris Jobke <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -25,19 +26,27 @@
*/
namespace OCP\AppFramework\Http;

use OCP\AppFramework\Http;

/**
* Prompts the user to download the a file
* @since 7.0.0
* @template S of int
* @template C of string
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class DownloadResponse extends Response {
/**
* Creates a response that prompts the user to download the file
* @param string $filename the name that the downloaded file should have
* @param string $contentType the mimetype that the downloaded file should have
* @param C $contentType the mimetype that the downloaded file should have
* @param S $status
* @param H $headers
* @since 7.0.0
*/
public function __construct(string $filename, string $contentType) {
parent::__construct();
public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
parent::__construct($status, $headers);

$filename = strtr($filename, ['"' => '\\"', '\\' => '\\\\']);

Expand Down
21 changes: 12 additions & 9 deletions lib/public/AppFramework/Http/FileDisplayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @author Christoph Wurst <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -24,31 +25,33 @@
namespace OCP\AppFramework\Http;

use OCP\AppFramework\Http;
use OCP\Files\File;
use OCP\Files\SimpleFS\ISimpleFile;

/**
* Class FileDisplayResponse
*
* @since 11.0.0
* @template S of int
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class FileDisplayResponse extends Response implements ICallbackResponse {
/** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */
/** @var File|ISimpleFile */
private $file;

/**
* FileDisplayResponse constructor.
*
* @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file
* @param int $statusCode
* @param array $headers
* @param File|ISimpleFile $file
* @param S $statusCode
* @param H $headers
* @since 11.0.0
*/
public function __construct($file, $statusCode = Http::STATUS_OK,
$headers = []) {
parent::__construct();
public function __construct(File|ISimpleFile $file, int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->file = $file;
$this->setStatus($statusCode);
$this->setHeaders(array_merge($this->getHeaders(), $headers));
$this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');

$this->setETag($file->getEtag());
Expand Down
23 changes: 14 additions & 9 deletions lib/public/AppFramework/Http/JSONResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Thomas Tanghus <[email protected]>
* @author Kate Döen <[email protected]>
*
* @license AGPL-3.0
*
Expand All @@ -32,26 +33,30 @@
/**
* A renderer for JSON calls
* @since 6.0.0
* @template S of int
* @template-covariant T of array|object|\stdClass|\JsonSerializable
* @template H of array<string, mixed>
* @template-extends Response<int, array<string, mixed>>
*/
class JSONResponse extends Response {
/**
* response data
* @var array|object
* @var T
*/
protected $data;


/**
* constructor of JSONResponse
* @param array|object $data the object or array that should be transformed
* @param int $statusCode the Http status code, defaults to 200
* @param T $data the object or array that should be transformed
* @param S $statusCode the Http status code, defaults to 200
* @param H $headers
* @since 6.0.0
*/
public function __construct($data = [], $statusCode = Http::STATUS_OK) {
parent::__construct();
public function __construct(mixed $data = [], int $statusCode = Http::STATUS_OK, array $headers = []) {
parent::__construct($statusCode, $headers);

$this->data = $data;
$this->setStatus($statusCode);
$this->addHeader('Content-Type', 'application/json; charset=utf-8');
}

Expand All @@ -68,7 +73,8 @@ public function render() {

/**
* Sets values in the data json array
* @param array|object $data an array or object which will be transformed
* @psalm-suppress InvalidTemplateParam
* @param T $data an array or object which will be transformed
* to JSON
* @return JSONResponse Reference to this object
* @since 6.0.0 - return value was added in 7.0.0
Expand All @@ -81,8 +87,7 @@ public function setData($data) {


/**
* Used to get the set parameters
* @return array the data
* @return T the data
* @since 6.0.0
*/
public function getData() {
Expand Down
Loading
Loading