fix: accept raw callable Function Responses without headersUnverified
Scaleway's minimal documented PHP response only carries statusCode and body, which raw callables were answering with a 500. Headers are now optional and stay absent when omitted, and a null isBase64Encoded is rejected instead of being turned into false. HttpConverter keeps returning headers, so its own return type stays narrower than the widened FunctionResponse.
ec8387c parent: 80a8c50 modified
docs/plan.md +1 -1 | @@ -18,7 +18,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | ||
| 18 | 18 | - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). |
| 19 | 19 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). |
| 20 | 20 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. |
| 21 | -- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. | |
| 21 | +- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. | |
| 22 | 22 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. |
| 23 | 23 | |
| 24 | 24 | ## Phases |
| @@ -18,7 +18,7 @@ Vocabulary follows [GLOSSARY.md](../GLOSSARY.md); decisions are recorded in [doc | |||
| 18 | - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). | 18 | - `vendor/bin/scaleway-package` builds a zip with `vendor/` (`--no-dev --classmap-authoritative`, from `composer.lock`) and a warmed prod cache, excluding `var/`, `tests/`, `.env.local` (ADR 0003). |
| 19 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). | 19 | - CRON Invocations become `POST /` HTTP requests by default; a JSON body `{"command": "...", "args": {...}, "secret": "..."}` whose secret matches `SCALEWAY_CRON_SECRET` makes it a Command Invocation run through the Console Application of the same Kernel, non-interactive, `args` mapped to an `ArrayInput`; the exit code 0 yields a 200 and any other a 500, both with the command output (ADR 0005). |
| 20 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. | 20 | - A Front Controller returning a Console `Application` or a `Command` serves Command Invocations only and answers a 403 to the others; services are reset before and after each command, as no HTTP request follows to trigger the Kernel's own reset. |
| 21 | -- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. | 21 | +- A Front Controller closure returning a `Closure` makes it a raw callable `fn (array $event, mixed $context): array`, built once like a Kernel (GenericRuntime would run it as a zero-argument script, meaningless per Invocation; invokable objects keep their type-based dispatch, a `$object(...)` turns one into a raw callable); it gets every Event and context untouched, CRON Invocations included without Command Invocation routing (raw means raw), and its return value must be the documented Function Response shape (int `statusCode`, optional `headers` of strings or lists of strings, string `body`, optional bool `isBase64Encoded`), undocumented keys dropped, anything else being a 500. |
| 22 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. | 22 | - Tests: PHPUnit on conversions, GitHub Actions CI (PHPUnit, PHPStan max, PHP-CS-Fixer; PHP 8.4/8.5 x Symfony 7.4/8.x), local Emulator `vendor/bin/scaleway-serve`, manual real E2E deployed with Terraform. |
| 23 | 23 | ||
| 24 | ## Phases | 24 | ## Phases |
modified
src/HttpConverter.php +2 -2 | @@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\Response; | ||
| 12 | 12 | /** |
| 13 | 13 | * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response. |
| 14 | 14 | * |
| 15 | - * @phpstan-type FunctionResponse array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: bool} | |
| 15 | + * @phpstan-type FunctionResponse array{statusCode: int, headers?: array<string, string|list<string>>, body: string, isBase64Encoded?: bool} | |
| 16 | 16 | */ |
| 17 | 17 | final class HttpConverter |
| 18 | 18 | { |
| @@ -144,7 +144,7 @@ final class HttpConverter | ||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
| 147 | - * @return FunctionResponse | |
| 147 | + * @return array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: true} | |
| 148 | 148 | */ |
| 149 | 149 | public static function toFunctionResponse(Response $response): array |
| 150 | 150 | { |
| @@ -12,7 +12,7 @@ use Symfony\Component\HttpFoundation\Response; | |||
| 12 | /** | 12 | /** |
| 13 | * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response. | 13 | * Converts an Event into a Symfony Request, and a Symfony Response into a Function Response. |
| 14 | * | 14 | * |
| 15 | - * @phpstan-type FunctionResponse array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: bool} | 15 | + * @phpstan-type FunctionResponse array{statusCode: int, headers?: array<string, string|list<string>>, body: string, isBase64Encoded?: bool} |
| 16 | */ | 16 | */ |
| 17 | final class HttpConverter | 17 | final class HttpConverter |
| 18 | { | 18 | { |
| @@ -144,7 +144,7 @@ final class HttpConverter | |||
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | /** | 146 | /** |
| 147 | - * @return FunctionResponse | 147 | + * @return array{statusCode: int, headers: array<string, string|list<string>>, body: string, isBase64Encoded?: true} |
| 148 | */ | 148 | */ |
| 149 | public static function toFunctionResponse(Response $response): array | 149 | public static function toFunctionResponse(Response $response): array |
| 150 | { | 150 | { |
modified
src/Runner/CallableRunner.php +13 -8 | @@ -24,20 +24,25 @@ final readonly class CallableRunner implements InvocationRunner | ||
| 24 | 24 | { |
| 25 | 25 | $response = ($this->callable)($event, $context); |
| 26 | 26 | |
| 27 | - // Scaleway only documents this shape for PHP, and what it makes of anything else is unknown: keys it does | |
| 28 | - // not document are dropped rather than passed along. | |
| 27 | + // Scaleway only documents this shape for PHP, its minimal example without headers, and what it makes of | |
| 28 | + // anything else is unknown: keys it does not document are dropped rather than passed along. | |
| 29 | 29 | $fields = \is_array($response) ? $response : []; |
| 30 | 30 | $statusCode = $fields['statusCode'] ?? null; |
| 31 | - $headers = $fields['headers'] ?? null; | |
| 32 | 31 | $body = $fields['body'] ?? null; |
| 33 | - $base64 = $fields['isBase64Encoded'] ?? false; | |
| 34 | - if (!\is_int($statusCode) || !self::isHeaders($headers) || !\is_string($body) || !\is_bool($base64)) { | |
| 35 | - throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response))); | |
| 32 | + if (!\is_int($statusCode) || !\is_string($body) | |
| 33 | + || \array_key_exists('headers', $fields) && !self::isHeaders($fields['headers']) | |
| 34 | + || \array_key_exists('isBase64Encoded', $fields) && !\is_bool($fields['isBase64Encoded']) | |
| 35 | + ) { | |
| 36 | + throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", optional "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response))); | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | - $functionResponse = ['statusCode' => $statusCode, 'headers' => $headers, 'body' => $body]; | |
| 39 | + $functionResponse = ['statusCode' => $statusCode, 'body' => $body]; | |
| 40 | + // Absent keys stay absent, as defaulting them would make up what the platform does without them. | |
| 41 | + if (\array_key_exists('headers', $fields)) { | |
| 42 | + $functionResponse['headers'] = $fields['headers']; | |
| 43 | + } | |
| 39 | 44 | if (\array_key_exists('isBase64Encoded', $fields)) { |
| 40 | - $functionResponse['isBase64Encoded'] = $base64; | |
| 45 | + $functionResponse['isBase64Encoded'] = $fields['isBase64Encoded']; | |
| 41 | 46 | } |
| 42 | 47 | |
| 43 | 48 | return $functionResponse; |
| @@ -24,20 +24,25 @@ final readonly class CallableRunner implements InvocationRunner | |||
| 24 | { | 24 | { |
| 25 | $response = ($this->callable)($event, $context); | 25 | $response = ($this->callable)($event, $context); |
| 26 | 26 | ||
| 27 | - // Scaleway only documents this shape for PHP, and what it makes of anything else is unknown: keys it does | 27 | + // Scaleway only documents this shape for PHP, its minimal example without headers, and what it makes of |
| 28 | - // not document are dropped rather than passed along. | 28 | + // anything else is unknown: keys it does not document are dropped rather than passed along. |
| 29 | $fields = \is_array($response) ? $response : []; | 29 | $fields = \is_array($response) ? $response : []; |
| 30 | $statusCode = $fields['statusCode'] ?? null; | 30 | $statusCode = $fields['statusCode'] ?? null; |
| 31 | - $headers = $fields['headers'] ?? null; | ||
| 32 | $body = $fields['body'] ?? null; | 31 | $body = $fields['body'] ?? null; |
| 33 | - $base64 = $fields['isBase64Encoded'] ?? false; | 32 | + if (!\is_int($statusCode) || !\is_string($body) |
| 34 | - if (!\is_int($statusCode) || !self::isHeaders($headers) || !\is_string($body) || !\is_bool($base64)) { | 33 | + || \array_key_exists('headers', $fields) && !self::isHeaders($fields['headers']) |
| 35 | - throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response))); | 34 | + || \array_key_exists('isBase64Encoded', $fields) && !\is_bool($fields['isBase64Encoded']) |
| 35 | + ) { | ||
| 36 | + throw new \UnexpectedValueException(\sprintf('A raw callable must return a Function Response: an array with an int "statusCode", optional "headers" mapping names to a string or a list of strings, a string "body" and an optional bool "isBase64Encoded"; "%s" returned.', get_debug_type($response))); | ||
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | - $functionResponse = ['statusCode' => $statusCode, 'headers' => $headers, 'body' => $body]; | 39 | + $functionResponse = ['statusCode' => $statusCode, 'body' => $body]; |
| 40 | + // Absent keys stay absent, as defaulting them would make up what the platform does without them. | ||
| 41 | + if (\array_key_exists('headers', $fields)) { | ||
| 42 | + $functionResponse['headers'] = $fields['headers']; | ||
| 43 | + } | ||
| 39 | if (\array_key_exists('isBase64Encoded', $fields)) { | 44 | if (\array_key_exists('isBase64Encoded', $fields)) { |
| 40 | - $functionResponse['isBase64Encoded'] = $base64; | 45 | + $functionResponse['isBase64Encoded'] = $fields['isBase64Encoded']; |
| 41 | } | 46 | } |
| 42 | 47 | ||
| 43 | return $functionResponse; | 48 | return $functionResponse; |
modified
tests/Fixtures/front-controllers/raw.php +2 -0 | @@ -22,6 +22,8 @@ return static function (): Closure { | ||
| 22 | 22 | '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'], |
| 23 | 23 | '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''], |
| 24 | 24 | '/string' => 'plain body', |
| 25 | + '/null-base64' => ['statusCode' => 200, 'body' => '', 'isBase64Encoded' => null], | |
| 26 | + '/minimal' => ['statusCode' => 200, 'body' => 'no headers'], | |
| 25 | 27 | default => [ |
| 26 | 28 | 'statusCode' => 202, |
| 27 | 29 | 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], |
| @@ -22,6 +22,8 @@ return static function (): Closure { | |||
| 22 | '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'], | 22 | '/invalid' => ['statusCode' => '200', 'headers' => [], 'body' => 'not an int status'], |
| 23 | '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''], | 23 | '/bad-header' => ['statusCode' => 200, 'headers' => ['X-Bad' => ['a' => 'b']], 'body' => ''], |
| 24 | '/string' => 'plain body', | 24 | '/string' => 'plain body', |
| 25 | + '/null-base64' => ['statusCode' => 200, 'body' => '', 'isBase64Encoded' => null], | ||
| 26 | + '/minimal' => ['statusCode' => 200, 'body' => 'no headers'], | ||
| 25 | default => [ | 27 | default => [ |
| 26 | 'statusCode' => 202, | 28 | 'statusCode' => 202, |
| 27 | 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], | 29 | 'headers' => ['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], |
modified
tests/HandlerTest.php +9 -1 | @@ -267,7 +267,7 @@ final class HandlerTest extends TestCase | ||
| 267 | 267 | $second = $handler->invoke(['path' => '/again'], null); |
| 268 | 268 | |
| 269 | 269 | self::assertSame(202, $first['statusCode']); |
| 270 | - self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers']); | |
| 270 | + self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers'] ?? null); | |
| 271 | 271 | self::assertFalse($first['isBase64Encoded'] ?? null); |
| 272 | 272 | self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.'); |
| 273 | 273 | self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first)); |
| @@ -275,6 +275,13 @@ final class HandlerTest extends TestCase | ||
| 275 | 275 | self::assertSame(1, RecordingKernel::$builds); |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | + public function testRawCallableMayOmitHeadersAsInScalewaysMinimalExample(): void | |
| 279 | + { | |
| 280 | + $response = $this->handler(['front_controller' => 'front-controllers/raw.php'])->invoke(['path' => '/minimal'], []); | |
| 281 | + | |
| 282 | + self::assertSame(['statusCode' => 200, 'body' => 'no headers'], $response); | |
| 283 | + } | |
| 284 | + | |
| 278 | 285 | public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void |
| 279 | 286 | { |
| 280 | 287 | $event = $this->commandEvent('app:greet'); |
| @@ -304,6 +311,7 @@ final class HandlerTest extends TestCase | ||
| 304 | 311 | yield 'non-int status' => ['/invalid', 'must return a Function Response']; |
| 305 | 312 | yield 'nested header value' => ['/bad-header', 'must return a Function Response']; |
| 306 | 313 | yield 'plain string' => ['/string', '"string" returned']; |
| 314 | + yield 'null base64 flag' => ['/null-base64', 'must return a Function Response']; | |
| 307 | 315 | } |
| 308 | 316 | |
| 309 | 317 | #[DataProvider('failingRawCallables')] |
| @@ -267,7 +267,7 @@ final class HandlerTest extends TestCase | |||
| 267 | $second = $handler->invoke(['path' => '/again'], null); | 267 | $second = $handler->invoke(['path' => '/again'], null); |
| 268 | 268 | ||
| 269 | self::assertSame(202, $first['statusCode']); | 269 | self::assertSame(202, $first['statusCode']); |
| 270 | - self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers']); | 270 | + self::assertSame(['Content-Type' => 'application/json', 'X-Multi' => ['a', 'b']], $first['headers'] ?? null); |
| 271 | self::assertFalse($first['isBase64Encoded'] ?? null); | 271 | self::assertFalse($first['isBase64Encoded'] ?? null); |
| 272 | self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.'); | 272 | self::assertArrayNotHasKey('multiValueHeaders', $first, 'Keys Scaleway does not document must be dropped.'); |
| 273 | self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first)); | 273 | self::assertSame(['calls' => 1, 'event' => $event, 'context' => ['memoryLimitInMb' => 128]], $this->decode($first)); |
| @@ -275,6 +275,13 @@ final class HandlerTest extends TestCase | |||
| 275 | self::assertSame(1, RecordingKernel::$builds); | 275 | self::assertSame(1, RecordingKernel::$builds); |
| 276 | } | 276 | } |
| 277 | 277 | ||
| 278 | + public function testRawCallableMayOmitHeadersAsInScalewaysMinimalExample(): void | ||
| 279 | + { | ||
| 280 | + $response = $this->handler(['front_controller' => 'front-controllers/raw.php'])->invoke(['path' => '/minimal'], []); | ||
| 281 | + | ||
| 282 | + self::assertSame(['statusCode' => 200, 'body' => 'no headers'], $response); | ||
| 283 | + } | ||
| 284 | + | ||
| 278 | public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void | 285 | public function testRawCallableGetsCommandInvocationsAsPlainEvents(): void |
| 279 | { | 286 | { |
| 280 | $event = $this->commandEvent('app:greet'); | 287 | $event = $this->commandEvent('app:greet'); |
| @@ -304,6 +311,7 @@ final class HandlerTest extends TestCase | |||
| 304 | yield 'non-int status' => ['/invalid', 'must return a Function Response']; | 311 | yield 'non-int status' => ['/invalid', 'must return a Function Response']; |
| 305 | yield 'nested header value' => ['/bad-header', 'must return a Function Response']; | 312 | yield 'nested header value' => ['/bad-header', 'must return a Function Response']; |
| 306 | yield 'plain string' => ['/string', '"string" returned']; | 313 | yield 'plain string' => ['/string', '"string" returned']; |
| 314 | + yield 'null base64 flag' => ['/null-base64', 'must return a Function Response']; | ||
| 307 | } | 315 | } |
| 308 | 316 | ||
| 309 | #[DataProvider('failingRawCallables')] | 317 | #[DataProvider('failingRawCallables')] |