Skip to content

Commit d97dde6

Browse files
committed
Fix for strip responses option
1 parent 1e014f1 commit d97dde6

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/OpenApiValidation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ public function validateResponseBody(ResponseInterface &$response, string $path,
204204
if (null === $responseBodyData && $responseSchema) {
205205
return [['name' => 'responseBody', 'code' => 'error_required']];
206206
}
207+
if ($this->options['stripResponse']) {
208+
$responseSchema = JsonHelper::additionalProperties($responseSchema, false);
209+
}
207210
$errors = $this->validateObject($responseSchema, json_encode($responseBodyData, JSON_PRESERVE_ZERO_FRACTION));
208211
if ($this->options['stripResponse']) {
209212
$notAdditionalOrNullErrors = [];

src/OpenApiValidation/Helpers/Json.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ public static function remove(array $json, array $path) : array
3030
}
3131
return $json;
3232
}
33+
public static function additionalProperties(array $json, bool $allowAdditionalProperties) {
34+
if (isset($json['properties'])) {
35+
$json['additionalProperties'] = $allowAdditionalProperties;
36+
}
37+
foreach ($json as $key => &$value) {
38+
if (is_array($value)) {
39+
$value = self::additionalProperties($value,$allowAdditionalProperties);
40+
}
41+
}
42+
return $json;
43+
}
3344
}

tests/JsonHelperTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Validation Middleware.
5+
*
6+
* @see https://github.com/hkarlstrom/openapi-validation-middleware
7+
*
8+
* @copyright Copyright (c) 2018 Henrik Karlström
9+
* @license MIT
10+
*/
11+
12+
namespace HKarlstrom\Middleware\OpenApiValidation;
13+
14+
use PHPUnit\Framework\TestCase;
15+
16+
class JsonHelperTest extends TestCase
17+
{
18+
public function testAdditionalProperties()
19+
{
20+
$json = [
21+
'type'=>'object',
22+
'properties' => [
23+
'foo' => [
24+
'type' => 'object',
25+
'properties' => [
26+
'bar' => [
27+
'type' => 'number'
28+
]
29+
]
30+
]
31+
]
32+
];
33+
$json = Helpers\Json::additionalProperties($json,false);
34+
$this->assertArrayHasKey('additionalProperties',$json);
35+
$this->assertFalse($json['additionalProperties']);
36+
$this->assertArrayHasKey('additionalProperties',$json['properties']['foo']);
37+
$this->assertFalse($json['properties']['foo']['additionalProperties']);
38+
}
39+
}

tests/ResponsesTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function testStrip()
6969
$this->assertSame(200, $response->getStatusCode());
7070
$this->assertSame('test', $json['foo']);
7171
$this->assertSame(100, $json['bar']);
72+
$this->assertFalse(isset($json['extra']));
7273
}
7374

7475
public function testResponseEmpty()

0 commit comments

Comments
 (0)