Skip to content

Commit 7d04492

Browse files
authored
Merge pull request #52 from pascalheidmann/separate-details-config
Decouple `$includeThrowableDetail` from generic `debug` config
2 parents 97b490e + 5a773ab commit 7d04492

3 files changed

+49
-4
lines changed

docs/book/response.md

+17
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ This package also provides a factory for generating the
151151
value is provided as the `$defaultTypesMap` parameter; see the
152152
[default types documentation](default-types.md) for details on defining
153153
this map. (Since 1.1.0.)
154+
- If the service contains a `problem-details` key with an array value
155+
containing a `include-throwable-details` key,
156+
and that value is a boolean,
157+
that value is used instead of global `debug` value for the `$includeThrowableDetail` parameter.
158+
(Since 1.14.0.)
159+
160+
### Example configuration
161+
162+
```php
163+
[
164+
'debug' => true,
165+
'problem-details' => [
166+
'json_flags' => JSON_PRETTY_PRINT,
167+
'include-throwable-details' => true,
168+
]
169+
]
170+
```
154171

155172
If any of the above config values are not present, a `null` value will be
156173
passed, allowing the default value to be used.

src/ProblemDetailsResponseFactoryFactory.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF
2020
{
2121
$config = $container->has('config') ? $container->get('config') : [];
2222
Assert::isArrayAccessible($config);
23-
$debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null;
24-
$includeThrowableDetail = $debug ?? ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS;
23+
$debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null;
24+
$debug ??= ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS;
2525

2626
$problemDetailsConfig = $config['problem-details'] ?? [];
2727
Assert::isArrayAccessible($problemDetailsConfig);
28+
29+
$includeThrowableDetail = isset($problemDetailsConfig['include-throwable-details'])
30+
&& is_bool($problemDetailsConfig['include-throwable-details'])
31+
? $problemDetailsConfig['include-throwable-details'] : null;
32+
$includeThrowableDetail ??= $debug;
33+
2834
$jsonFlags = $problemDetailsConfig['json_flags'] ?? null;
2935
assert($jsonFlags === null || is_int($jsonFlags));
3036
$defaultTypesMap = $problemDetailsConfig['default_types_map'] ?? [];
3137
Assert::isArray($defaultTypesMap);
38+
3239
foreach ($defaultTypesMap as $key => $value) {
3340
assert(is_int($key));
3441
assert(is_string($value));
@@ -38,7 +45,7 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF
3845

3946
return new ProblemDetailsResponseFactory(
4047
$this->detectResponseFactory($container),
41-
$includeThrowableDetail,
48+
$debug,
4249
$jsonFlags,
4350
$includeThrowableDetail,
4451
ProblemDetailsResponseFactory::DEFAULT_DETAIL_MESSAGE,

test/ProblemDetailsResponseFactoryFactoryTest.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults(): void
5959
{
6060
$response = $this->createMock(ResponseInterface::class);
6161
$response->method('withStatus')->willReturnSelf();
62-
$this->container->set(ResponseInterface::class, static fn () => $response);
62+
$this->container->set(ResponseInterface::class, static fn() => $response);
6363

6464
$factoryFactory = new ProblemDetailsResponseFactoryFactory();
6565
$factory = $factoryFactory($this->container);
@@ -132,4 +132,25 @@ public function testUsesDefaultTypesSettingFromConfigWhenPresent(): void
132132

133133
self::assertSame($expectedDefaultTypes, $defaultTypesMap->getValue($factory));
134134
}
135+
136+
public function testUsesIncludeThrowableDetailsSettingFromConfigWhenPresent(): void
137+
{
138+
$this->container->set(
139+
'config',
140+
[
141+
'problem-details' => [
142+
'include-throwable-details' => ProblemDetailsResponseFactory::INCLUDE_THROWABLE_DETAILS,
143+
],
144+
]
145+
);
146+
$this->container->set(ResponseInterface::class, static fn() => null);
147+
148+
$factoryFactory = new ProblemDetailsResponseFactoryFactory();
149+
$factory = $factoryFactory($this->container);
150+
$isDebug = (new ReflectionObject($factory))->getProperty('isDebug');
151+
$exceptionDetailsInResponse = (new ReflectionObject($factory))->getProperty('exceptionDetailsInResponse');
152+
153+
self::assertSame(ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS, $isDebug->getValue($factory));
154+
self::assertSame(true, $exceptionDetailsInResponse->getValue($factory));
155+
}
135156
}

0 commit comments

Comments
 (0)