Skip to content

Commit

Permalink
Merge pull request #34 from slackhq/handle-type-being-null
Browse files Browse the repository at this point in the history
Handle `type` not being a string value like we expect
  • Loading branch information
Michael Hahn authored Feb 10, 2020
2 parents 04b77ea + 43804cf commit 17472de
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/Codegen/Constraints/UntypedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions tests/UntypedSchemaValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
5 changes: 2 additions & 3 deletions tests/examples/codegen/CustomCodegenConfigValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* To re-generate this file run `make test`
*
*
* @generated SignedSource<<359265246c578753e092aaac23c09040>>
* @generated SignedSource<<b091dd7814125f747984f31d8fafd43c>>
*/
namespace Slack\Hack\JsonSchema\Tests\Generated;
use namespace Slack\Hack\JsonSchema;
Expand Down Expand Up @@ -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' =>
Expand Down
5 changes: 2 additions & 3 deletions tests/examples/codegen/PersonSchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' =>
Expand Down
5 changes: 2 additions & 3 deletions tests/examples/codegen/UntypedSchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' =>
Expand Down

0 comments on commit 17472de

Please sign in to comment.