Skip to content

Commit 2bb9969

Browse files
Merge pull request #63389 from nextcloud/fix/combine-responses
fix: combine StreamGeneratorResponse with StreamTraversableResponse
2 parents b051397 + f4a3c22 commit 2bb9969

9 files changed

Lines changed: 40 additions & 90 deletions

File tree

apps/dav/lib/Controller/CalendarExportController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1616
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1717
use OCP\AppFramework\Http\DataResponse;
18-
use OCP\AppFramework\Http\StreamGeneratorResponse;
18+
use OCP\AppFramework\Http\StreamTraversableResponse;
1919
use OCP\AppFramework\OCSController;
2020
use OCP\Calendar\CalendarExportOptions;
2121
use OCP\Calendar\ICalendarExport;
@@ -46,7 +46,7 @@ public function __construct(
4646
* @param array{rangeStart:string,rangeCount:positive-int} $options configuration options
4747
* @param string|null $user system user id
4848
*
49-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
49+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5050
*
5151
* 200: data in requested format
5252
* 400: invalid parameters
@@ -99,7 +99,7 @@ public function export(string $target, ?string $type = null, ?array $options = n
9999
'xcal' => 'application/calendar+xml; charset=UTF-8',
100100
default => 'text/calendar; charset=UTF-8'
101101
};
102-
$response = new StreamGeneratorResponse($this->exportService->export($calendar, $options), $contentType, Http::STATUS_OK);
102+
$response = new StreamTraversableResponse($this->exportService->export($calendar, $options), Http::STATUS_OK, ['Content-Type' => $contentType]);
103103
$response->cacheFor(0);
104104

105105
return $response;

apps/dav/lib/Controller/CalendarImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2121
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2222
use OCP\AppFramework\Http\DataResponse;
23-
use OCP\AppFramework\Http\StreamGeneratorResponse;
23+
use OCP\AppFramework\Http\StreamTraversableResponse;
2424
use OCP\AppFramework\OCSController;
2525
use OCP\Calendar\CalendarImportOptions;
2626
use OCP\Calendar\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data calendar data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/calendar')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$calendarId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -144,6 +144,6 @@ public function import(string $transaction, string $target, array $options, stri
144144
}
145145
})();
146146

147-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
147+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
148148
}
149149
}

apps/dav/lib/Controller/ContactsImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2020
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2121
use OCP\AppFramework\Http\DataResponse;
22-
use OCP\AppFramework\Http\StreamGeneratorResponse;
22+
use OCP\AppFramework\Http\StreamTraversableResponse;
2323
use OCP\AppFramework\OCSController;
2424
use OCP\Contacts\ContactsImportOptions;
2525
use OCP\Contacts\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data contacts data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/contacts')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$addressBookId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -141,6 +141,6 @@ public function import(string $transaction, string $target, array $options, stri
141141
}
142142
})();
143143

144-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
144+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
145145
}
146146
}

apps/dav/openapi.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,17 +1660,20 @@
16601660
"content": {
16611661
"text/calendar; charset=UTF-8": {
16621662
"schema": {
1663-
"anyOf": []
1663+
"type": "string",
1664+
"format": "binary"
16641665
}
16651666
},
16661667
"application/calendar+json; charset=UTF-8": {
16671668
"schema": {
1668-
"anyOf": []
1669+
"type": "string",
1670+
"format": "binary"
16691671
}
16701672
},
16711673
"application/calendar+xml; charset=UTF-8": {
16721674
"schema": {
1673-
"anyOf": []
1675+
"type": "string",
1676+
"format": "binary"
16741677
}
16751678
}
16761679
}
@@ -1885,7 +1888,8 @@
18851888
"content": {
18861889
"application/x-ndjson": {
18871890
"schema": {
1888-
"anyOf": []
1891+
"type": "string",
1892+
"format": "binary"
18891893
}
18901894
}
18911895
}
@@ -2088,7 +2092,8 @@
20882092
"content": {
20892093
"application/x-ndjson": {
20902094
"schema": {
2091-
"anyOf": []
2095+
"type": "string",
2096+
"format": "binary"
20922097
}
20932098
}
20942099
}

lib/composer/composer/autoload_classmap.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@
166166
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => $baseDir . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
167167
'OCP\\AppFramework\\Http\\Response' => $baseDir . '/lib/public/AppFramework/Http/Response.php',
168168
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
169-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
170169
'OCP\\AppFramework\\Http\\StreamResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamResponse.php',
171170
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
172171
'OCP\\AppFramework\\Http\\TemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/composer/composer/autoload_static.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
207207
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
208208
'OCP\\AppFramework\\Http\\Response' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/Response.php',
209209
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
210-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
211210
'OCP\\AppFramework\\Http\\StreamResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamResponse.php',
212211
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
213212
'OCP\\AppFramework\\Http\\TemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/public/AppFramework/Http/StreamGeneratorResponse.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

openapi.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21661,17 +21661,20 @@
2166121661
"content": {
2166221662
"text/calendar; charset=UTF-8": {
2166321663
"schema": {
21664-
"anyOf": []
21664+
"type": "string",
21665+
"format": "binary"
2166521666
}
2166621667
},
2166721668
"application/calendar+json; charset=UTF-8": {
2166821669
"schema": {
21669-
"anyOf": []
21670+
"type": "string",
21671+
"format": "binary"
2167021672
}
2167121673
},
2167221674
"application/calendar+xml; charset=UTF-8": {
2167321675
"schema": {
21674-
"anyOf": []
21676+
"type": "string",
21677+
"format": "binary"
2167521678
}
2167621679
}
2167721680
}
@@ -21886,7 +21889,8 @@
2188621889
"content": {
2188721890
"application/x-ndjson": {
2188821891
"schema": {
21889-
"anyOf": []
21892+
"type": "string",
21893+
"format": "binary"
2189021894
}
2189121895
}
2189221896
}
@@ -22089,7 +22093,8 @@
2208922093
"content": {
2209022094
"application/x-ndjson": {
2209122095
"schema": {
22092-
"anyOf": []
22096+
"type": "string",
22097+
"format": "binary"
2209322098
}
2209422099
}
2209522100
}

tests/lib/AppFramework/Http/StreamGeneratorResponseTest.php renamed to tests/lib/AppFramework/Http/StreamTraversableResponseTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
namespace Test\AppFramework\Http;
1010

1111
use OCP\AppFramework\Http\IOutput;
12-
use OCP\AppFramework\Http\StreamGeneratorResponse;
12+
use OCP\AppFramework\Http\StreamTraversableResponse;
1313

14-
class StreamGeneratorResponseTest extends \Test\TestCase {
14+
class StreamTraversableResponseTest extends \Test\TestCase {
1515

1616
protected function setUp(): void {
1717
parent::setUp();
@@ -22,7 +22,7 @@ public function testConstructor() {
2222
yield 'chunk1';
2323
yield 'chunk2';
2424
};
25-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
25+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
2626

2727
$headers = $response->getHeaders();
2828
$this->assertEquals('text/plain', $headers['Content-Type']);
@@ -37,8 +37,13 @@ public function testCallback() {
3737
$count++;
3838
yield 'chunk2';
3939
};
40-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
40+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
4141
$output = $this->createMock(IOutput::class);
42+
$output->expects($this->exactly(2))
43+
->method('setOutput')
44+
->with($this->callback(function ($chunk) {
45+
return in_array($chunk, ['chunk1', 'chunk2'], true);
46+
}));
4247

4348
$response->callback($output);
4449
$this->assertEquals($count, 2);

0 commit comments

Comments
 (0)