From b5990ffc0de1b6eec163d423b472869ee57362d7 Mon Sep 17 00:00:00 2001 From: "pascal.heidmann" Date: Wed, 21 Feb 2024 14:40:12 +0100 Subject: [PATCH 1/4] Decouple `$includeThrowableDetail` from generic `debug` config to allow finer control what to display from a throwable by adding optional `include-throwable-details` to `problem-details` config Signed-off-by: pascal.heidmann --- docs/book/response.md | 5 +++++ src/ProblemDetailsResponseFactoryFactory.php | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/book/response.md b/docs/book/response.md index 5a52153..54999f8 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -151,6 +151,11 @@ This package also provides a factory for generating the value is provided as the `$defaultTypesMap` parameter; see the [default types documentation](default-types.md) for details on defining this map. (Since 1.1.0.) + - If the service contains a `problem-details` key with an array value + containing a `include-throwable-details` key, + and that value is a boolean, + that value is used instead of global `debug` value for the `$includeThrowableDetail` parameter. + (Since 1.14.0.) If any of the above config values are not present, a `null` value will be passed, allowing the default value to be used. diff --git a/src/ProblemDetailsResponseFactoryFactory.php b/src/ProblemDetailsResponseFactoryFactory.php index aaaf2a6..2bf563e 100644 --- a/src/ProblemDetailsResponseFactoryFactory.php +++ b/src/ProblemDetailsResponseFactoryFactory.php @@ -20,15 +20,22 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF { $config = $container->has('config') ? $container->get('config') : []; Assert::isArrayAccessible($config); - $debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null; - $includeThrowableDetail = $debug ?? ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS; + $debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null; + $debug ??= ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS; $problemDetailsConfig = $config['problem-details'] ?? []; Assert::isArrayAccessible($problemDetailsConfig); + + $includeThrowableDetail = isset($problemDetailsConfig['include-throwable-details']) + && is_bool($problemDetailsConfig['include-throwable-details']) + ? $problemDetailsConfig['include-throwable-details'] : null; + $includeThrowableDetail ??= $debug; + $jsonFlags = $problemDetailsConfig['json_flags'] ?? null; assert($jsonFlags === null || is_int($jsonFlags)); $defaultTypesMap = $problemDetailsConfig['default_types_map'] ?? []; Assert::isArray($defaultTypesMap); + foreach ($defaultTypesMap as $key => $value) { assert(is_int($key)); assert(is_string($value)); @@ -38,7 +45,7 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF return new ProblemDetailsResponseFactory( $this->detectResponseFactory($container), - $includeThrowableDetail, + $debug, $jsonFlags, $includeThrowableDetail, ProblemDetailsResponseFactory::DEFAULT_DETAIL_MESSAGE, From a40ff3b347804a2a95b042489202aa989f018d12 Mon Sep 17 00:00:00 2001 From: "pascal.heidmann" Date: Thu, 22 Feb 2024 17:06:35 +0100 Subject: [PATCH 2/4] Add test case for new config `problem-details.include-throwable-details` Signed-off-by: pascal.heidmann --- ...oblemDetailsResponseFactoryFactoryTest.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/ProblemDetailsResponseFactoryFactoryTest.php b/test/ProblemDetailsResponseFactoryFactoryTest.php index acab0c1..55328ab 100644 --- a/test/ProblemDetailsResponseFactoryFactoryTest.php +++ b/test/ProblemDetailsResponseFactoryFactoryTest.php @@ -59,7 +59,7 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults(): void { $response = $this->createMock(ResponseInterface::class); $response->method('withStatus')->willReturnSelf(); - $this->container->set(ResponseInterface::class, static fn () => $response); + $this->container->set(ResponseInterface::class, static fn() => $response); $factoryFactory = new ProblemDetailsResponseFactoryFactory(); $factory = $factoryFactory($this->container); @@ -132,4 +132,25 @@ public function testUsesDefaultTypesSettingFromConfigWhenPresent(): void self::assertSame($expectedDefaultTypes, $defaultTypesMap->getValue($factory)); } + + public function testUsesIncludeThrowableDetailsSettingFromConfigWhenPresent(): void + { + $this->container->set( + 'config', + [ + 'problem-details' => [ + 'include-throwable-details' => ProblemDetailsResponseFactory::INCLUDE_THROWABLE_DETAILS, + ], + ] + ); + $this->container->set(ResponseInterface::class, static fn() => null); + + $factoryFactory = new ProblemDetailsResponseFactoryFactory(); + $factory = $factoryFactory($this->container); + $isDebug = (new ReflectionObject($factory))->getProperty('isDebug'); + $exceptionDetailsInResponse = (new ReflectionObject($factory))->getProperty('exceptionDetailsInResponse'); + + self::assertSame(ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS, $isDebug->getValue($factory)); + self::assertSame(true, $exceptionDetailsInResponse->getValue($factory)); + } } From 347b3b0dc2f8a5383d83153d10593aea56973449 Mon Sep 17 00:00:00 2001 From: "pascal.heidmann" Date: Fri, 23 Feb 2024 09:37:55 +0100 Subject: [PATCH 3/4] Add example configuration for `ProblemDetailsResponseFactoryFactory` in response.md Signed-off-by: pascal.heidmann --- docs/book/response.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/book/response.md b/docs/book/response.md index 54999f8..faecce9 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -157,6 +157,17 @@ This package also provides a factory for generating the that value is used instead of global `debug` value for the `$includeThrowableDetail` parameter. (Since 1.14.0.) +### Example configuration +```php +[ + 'debug' => true, + 'problem-details' => [ + 'json_flags' => JSON_PRETTY_PRINT, + 'include-throwable-details' => true, + ] +] +``` + If any of the above config values are not present, a `null` value will be passed, allowing the default value to be used. From 5a773ab9cba945f0d0b112d99f5abc7f4afb2d5d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 26 Feb 2024 11:46:08 +0100 Subject: [PATCH 4/4] Added whitespace around fenced code block Signed-off-by: Marco Pivetta --- docs/book/response.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/book/response.md b/docs/book/response.md index faecce9..a390dc2 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -158,6 +158,7 @@ This package also provides a factory for generating the (Since 1.14.0.) ### Example configuration + ```php [ 'debug' => true,