From 65213a2bf07de95a64beb5efcc11e7361b2ee82c Mon Sep 17 00:00:00 2001 From: Jin Hu Date: Sat, 18 Jul 2020 16:44:22 +0800 Subject: [PATCH] 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'], ], ],