diff --git a/.gitignore b/.gitignore index d1502b0..ff7f293 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ composer.lock +.idea/ diff --git a/composer.json b/composer.json index 99f8872..19cab58 100644 --- a/composer.json +++ b/composer.json @@ -9,12 +9,12 @@ } ], "require": { - "php": ">=7.0", - "phpdocumentor/reflection-docblock": "^4.3", + "php": ">=7.4", + "phpdocumentor/reflection-docblock": "^4.3 || ^5.0", "justinrainbow/json-schema": "^5.2" }, "require-dev": { - "phpunit/phpunit": "^7.5" + "phpunit/phpunit": "^9.6" }, "autoload": { "psr-4": { 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; } diff --git a/src/DocGenerator.php b/src/DocGenerator.php index c10610f..01d0904 100644 --- a/src/DocGenerator.php +++ b/src/DocGenerator.php @@ -2,10 +2,12 @@ namespace rethink\typedphp; -use ReflectionClass; use InvalidArgumentException; -use phpDocumentor\Reflection\DocBlockFactory; use phpDocumentor\Reflection\DocBlock; +use phpDocumentor\Reflection\DocBlockFactory; +use ReflectionClass; +use rethink\typedphp\security\ScopeInterface; +use const PREG_SPLIT_NO_EMPTY; /** * Class DocGenerator @@ -63,6 +65,7 @@ public function buildApiObject($apiClass) 'operationId' => $this->getStaticProperty($class, 'op'), 'parameters' => $parameters ? $this->parser->parse($parameters) : [], 'responses' => (object)$this->buildResponses($apiClass, $class), + 'security' => $this->buildSecurity($apiClass), ]; if ($bodyDefinition = $this->buildRequestBody($apiClass, $class)) { @@ -120,17 +123,75 @@ private function parseContentType(\ReflectionClass $class) $docblock = DocBlockFactory::createInstance()->create($comment); $tags = $docblock->getTagsByName('content-type'); if (count($tags)) { - return trim((string) $tags[0]->getDescription()); + return trim((string)$tags[0]->getDescription()); } } return null; } + /** + * @param $apiClass + * @return array + * @link https://spec.openapis.org/oas/v3.0.1#security-requirement-object + */ + protected function buildSecurity($apiClass): array + { + if (method_exists($apiClass, 'scopes') === false) { + return []; + } + + $scopes = $apiClass::scopes(); + + // supported format: + // 1. Scope + // 2. [Scope1, Scope2] AND relation + // 3. [[Scope1, Scope2], [Scope3, Scope4]] OR relation + + // only 1 scope + if ($scopes instanceof ScopeInterface) { + return [ + [ + $scopes->schemeName() => [ + $scopes->name(), + ], + ] + ]; + } + + if (is_array($scopes)) { + // multiple scopes with AND relation + if (current($scopes) instanceof ScopeInterface) { + return [ + $this->buildForAndScopes(...$scopes) + ]; + } + // multiple scopes with OR relation + if (is_array(current($scopes))) { + $security = []; + foreach ($scopes as $scope) { + $security[] = $this->buildForAndScopes(...$scope); + } + return $security; + } + } + + throw new InvalidArgumentException('Invalid scopes definition.'); + } + + protected function buildForAndScopes(ScopeInterface ...$scopes): array + { + $result = []; + foreach ($scopes as $scope) { + $result[$scope->schemeName()][] = $scope->name(); + } + return $result; + } + protected function buildResponses($apiClass, \ReflectionClass $class) { $responses = []; - foreach ($apiClass::responses() as $code => $responseDefinition) { + foreach ($apiClass::responses() as $code => $responseDefinition) { if ($responseDefinition !== null) { $responses[$code] = [ @@ -186,6 +247,58 @@ public function generate() return [ 'paths' => (object)$this->buildPathsObject(), 'schemas' => (object)$this->parser->getSchemas(), + 'securitySchemes' => (object)$this->buildSecuritySchemes(), ]; } + + /** + * @return array + * @link https://spec.openapis.org/oas/v3.0.1#security-scheme-object + */ + protected function buildSecuritySchemes(): array + { + $scopes = []; + foreach ($this->apiClasses as $apiClass) { + if (method_exists($apiClass, 'scopes')) { + $scopes[] = $this->collectScopes($apiClass::scopes()); + } + } + $scopes = array_merge([], ...$scopes); + + $securitySchemes = []; + /** @var ScopeInterface $scope */ + foreach ($scopes as $scope) { + if (isset($securitySchemes[$scope->schemeName()])) { + $securitySchemes[$scope->schemeName()]['flows']['authorizationCode']['scopes'][$scope->name()] = $scope->description(); + } else { + $securitySchemes[$scope->schemeName()] = [ + 'type' => 'oauth2', + 'flows' => [ + 'clientCredentials' => [ + 'tokenUrl' => '', + 'scopes' => [ + $scope->name() => $scope->description(), + ], + ], + ], + ]; + } + } + return $securitySchemes; + } + + protected function collectScopes($scopes): array + { + if ($scopes instanceof ScopeInterface) { + return [$scopes]; + } + if (is_array($scopes)) { + $result = []; + foreach ($scopes as $scope) { + $result[] = $this->collectScopes($scope); + } + return array_merge([], ...$result); + } + throw new InvalidArgumentException('Invalid scopes definition.'); + } } diff --git a/src/InputValidator.php b/src/InputValidator.php index 1614dc4..8b8b916 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 @@ -30,15 +32,18 @@ protected function fetchData($fetcher) protected function validateInternal($definition, $data, &$result) { if (($definition['required'] ?? false) && !array_key_exists($definition['name'], $data)) { - $this->errors[] = "The required ${definition['in']} parameter: '${definition['name']}' is required"; + $this->errors[] = "The required {$definition['in']} parameter: '{$definition['name']}' is required"; return false; } else if (!array_key_exists($definition['name'], $data)) { return false; } $schema = $definition['schema']; + + $factory = new Factory(); + $factory->setConstraintClass('type', TypeConstraint::class); - $validator = new Validator(); + $validator = new Validator($factory); $result = $data[$definition['name']]; @@ -46,7 +51,7 @@ protected function validateInternal($definition, $data, &$result) if (!$validator->isValid()) { foreach ($validator->getErrors() as $error) { - $this->errors[] = "The type of ${definition['in']} parameter \"${definition['name']}\" is invalid, " . lcfirst($error['message']); + $this->errors[] = "The type of {$definition['in']} parameter \"{$definition['name']}\" is invalid, " . lcfirst($error['message']); } return false; } diff --git a/src/TypeParser.php b/src/TypeParser.php index a745916..1bac709 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 @@ -36,6 +37,8 @@ class TypeParser protected $schemas = []; + protected $object_chains = []; + public function __construct($mode) { $this->mode = $mode; @@ -171,47 +174,86 @@ protected function parseObject($definition) $definitionName = $definition::name(); + $isNestedType = in_array($definitionName, $this->object_chains); + if (($this->mode & self::MODE_REF_SCHEMA) && isset($this->schemas[$definition])) { return $this->makeNullableSchema([ '$ref' => '#/components/schemas/' . $definitionName, ], $nullable); } - $reflection = new \ReflectionClass($definition); - $properties = []; - $requiredFields = []; + $this->object_chains[] = $definitionName; - foreach ($reflection->getStaticProperties() as $property => $tmpDefinition) { - list($required, $schema) = $this->parseField($tmpDefinition); - $comment = $reflection->getProperty($property)->getDocComment(); - if ($comment) { - $docblock = DocBlockFactory::createInstance()->create($comment); - $schema['title'] = trim($docblock->getSummary()); - $schema['description'] = trim($docblock->getDescription()->render()); + if ($isNestedType) { + $result = $this->makeNullableSchema([ + '$ref' => '#/components/schemas/' . $definitionName, + ], $nullable); + } else { + $result = $this->parseObjectSchema($definitionName, $definition, $nullable); + } + + array_pop($this->object_chains); + + return $result; + } + + protected function parseObjectSchema(string $name, string $definition, bool $nullable): array + { + if (!isset($this->schemas[$name])) { + $reflection = new \ReflectionClass($definition); + $properties = []; + $requiredFields = []; + + foreach ($reflection->getStaticProperties() as $property => $tmpDefinition) { + list($required, $schema) = $this->parseField($tmpDefinition); + $comment = $reflection->getProperty($property)->getDocComment(); + if ($comment) { + $docblock = DocBlockFactory::createInstance()->create($comment); + $title = trim($docblock->getSummary()); + if ($title) { + $schema['title'] = $title; + } + $description = trim($docblock->getDescription()->render()); + if ($description) { + $schema['description'] = $description; + } + + $tag = $docblock->getTagsByName('enum')[0] ?? null; + if ($tag) { + $schema['type'] = 'string'; + $schema['enum'] = preg_split("/\s*[,]\s*/u", $tag->getDescription()->render(), -1, PREG_SPLIT_NO_EMPTY); + unset($schema['$ref']); + } + } + $properties[$property] = $schema; + + if ($required) { + $requiredFields[] = $property; + } } - $properties[$property] = $schema; - if ($required) { - $requiredFields[] = $property; + $schema = [ + 'type' => 'object', + 'properties' => $properties, + ]; + if ($requiredFields) { + $schema['required'] = $requiredFields; } - } - $schema = [ - 'type' => 'object', - 'properties' => $properties, - ]; - if ($requiredFields) { - $schema['required'] = $requiredFields; + $this->schemas[$name] = $schema; + } else { + $schema = $this->schemas[$name]; } - if ($this->mode & self::MODE_REF_SCHEMA) { - $this->schemas[$definitionName] = $schema; - return $this->makeNullableSchema([ - '$ref' => '#/components/schemas/' . $definitionName, + if (($this->mode & self::MODE_REF_SCHEMA)) { + $result = $this->makeNullableSchema([ + '$ref' => '#/components/schemas/' . $name, ], $nullable); } else { - return $this->makeNullableSchema($schema, $nullable); + $result = $this->makeNullableSchema($schema, $nullable); } + + return $result; } protected function parseEnum(string $definition) @@ -240,11 +282,6 @@ protected function isVersion31() protected function makeNullableSchema(array $schema, $nullable) { - if (($this->mode & self::MODE_OPEN_API) && ! $this->isVersion31()) { - // OpenAPI specficition does not support this, just ingore the nullable setting. - return $schema; - } - if (! $nullable) { return $schema; } @@ -268,12 +305,8 @@ protected function parseScalar($definition) $typeClass = $this->getValidTypeClass($definition); $schema = $typeClass::toArray(); - if (($this->mode & self::MODE_JSON_SCHEMA) && $nullable) { - $schema['type'] = [$schema['type'], 'null']; - } elseif (($this->mode & self::MODE_OPEN_API) && $this->isVersion31() && $nullable) { + if ($nullable) { $schema['type'] = [$schema['type'], 'null']; - } elseif (($this->mode & self::MODE_OPEN_API) && $nullable) { - $schema['nullable'] = $nullable; } return $schema; @@ -312,18 +345,69 @@ 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 ($this->mode & self::MODE_REF_SCHEMA) { + $definitionName = $definition::name(); + $this->schemas[$definitionName] = [ + 'oneOf' => $types, + ]; + + return $this->makeNullableSchema([ + '$ref' => '#/components/schemas/' . $definitionName, + ], $nullable); + } + + if ($nullable) { + $types[] = ['type' => 'null']; + } + + return [ + 'oneOf' => $types, + ]; + } + protected function parseString($definition) { + static $cached = []; $newDefinition = trim($definition, '?'); + + $key = $definition; + if (is_subclass_of($newDefinition, MapType::class)) { + $key = $newDefinition; + } + + $key = $this->mode & self::MODE_REF_SCHEMA ? 'ref:' . $key : $key; + + 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($definition); + } elseif (is_subclass_of($newDefinition, UnionType::class)) { + $cached[$key] = $this->parseUnion($definition); } else { - return $this->parseScalar($definition); + $cached[$key] = $this->parseScalar($definition); } + return $cached[$key]; } /** diff --git a/src/TypeValidator.php b/src/TypeValidator.php index cb96cf3..a4a5170 100644 --- a/src/TypeValidator.php +++ b/src/TypeValidator.php @@ -38,7 +38,7 @@ protected function formatError(array $error) if (empty($error['property'])) { return $error['message']; } else { - return "The data of \"${error['property']}\" is invalid, " . lcfirst($error['message']); + return "The data of \"{$error['property']}\" is invalid, " . lcfirst($error['message']); } } diff --git a/src/constraints/TypeConstraint.php b/src/constraints/TypeConstraint.php new file mode 100644 index 0000000..45cd833 --- /dev/null +++ b/src/constraints/TypeConstraint.php @@ -0,0 +1,21 @@ +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'], + ]; + } + public function typeToArrayCases() { + $product001Schema = $this->product001Schema(); + return [ [ Map001Type::class, @@ -28,7 +56,6 @@ public function typeToArrayCases() 'type' => 'object', 'example' => ['id' => 1, 'name' => 'INFO'], ], - null, ], [ Map002Type::class, @@ -39,7 +66,6 @@ public function typeToArrayCases() ], 'example' => ['优' => 90, '良' => 80, '中' => 60], ], - null, ], [ Map003Type::class, @@ -54,14 +80,12 @@ public function typeToArrayCases() ], ], ], - null, ], [ 'string', [ 'type' => 'string', ], - null, ], [ ['string'], @@ -71,7 +95,6 @@ public function typeToArrayCases() 'type' => 'string', ], ], - null, ], [ @@ -79,10 +102,6 @@ public function typeToArrayCases() [ 'type' => ['string', 'null'], ], - [ - 'type' => 'string', - 'nullable' => true, - ], ], [ @@ -93,13 +112,6 @@ public function typeToArrayCases() 'type' => ['string', 'null'], ], ], - [ - 'type' => 'array', - 'items' => [ - 'type' => 'string', - 'nullable' => true, - ], - ], ], [ @@ -111,13 +123,6 @@ public function typeToArrayCases() 'bar', ], ], - [ - 'type' => 'string', - 'enum' => [ - 'foo', - 'bar', - ], - ], ], [ @@ -142,27 +147,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 +168,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 +197,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 +243,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, - ], - ], - ], - ], - ], - ], ], [ @@ -404,52 +298,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 +326,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,43 +363,57 @@ public function typeToArrayCases() 'required' => ['id', 'file'], ], ], + ], + [ + Union001Type::class, + [ + 'oneOf' => [ + [ + 'type' => 'string', + ], + [ + 'type' => 'integer', + ], + $product001Schema, + ], + ], + ], + [ + Discriminated001Type::class, [ - '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}$', + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + 'enum' => ['foobar'], + ], + 'field1' => [ + 'type' => 'string', + ], + ] + ], + ], + [ + RecursiveType::class, + [ + 'type' => 'object', + 'properties' => [ + 'name' => [ + 'type' => 'string', + ], + 'sub' => [ + 'oneOf' => [ + [ + 'type' => 'null' + ], + [ + '$ref' => '#/components/schemas/Recursive', + ] ], ], - 'required' => ['id', 'file'], ], ], - ], + ] ]; } @@ -581,17 +421,13 @@ 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); $this->assertEquals($expect1, $parser->parse($type)); $parser = new TypeParser(TypeParser::MODE_OPEN_API); - $this->assertEquals($expect2 ?? $expect1, $parser->parse($type)); - - $parser = new TypeParser(TypeParser::MODE_OPEN_API | TypeParser::MODE_OPEN_API_31); $this->assertEquals($expect1, $parser->parse($type)); } @@ -616,14 +452,42 @@ public function typeToArrayWithRefCases() 'field2' => [ 'type' => 'array', 'items' => [ - 'type' => 'string', - 'nullable' => true, + 'type' => ['string', 'null'], ], ], ], ], ], ], + [ + Union001Type::class . '?', + [ + 'oneOf' => [ + [ + 'type' => 'null', + ], + [ + '$ref' => '#/components/schemas/Union001', + ], + ], + ], + [ + 'Union001' => [ + 'oneOf' => [ + [ + 'type' => 'string', + ], + [ + 'type' => 'integer', + ], + [ + '$ref' => '#/components/schemas/Product001', + ], + ], + ], + 'Product001' => $this->product001Schema(), + ] + ], ]; } @@ -641,6 +505,121 @@ public function testTypeToArrayWithRef($type, $expect, $schema) public function inputDataCases() { return [ + // cast integer to bool values + [ + [ + '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], + ], + ], + [ + [ + '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], + ], + ], + [ + [ + '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 [ [ @@ -752,6 +731,26 @@ public function testValidateInputData($inputs, $definition, $errors, $data) public function dataCases() { + $nestedSchema = [ + 'type' => 'object', + 'properties' => [ + 'name' => [ + 'type' => 'string', + ], + 'sub' => [ + 'oneOf' => [ + [ + 'type' => 'null' + ], + [ + '$ref' => '#/components/schemas/Recursive', + ] + ], + ], + ], + 'required' => ['name', 'sub'], + ]; + return [ [ '1', @@ -807,6 +806,28 @@ public function dataCases() ], [], ], + + # validate nested data type + [ + [ + 'name' => 'foobar', + 'sub' => [ + 'name' => 'sub foobar', + 'sub' => [ + 'name' => 'sub sub foobar', + 'sub' => null, + ], + ] + ], + $nestedSchema + [ + 'components' => [ + 'schemas' => [ + 'Recursive' => $nestedSchema, + ], + ] + ], + [], + ] ]; } @@ -919,7 +940,6 @@ class Dict003ItemType extends ProductType class Map003Type extends MapType { - public static function valueType(): string { return Dict003ItemType::class; @@ -929,5 +949,31 @@ public static function example(): array { return []; } +} + +class Union001Type extends UnionType +{ + public static function allowedTypes(): array + { + return [ + 'string', + 'integer', + Product001Type::class, + ]; + } +} + +class Discriminated001Type extends ProductType +{ + /** + * @enum foobar + */ + public static string $type = 'string'; + public static string $field1 = 'string'; +} +class RecursiveType extends ProductType +{ + public static $name = 'string'; + public static $sub = RecursiveType::class . '?'; }