From c39e52bbd97116fa0f72c1129195a83a8dc8b5d4 Mon Sep 17 00:00:00 2001 From: jvdhehvg Date: Wed, 1 Jul 2026 15:27:37 +0200 Subject: [PATCH 1/4] Fixed required check in \Nijens\OpenapiBundle\Validation\RequestValidator\RequestParameterValidator::validateQueryParameter --- .../RequestParameterValidator.php | 2 +- .../RequestParameterValidatorTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Validation/RequestValidator/RequestParameterValidator.php b/src/Validation/RequestValidator/RequestParameterValidator.php index 7615ca6..6b0db9b 100644 --- a/src/Validation/RequestValidator/RequestParameterValidator.php +++ b/src/Validation/RequestValidator/RequestParameterValidator.php @@ -78,7 +78,7 @@ private function getValidateQueryParametersFromRequest(Request $request): array private function validateQueryParameter(Request $request, string $parameterName, stdClass $parameter): array { $violations = []; - if ($request->query->has($parameterName) === false && $parameter->required ?? false) { + if (($parameter->required ?? false) && $request->query->has($parameterName) === false) { $violations[] = new Violation( 'required_query_parameter', sprintf('Query parameter %s is required.', $parameterName), diff --git a/tests/Validation/RequestValidator/RequestParameterValidatorTest.php b/tests/Validation/RequestValidator/RequestParameterValidatorTest.php index 1997bcf..cbf739a 100644 --- a/tests/Validation/RequestValidator/RequestParameterValidatorTest.php +++ b/tests/Validation/RequestValidator/RequestParameterValidatorTest.php @@ -91,6 +91,29 @@ public function testCannotValidateRequiredRequestParameterWithoutValue(): void ); } + public function testCanValidateOptionalRequestParameterWithoutRequiredFlag(): void + { + $request = new Request(); + $request->attributes->set( + RouteContext::REQUEST_ATTRIBUTE, + [ + RouteContext::REQUEST_VALIDATE_QUERY_PARAMETERS => [ + 'foo' => json_encode([ + 'name' => 'foo', + 'in' => 'query', + 'schema' => [ + 'type' => 'string', + ], + ]), + ], + ] + ); + + static::assertNull( + $this->validator->validate($request) + ); + } + public function testCanValidateRequestParameterOfTypeBoolean(): void { $request = new Request(); From e5016ecb9aaa80a5232e3ff3a018cb4a5447670b Mon Sep 17 00:00:00 2001 From: jvdhehvg Date: Wed, 1 Jul 2026 15:43:05 +0200 Subject: [PATCH 2/4] Normalize booleanish query parameters in RequestParameterValidator --- .../RequestParameterValidator.php | 24 +++++++++++++++++- .../RequestParameterValidatorTest.php | 25 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Validation/RequestValidator/RequestParameterValidator.php b/src/Validation/RequestValidator/RequestParameterValidator.php index 6b0db9b..cd88a2e 100644 --- a/src/Validation/RequestValidator/RequestParameterValidator.php +++ b/src/Validation/RequestValidator/RequestParameterValidator.php @@ -92,7 +92,10 @@ private function validateQueryParameter(Request $request, string $parameterName, return $violations; } - $parameterValue = $request->query->get($parameterName); + $parameterValue = $this->normalizeQueryParameterValue( + $request->query->get($parameterName), + $parameter->schema + ); $this->jsonValidator->validate($parameterValue, $parameter->schema, Constraint::CHECK_MODE_COERCE_TYPES); if ($this->jsonValidator->isValid()) { @@ -111,4 +114,23 @@ function (array $validationError) use ($parameterName): Violation { $validationErrors ); } + + /** + * Normalizes query parameter values that Symfony exposes as strings. + * + * Boolean query parameters are commonly passed as `1`/`0` in URLs, but + * JsonSchema expects native booleans for a boolean schema. + */ + private function normalizeQueryParameterValue(mixed $parameterValue, stdClass $schema): mixed + { + if (($schema->type ?? null) !== 'boolean' || is_string($parameterValue) === false) { + return $parameterValue; + } + + return match (strtolower($parameterValue)) { + '1', 'true' => true, + '0', 'false' => false, + default => $parameterValue, + }; + } } diff --git a/tests/Validation/RequestValidator/RequestParameterValidatorTest.php b/tests/Validation/RequestValidator/RequestParameterValidatorTest.php index cbf739a..e1ed870 100644 --- a/tests/Validation/RequestValidator/RequestParameterValidatorTest.php +++ b/tests/Validation/RequestValidator/RequestParameterValidatorTest.php @@ -139,6 +139,31 @@ public function testCanValidateRequestParameterOfTypeBoolean(): void ); } + public function testCanValidateRequestParameterOfTypeBooleanWithOne(): void + { + $request = new Request(); + $request->query->set('foo', '1'); + $request->attributes->set( + RouteContext::REQUEST_ATTRIBUTE, + [ + RouteContext::REQUEST_VALIDATE_QUERY_PARAMETERS => [ + 'foo' => json_encode([ + 'name' => 'foo', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ]), + ], + ] + ); + + static::assertNull( + $this->validator->validate($request) + ); + } + public function testCanValidateRequestParameterOfTypeString(): void { $request = new Request(); From 5d0ecdc212f9f6da72100fc6b84243fc3dfe8d78 Mon Sep 17 00:00:00 2001 From: Jasper van der Hoeven <104764482+jvdhehvg@users.noreply.github.com> Date: Thu, 2 Jul 2026 15:53:59 +0200 Subject: [PATCH 3/4] Apply suggestion from @niels-nijens Co-authored-by: Niels Nijens --- .../RequestValidator/RequestParameterValidator.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Validation/RequestValidator/RequestParameterValidator.php b/src/Validation/RequestValidator/RequestParameterValidator.php index cd88a2e..493e3bc 100644 --- a/src/Validation/RequestValidator/RequestParameterValidator.php +++ b/src/Validation/RequestValidator/RequestParameterValidator.php @@ -92,10 +92,10 @@ private function validateQueryParameter(Request $request, string $parameterName, return $violations; } - $parameterValue = $this->normalizeQueryParameterValue( - $request->query->get($parameterName), - $parameter->schema - ); + $parameterValue = $request->query->get($parameterName); + if ($schema->type === 'boolean') { + $parameterValue = $request->query->getBoolean($parameterName, $schema->default ?? false); + } $this->jsonValidator->validate($parameterValue, $parameter->schema, Constraint::CHECK_MODE_COERCE_TYPES); if ($this->jsonValidator->isValid()) { From bf96b67a4cc7f43dda7f96619ec2c293ed532696 Mon Sep 17 00:00:00 2001 From: jvdhehvg Date: Thu, 2 Jul 2026 15:57:46 +0200 Subject: [PATCH 4/4] Fixed variable name --- .../RequestParameterValidator.php | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/Validation/RequestValidator/RequestParameterValidator.php b/src/Validation/RequestValidator/RequestParameterValidator.php index 493e3bc..2501772 100644 --- a/src/Validation/RequestValidator/RequestParameterValidator.php +++ b/src/Validation/RequestValidator/RequestParameterValidator.php @@ -93,8 +93,8 @@ private function validateQueryParameter(Request $request, string $parameterName, } $parameterValue = $request->query->get($parameterName); - if ($schema->type === 'boolean') { - $parameterValue = $request->query->getBoolean($parameterName, $schema->default ?? false); + if ($parameter->schema->type === 'boolean') { + $parameterValue = $request->query->getBoolean($parameterName, $parameter->schema->default ?? false); } $this->jsonValidator->validate($parameterValue, $parameter->schema, Constraint::CHECK_MODE_COERCE_TYPES); @@ -114,23 +114,4 @@ function (array $validationError) use ($parameterName): Violation { $validationErrors ); } - - /** - * Normalizes query parameter values that Symfony exposes as strings. - * - * Boolean query parameters are commonly passed as `1`/`0` in URLs, but - * JsonSchema expects native booleans for a boolean schema. - */ - private function normalizeQueryParameterValue(mixed $parameterValue, stdClass $schema): mixed - { - if (($schema->type ?? null) !== 'boolean' || is_string($parameterValue) === false) { - return $parameterValue; - } - - return match (strtolower($parameterValue)) { - '1', 'true' => true, - '0', 'false' => false, - default => $parameterValue, - }; - } }