-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from slackhq/fix-cyclic-ref
add hackIgnoreRefs option for preventing circular references
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?hh // strict | ||
|
||
namespace Slack\Hack\JsonSchema\Tests; | ||
|
||
use namespace HH\Lib\C; | ||
use function Facebook\FBExpect\expect; | ||
|
||
use type Slack\Hack\JsonSchema\Tests\Generated\{IgnoreRefsValidator}; | ||
|
||
final class IgnoreRefsValidatorTest extends BaseCodegenTestCase { | ||
|
||
<<__Override>> | ||
public static async function beforeFirstTestAsync(): Awaitable<void> { | ||
$ret = self::getBuilder('ignore-refs.json', 'IgnoreRefsValidator'); | ||
$ret['codegen']->build(); | ||
require_once($ret['path']); | ||
} | ||
|
||
public function testInvalidEmptyInput(): void { | ||
$validator = new IgnoreRefsValidator(dict['randomprop' => 'IanIzzy']); | ||
$validator->validate(); | ||
expect($validator->isValid())->toBeTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?hh // strict | ||
/** | ||
* This file is generated. Do not modify it manually! | ||
* | ||
* To re-generate this file run `make test` | ||
* | ||
* | ||
* @generated SignedSource<<f57a9080fda14e4ed995266ea7ae4d40>> | ||
*/ | ||
namespace Slack\Hack\JsonSchema\Tests\Generated; | ||
use namespace Slack\Hack\JsonSchema; | ||
use namespace Slack\Hack\JsonSchema\Constraints; | ||
|
||
type TIgnoreRefsValidatorPropertiesRandomprop = mixed; | ||
|
||
type TIgnoreRefsValidator = shape( | ||
?'randomprop' => TIgnoreRefsValidatorPropertiesRandomprop, | ||
... | ||
); | ||
|
||
final class IgnoreRefsValidatorPropertiesRandomprop { | ||
|
||
public static function check( | ||
mixed $input, | ||
string $_pointer, | ||
): TIgnoreRefsValidatorPropertiesRandomprop { | ||
return $input; | ||
} | ||
} | ||
|
||
final class IgnoreRefsValidator | ||
extends JsonSchema\BaseValidator<TIgnoreRefsValidator> { | ||
|
||
private static bool $coerce = false; | ||
|
||
public static function check( | ||
mixed $input, | ||
string $pointer, | ||
): TIgnoreRefsValidator { | ||
$typed = Constraints\ObjectConstraint::check($input, $pointer, self::$coerce); | ||
|
||
$errors = vec[]; | ||
$output = shape(); | ||
|
||
/*HHAST_IGNORE_ERROR[UnusedVariable] Some loops generated with this statement do not use their $value*/ | ||
foreach ($typed as $key => $value) { | ||
/* HH_IGNORE_ERROR[4051] allow dynamic access to preserve input. See comment in the codegen lib for reasoning and alternatives if needed. */ | ||
$output[$key] = $value; | ||
} | ||
|
||
if (\HH\Lib\C\contains_key($typed, 'randomprop')) { | ||
try { | ||
$output['randomprop'] = IgnoreRefsValidatorPropertiesRandomprop::check( | ||
$typed['randomprop'], | ||
JsonSchema\get_pointer($pointer, 'randomprop'), | ||
); | ||
} catch (JsonSchema\InvalidFieldException $e) { | ||
$errors = \HH\Lib\Vec\concat($errors, $e->errors); | ||
} | ||
} | ||
|
||
if (\HH\Lib\C\count($errors)) { | ||
throw new JsonSchema\InvalidFieldException($pointer, $errors); | ||
} | ||
|
||
/* HH_IGNORE_ERROR[4163] */ | ||
return $output; | ||
} | ||
|
||
<<__Override>> | ||
protected function process(): TIgnoreRefsValidator { | ||
return self::check($this->input, $this->pointer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"randomprop": { | ||
"$ref": "#/definitions/objectRef", | ||
"hackIgnoreRef": true | ||
} | ||
}, | ||
"definitions": { | ||
"otherRef": { | ||
"$ref": "#/definitions/anotherRef" | ||
}, | ||
"anotherRef": { | ||
"$ref": "#/definitions/otherRef" | ||
}, | ||
"objectRef": { | ||
"type": "object", | ||
"properties": { | ||
"thing": { | ||
"$ref": "#/definitions/objectRef2" | ||
} | ||
|
||
} | ||
}, | ||
"objectRef2": { | ||
"$ref": "#/definitions/objectRef" | ||
} | ||
} | ||
} |