From 04fafdbc2c7b8da96a6714f7e848a76d99f8f429 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Tue, 9 Jun 2020 14:48:33 +0800 Subject: [PATCH 1/7] Typehint for ApiInterface --- src/ApiInterface.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ApiInterface.php b/src/ApiInterface.php index dfc22e3..cc62577 100644 --- a/src/ApiInterface.php +++ b/src/ApiInterface.php @@ -9,11 +9,17 @@ */ interface ApiInterface { + /** + * @return mixed + */ public static function parameters(); + /** + * @return mixed + */ public static function requestBody(); - public static function responses(); + public static function responses(): array; - public static function permissions(); + public static function permissions(): array; } From 65213a2bf07de95a64beb5efcc11e7361b2ee82c Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Sat, 18 Jul 2020 16:44:22 +0800 Subject: [PATCH 2/7] Update to generate OpenAPI 3.1 docs --- src/TypeParser.php | 15 +-- tests/TypeTest.php | 251 +-------------------------------------------- 2 files changed, 8 insertions(+), 258 deletions(-) diff --git a/src/TypeParser.php b/src/TypeParser.php index c0184c6..badeccc 100644 --- a/src/TypeParser.php +++ b/src/TypeParser.php @@ -148,9 +148,7 @@ protected function parseInputField($definition) 'in' => $fetcher, 'schema' => $schema, ]; - if ($required) { - $result['required'] = $required; - } + $result['required'] = $required; return $result; } @@ -236,17 +234,12 @@ protected function parseEnum(string $definition) protected function makeNullableSchema(array $schema, $nullable) { - if ($this->mode & self::MODE_OPEN_API) { - // OpenAPI specficition does not support this, just ingore the nullable setting. - return $schema; - } - if (! $nullable) { return $schema; } return [ - 'anyOf' => [ + 'oneOf' => [ ['type' => 'null'], $schema, ], @@ -264,10 +257,8 @@ protected function parseScalar($definition) $typeClass = $this->getValidTypeClass($definition); $schema = $typeClass::toArray(); - if (($this->mode & self::MODE_JSON_SCHEMA) && $nullable) { + if ($nullable) { $schema['type'] = [$schema['type'], 'null']; - } elseif (($this->mode & self::MODE_OPEN_API) && $nullable) { - $schema['nullable'] = $nullable; } return $schema; diff --git a/tests/TypeTest.php b/tests/TypeTest.php index 95ea84c..1d6b86a 100644 --- a/tests/TypeTest.php +++ b/tests/TypeTest.php @@ -28,7 +28,6 @@ public function typeToArrayCases() 'type' => 'object', 'example' => ['id' => 1, 'name' => 'INFO'], ], - null, ], [ Map002Type::class, @@ -39,7 +38,6 @@ public function typeToArrayCases() ], 'example' => ['优' => 90, '良' => 80, '中' => 60], ], - null, ], [ Map003Type::class, @@ -54,14 +52,12 @@ public function typeToArrayCases() ], ], ], - null, ], [ 'string', [ 'type' => 'string', ], - null, ], [ ['string'], @@ -71,7 +67,6 @@ public function typeToArrayCases() 'type' => 'string', ], ], - null, ], [ @@ -79,10 +74,6 @@ public function typeToArrayCases() [ 'type' => ['string', 'null'], ], - [ - 'type' => 'string', - 'nullable' => true, - ], ], [ @@ -93,13 +84,6 @@ public function typeToArrayCases() 'type' => ['string', 'null'], ], ], - [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], ], [ @@ -111,13 +95,6 @@ public function typeToArrayCases() 'bar', ], ], - [ - 'type' => 'string', - 'enum' => [ - 'foo', - 'bar', - ], - ], ], [ @@ -142,27 +119,6 @@ public function typeToArrayCases() ], 'required' => ['id', 'file'], ], - [ - 'type' => 'object', - 'properties' => [ - 'id' => ['type' => 'integer'], - 'name' => ['type' => 'string'], - 'is_admin' => ['type' => 'boolean'], - 'file' => ['type' => 'string', 'format' => 'binary',], - 'nullable_field' => ['type' => 'string', 'nullable' => true], - 'date' => [ - 'type' => 'string', - 'format' => 'date', - 'nullable' => true, - 'pattern' => '^\d{4}-\d{2}-\d{2}$', - ], - 'time' => [ - 'type' => 'string', - 'pattern' => '^\d{2}:\d{2}:\d{2}$', - ], - ], - 'required' => ['id', 'file'], - ], ], [ @@ -184,24 +140,6 @@ public function typeToArrayCases() ], ], ], - [ - 'type' => 'object', - 'properties' => [ - 'field1' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - ], - ], - 'field2' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], - ], - ], ], [ Product005Type::class, @@ -231,33 +169,6 @@ public function typeToArrayCases() ], ], ], - [ - 'required' => ['related1'], - 'type' => 'object', - 'properties' => [ - 'related1' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'object', - 'properties' => [ - 'field1' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - ], - ], - 'field2' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], - ], - ], - ], - ], - ], ], [ Product003Type::class, @@ -304,51 +215,6 @@ public function typeToArrayCases() ], ], ], - [ - 'type' => 'object', - 'properties' => [ - 'related1' => [ - 'type' => 'object', - 'properties' => [ - 'id' => ['type' => 'integer'], - 'name' => ['type' => 'string'], - 'is_admin' => ['type' => 'boolean'], - 'file' => ['type' => 'string', 'format' => 'binary',], - 'nullable_field' => ['type' => 'string', 'nullable' => true], - 'date' => [ - 'type' => 'string', - 'format' => 'date', - 'nullable' => true, - 'pattern' => '^\d{4}-\d{2}-\d{2}$', - ], - 'time' => [ - 'type' => 'string', - 'pattern' => '^\d{2}:\d{2}:\d{2}$', - ], - ], - 'required' => ['id', 'file'], - - ], - 'related2' => [ - 'type' => 'object', - 'properties' => [ - 'field1' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - ], - ], - 'field2' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], - ], - ], - ], - ], ], [ @@ -378,7 +244,7 @@ public function typeToArrayCases() ], 'related2' => [ - 'anyOf' => [ + 'oneOf' => [ [ 'type' => 'null', ], @@ -404,52 +270,6 @@ public function typeToArrayCases() ], 'required' => ['related1'], ], - [ - 'type' => 'object', - 'properties' => [ - 'related1' => [ - 'type' => 'object', - 'properties' => [ - 'id' => ['type' => 'integer'], - 'name' => ['type' => 'string'], - 'is_admin' => ['type' => 'boolean'], - 'file' => ['type' => 'string', 'format' => 'binary'], - 'nullable_field' => ['type' => 'string', 'nullable' => true], - 'date' => [ - 'type' => 'string', - 'format' => 'date', - 'nullable' => true, - 'pattern' => '^\d{4}-\d{2}-\d{2}$', - ], - 'time' => [ - 'type' => 'string', - 'pattern' => '^\d{2}:\d{2}:\d{2}$', - ], - ], - 'required' => ['id', 'file'], - - ], - 'related2' => [ - 'type' => 'object', - 'properties' => [ - 'field1' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - ], - ], - 'field2' => [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], - ], - ], - ], - 'required' => ['related1'], - ], ], [ @@ -478,29 +298,7 @@ public function typeToArrayCases() 'schema' => ['type' => 'number'], ], ], - - [ - [ - 'name' => 'limit', - 'in' => 'query', - 'required' => true, - 'schema' => ['type' => 'number'], - ], - [ - 'name' => 'offset', - 'in' => 'query', - 'required' => false, - 'schema' => ['type' => 'number', 'nullable' => true], - ], - [ - 'name' => 'default_in', - 'in' => 'query', - 'required' => false, - 'schema' => ['type' => 'number'], - ], - ], ], - [ [Product001Type::class], [ @@ -537,42 +335,6 @@ public function typeToArrayCases() 'required' => ['id', 'file'], ], ], - [ - 'type' => 'array', - 'items' => [ - 'type' => 'object', - 'properties' => [ - 'id' => [ - 'type' => 'integer', - ], - 'name' => [ - 'type' => 'string', - ], - 'is_admin' => [ - 'type' => 'boolean', - ], - 'file' => [ - 'type' => 'string', - 'format' => 'binary', - ], - 'nullable_field' => [ - 'type' => 'string', - 'nullable' => true, - ], - 'date' => [ - 'type' => 'string', - 'format' => 'date', - 'nullable' => true, - 'pattern' => '^\d{4}-\d{2}-\d{2}$', - ], - 'time' => [ - 'type' => 'string', - 'pattern' => '^\d{2}:\d{2}:\d{2}$', - ], - ], - 'required' => ['id', 'file'], - ], - ], ], ]; } @@ -581,16 +343,14 @@ public function typeToArrayCases() * @dataProvider typeToArrayCases * @param $type * @param $expect1 - * @param $expect2 */ - public function testTypeToArray($type, $expect1, $expect2) + public function testTypeToArray($type, $expect1) { $parser = new TypeParser(TypeParser::MODE_JSON_SCHEMA); - var_dump($parser->parse($type)); $this->assertEquals($expect1, $parser->parse($type)); $parser = new TypeParser(TypeParser::MODE_OPEN_API); - $this->assertEquals($expect2 ?? $expect1, $parser->parse($type)); + $this->assertEquals($expect1, $parser->parse($type)); } public function typeToArrayWithRefCases() @@ -614,8 +374,7 @@ public function typeToArrayWithRefCases() 'field2' => [ 'type' => 'array', 'items' => [ - 'type' => 'string', - 'nullable' => true, + 'type' => ['string', 'null'], ], ], ], @@ -797,7 +556,7 @@ public function dataCases() 'type' => 'object', 'properties' => [ 'foo' => [ - 'anyOf' => [ + 'oneOf' => [ ['type' => 'null'], ], ], From 24281517c0dc3ef00c07285f023617e874097a76 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Tue, 2 Mar 2021 15:17:32 +0800 Subject: [PATCH 3/7] Upgraded phpdocumentor/reflection-docblock --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 99f8872..5fb8891 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "require": { "php": ">=7.0", - "phpdocumentor/reflection-docblock": "^4.3", + "phpdocumentor/reflection-docblock": "^4.3 || ^5.0", "justinrainbow/json-schema": "^5.2" }, "require-dev": { From 265fd6b5500bbeca545583f37f11cdb3432fbb46 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Thu, 1 Apr 2021 18:17:14 +0800 Subject: [PATCH 4/7] Improved InputValidator to convert int to boolean --- src/InputValidator.php | 7 ++- src/constraints/TypeConstraint.php | 21 ++++++++ tests/TypeTest.php | 77 ++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/constraints/TypeConstraint.php diff --git a/src/InputValidator.php b/src/InputValidator.php index 1614dc4..664f121 100644 --- a/src/InputValidator.php +++ b/src/InputValidator.php @@ -3,7 +3,9 @@ namespace rethink\typedphp; use JsonSchema\Constraints\Constraint; +use JsonSchema\Constraints\Factory; use JsonSchema\Validator; +use rethink\typedphp\constraints\TypeConstraint; /** * Class InputValidator @@ -37,8 +39,11 @@ protected function validateInternal($definition, $data, &$result) } $schema = $definition['schema']; + + $factory = new Factory(); + $factory->setConstraintClass('type', TypeConstraint::class); - $validator = new Validator(); + $validator = new Validator($factory); $result = $data[$definition['name']]; diff --git a/src/constraints/TypeConstraint.php b/src/constraints/TypeConstraint.php new file mode 100644 index 0000000..baf8a66 --- /dev/null +++ b/src/constraints/TypeConstraint.php @@ -0,0 +1,21 @@ + ['a' => '1'], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => true], + ], + ], + [ + [ + 'query' => ['a' => '0'], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => false], + ], + ], + [ + [ + 'query' => ['a' => 1], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => true], + ], + ], + [ + [ + 'query' => ['a' => 0], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => false], + ], + ], // missing required input field [ [ From 918bbc8f728d9e15d748e794c1b2c928c792329b Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Thu, 1 Apr 2021 20:28:18 +0800 Subject: [PATCH 5/7] Fixed break string bools --- src/constraints/TypeConstraint.php | 4 ++-- tests/TypeTest.php | 38 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/constraints/TypeConstraint.php b/src/constraints/TypeConstraint.php index baf8a66..45cd833 100644 --- a/src/constraints/TypeConstraint.php +++ b/src/constraints/TypeConstraint.php @@ -10,9 +10,9 @@ class TypeConstraint extends \JsonSchema\Constraints\TypeConstraint { protected function toBoolean($value) { - if ($value == 1) { + if ($value === 1 || $value === '1') { return true; - } elseif ($value == 0) { + } elseif ($value === 0 || $value === '0') { return false; } else { return parent::toBoolean($value); diff --git a/tests/TypeTest.php b/tests/TypeTest.php index 24f82c6..682f6c2 100644 --- a/tests/TypeTest.php +++ b/tests/TypeTest.php @@ -718,6 +718,44 @@ public function inputDataCases() 'query' => ['a' => false], ], ], + [ + [ + 'query' => ['a' => 'true'], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => true], + ], + ], + [ + [ + 'query' => ['a' => 'false'], + ], + [ + [ + 'name' => 'a', + 'in' => 'query', + 'required' => true, + 'schema' => [ + 'type' => 'boolean', + ], + ], + ], + [], + [ + 'query' => ['a' => false], + ], + ], // missing required input field [ [ From 361a3524d259cfddaca1f96e419908d658f4efd2 Mon Sep 17 00:00:00 2001 From: Devon Liu Date: Wed, 21 Dec 2022 15:43:44 +0800 Subject: [PATCH 6/7] Speed up parser by cache parsed result --- src/TypeParser.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/TypeParser.php b/src/TypeParser.php index a745916..65aefae 100644 --- a/src/TypeParser.php +++ b/src/TypeParser.php @@ -314,16 +314,28 @@ protected function parseMap(string $definition): array protected function parseString($definition) { + static $cached = []; $newDefinition = trim($definition, '?'); + + $key = $definition; + if (is_subclass_of($newDefinition, MapType::class)) { + $key = $newDefinition; + } + + if (isset($cached[$key])) { + return $cached[$key]; + } + if (is_subclass_of($newDefinition, ProductType::class)) { - return $this->parseObject($definition); + $cached[$key] = $this->parseObject($definition); } elseif (is_subclass_of($newDefinition, SumType::class)) { - return $this->parseEnum($definition); + $cached[$key]= $this->parseEnum($definition); } elseif (is_subclass_of($newDefinition, MapType::class)) { - return $this->parseMap($newDefinition); + $cached[$key] = $this->parseMap($newDefinition); } else { - return $this->parseScalar($definition); + $cached[$key] = $this->parseScalar($definition); } + return $cached[$key]; } /** From 96d3d44e65b14a9ae6e0d9aa32d86c0cee679ce5 Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Tue, 21 Mar 2023 12:21:33 +0800 Subject: [PATCH 7/7] Added union type --- src/TypeParser.php | 29 +++++++++- src/types/ProductType.php | 4 +- src/types/UnionType.php | 24 ++++++++ tests/TypeTest.php | 114 +++++++++++++++++++++++++++++++++++++- 4 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 src/types/UnionType.php diff --git a/src/TypeParser.php b/src/TypeParser.php index 65aefae..2959488 100644 --- a/src/TypeParser.php +++ b/src/TypeParser.php @@ -18,6 +18,7 @@ use rethink\typedphp\types\TimestampType; use rethink\typedphp\types\TimeType; use rethink\typedphp\types\Type; +use rethink\typedphp\types\UnionType; /** * Class TypeParser @@ -312,6 +313,30 @@ protected function parseMap(string $definition): array return $this->makeNullableSchema($schema, $nullable); } + protected function parseUnion(string $definition): array + { + $nullable = false; + if ($this->isNullable($definition)) { + $nullable = true; + $definition = trim($definition, '?'); + } + + assert(is_subclass_of($definition, UnionType::class)); + + $types = []; + foreach ($definition::allowedTypes() as $allowedType) { + $types[] = $this->parse($allowedType); + } + + if ($nullable) { + $types[] = ['type' => 'null']; + } + + return [ + 'oneOf' => $types, + ]; + } + protected function parseString($definition) { static $cached = []; @@ -331,7 +356,9 @@ protected function parseString($definition) } elseif (is_subclass_of($newDefinition, SumType::class)) { $cached[$key]= $this->parseEnum($definition); } elseif (is_subclass_of($newDefinition, MapType::class)) { - $cached[$key] = $this->parseMap($newDefinition); + $cached[$key] = $this->parseMap($definition); + } elseif (is_subclass_of($newDefinition, UnionType::class)) { + $cached[$key] = $this->parseUnion($definition); } else { $cached[$key] = $this->parseScalar($definition); } diff --git a/src/types/ProductType.php b/src/types/ProductType.php index e5b8f1c..6de5ea6 100644 --- a/src/types/ProductType.php +++ b/src/types/ProductType.php @@ -22,8 +22,6 @@ public static function name() public static function toArray() { - $parser = new TypeParser(static::class); - - return $parser->parse(); + throw new \Exception('Should not be called'); } } diff --git a/src/types/UnionType.php b/src/types/UnionType.php new file mode 100644 index 0000000..97e6802 --- /dev/null +++ b/src/types/UnionType.php @@ -0,0 +1,24 @@ + 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + 'name' => ['type' => 'string'], + 'is_admin' => ['type' => 'boolean'], + 'file' => ['type' => 'string', 'format' => 'binary',], + 'nullable_field' => ['type' => ['string', 'null']], + 'date' => [ + 'type' => ['string', 'null'], + 'format' => 'date', + 'pattern' => '^\d{4}-\d{2}-\d{2}$', + ], + 'time' => [ + 'type' => 'string', + 'pattern' => '^\d{2}:\d{2}:\d{2}$', + ], + ], + 'required' => ['id', 'file'], + ]; + } + + protected function product001SchemaWithNull(): array + { + return [ + 'type' => 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + 'name' => ['type' => 'string'], + 'is_admin' => ['type' => 'boolean'], + 'file' => ['type' => 'string', 'format' => 'binary',], + 'nullable_field' => ['type' => 'string', 'nullable' => true], + 'date' => [ + 'type' => 'string', + 'format' => 'date', + 'nullable' => true, + 'pattern' => '^\d{4}-\d{2}-\d{2}$', + ], + 'time' => [ + 'type' => 'string', + 'pattern' => '^\d{2}:\d{2}:\d{2}$', + ], + ], + 'required' => ['id', 'file'], + ]; + } + public function typeToArrayCases() { + $product001Schema = $this->product001Schema(); + + $product001SchemaWithNull = $this->product001SchemaWithNull(); + return [ [ Map001Type::class, @@ -574,6 +629,31 @@ public function typeToArrayCases() ], ], ], + [ + Union001Type::class, + [ + 'oneOf' => [ + [ + 'type' => 'string', + ], + [ + 'type' => 'integer', + ], + $product001Schema, + ], + ], + [ + 'oneOf' => [ + [ + 'type' => 'string', + ], + [ + 'type' => 'integer', + ], + $product001SchemaWithNull, + ], + ] + ], ]; } @@ -624,6 +704,28 @@ public function typeToArrayWithRefCases() ], ], ], + [ + Union001Type::class . '?', + [ + 'oneOf' => [ + [ + 'type' => 'string', + ], + [ + 'type' => 'integer', + ], + [ + '$ref' => '#/components/schemas/Product001', + ], + [ + 'type' => 'null', + ], + ], + ], + [ + 'Product001' => $this->product001SchemaWithNull(), + ] + ], ]; } @@ -1034,7 +1136,6 @@ class Dict003ItemType extends ProductType class Map003Type extends MapType { - public static function valueType(): string { return Dict003ItemType::class; @@ -1044,5 +1145,16 @@ public static function example(): array { return []; } +} +class Union001Type extends UnionType +{ + public static function allowedTypes(): array + { + return [ + 'string', + 'integer', + Product001Type::class, + ]; + } }