diff --git a/src/Codegen/Constraints/UntypedBuilder.php b/src/Codegen/Constraints/UntypedBuilder.php index 72ec3f7..e083b81 100644 --- a/src/Codegen/Constraints/UntypedBuilder.php +++ b/src/Codegen/Constraints/UntypedBuilder.php @@ -430,11 +430,10 @@ private function generateOptimizedAnyOfChecks(TOptimizedAnyOfTypes $any_of_types ) ->ensureEmptyLine() ->addMultilineCall('Constraints\ObjectRequiredConstraint::check', vec['$typed', 'keyset[$key]', '$pointer']) - ->addAssignment('$type_name', '($typed[$key] ?? null) as nonnull', HackBuilderValues::literal()) ->addAssignment('$field_pointer', 'JsonSchema\get_pointer($pointer, $key)', HackBuilderValues::literal()) ->addAssignment( '$type_name', - 'Constraints\StringConstraint::check($type_name, $field_pointer, false)', + 'Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false)', HackBuilderValues::literal(), ) ->ensureEmptyLine() diff --git a/tests/UntypedSchemaValidatorTest.php b/tests/UntypedSchemaValidatorTest.php index 09ab78b..50ea1c8 100644 --- a/tests/UntypedSchemaValidatorTest.php +++ b/tests/UntypedSchemaValidatorTest.php @@ -163,4 +163,23 @@ public function testAnyOfOptimizedEnumInvalid(): void { expect($validator->isValid())->toBeFalse(); } + public function testAnyOfOptimizedEnumInvalidType(): void { + $input = dict[ + 'any_of_optimized_enum' => dict[ + 'type' => null, + 'integer' => 3, + ], + ]; + + $validator = new UntypedSchemaValidator($input); + $validator->validate(); + expect($validator->isValid())->toBeFalse(); + $errors = $validator->getErrors(); + expect(C\count($errors))->toBeSame(1); + $error = $errors[0]; + expect($error['code'])->toBeSame('invalid_type'); + expect($error['message'])->toBeSame('must provide a string'); + expect($error['pointer'] ?? null)->toBeSame('/any_of_optimized_enum/type'); + } + } diff --git a/tests/examples/codegen/CustomCodegenConfigValidator.php b/tests/examples/codegen/CustomCodegenConfigValidator.php index 96438d0..35285cd 100644 --- a/tests/examples/codegen/CustomCodegenConfigValidator.php +++ b/tests/examples/codegen/CustomCodegenConfigValidator.php @@ -5,7 +5,7 @@ * To re-generate this file run `make test` * * - * @generated SignedSource<<359265246c578753e092aaac23c09040>> + * @generated SignedSource<> */ namespace Slack\Hack\JsonSchema\Tests\Generated; use namespace Slack\Hack\JsonSchema; @@ -649,10 +649,9 @@ public static function check( $typed = Constraints\ObjectConstraint::check($input, $pointer, false); Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer); - $type_name = ($typed[$key] ?? null) as nonnull; $field_pointer = JsonSchema\get_pointer($pointer, $key); $type_name = - Constraints\StringConstraint::check($type_name, $field_pointer, false); + Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false); $types = dict[ 'phone' => diff --git a/tests/examples/codegen/PersonSchemaValidator.php b/tests/examples/codegen/PersonSchemaValidator.php index ae3aa9f..c75f99f 100644 --- a/tests/examples/codegen/PersonSchemaValidator.php +++ b/tests/examples/codegen/PersonSchemaValidator.php @@ -5,7 +5,7 @@ * To re-generate this file run `make test` * * - * @generated SignedSource<<386e40d994e41438a6cf7c899b5da3c1>> + * @generated SignedSource<<2ab2503832dc94e7486ad2834e8e40a5>> */ namespace Slack\Hack\JsonSchema\Tests\Generated; use namespace Slack\Hack\JsonSchema; @@ -649,10 +649,9 @@ public static function check( $typed = Constraints\ObjectConstraint::check($input, $pointer, false); Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer); - $type_name = ($typed[$key] ?? null) as nonnull; $field_pointer = JsonSchema\get_pointer($pointer, $key); $type_name = - Constraints\StringConstraint::check($type_name, $field_pointer, false); + Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false); $types = dict[ 'phone' => diff --git a/tests/examples/codegen/UntypedSchemaValidator.php b/tests/examples/codegen/UntypedSchemaValidator.php index b66314a..2b2ad3e 100644 --- a/tests/examples/codegen/UntypedSchemaValidator.php +++ b/tests/examples/codegen/UntypedSchemaValidator.php @@ -5,7 +5,7 @@ * To re-generate this file run `make test` * * - * @generated SignedSource<<3c498202f74829743c3f3d148c18ac0b>> + * @generated SignedSource<<0f9dda249bf4bf5215072b9e288a0fa5>> */ namespace Slack\Hack\JsonSchema\Tests\Generated; use namespace Slack\Hack\JsonSchema; @@ -965,10 +965,9 @@ public static function check( $typed = Constraints\ObjectConstraint::check($input, $pointer, false); Constraints\ObjectRequiredConstraint::check($typed, keyset[$key], $pointer); - $type_name = ($typed[$key] ?? null) as nonnull; $field_pointer = JsonSchema\get_pointer($pointer, $key); $type_name = - Constraints\StringConstraint::check($type_name, $field_pointer, false); + Constraints\StringConstraint::check($typed[$key] ?? null, $field_pointer, false); $types = dict[ 'first' =>