From d8798af27b3e9cd6e8713c381c0dc49e8f1d89a0 Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 27 Nov 2020 13:42:46 +1000 Subject: [PATCH 1/3] Include date format for date types --- src/Parameters/BodyParameterGenerator.php | 4 +++ .../Concerns/GeneratesFromRules.php | 9 +++++++ .../Parameters/BodyParameterGeneratorTest.php | 25 ++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Parameters/BodyParameterGenerator.php b/src/Parameters/BodyParameterGenerator.php index 26ea1b3..d7b6822 100644 --- a/src/Parameters/BodyParameterGenerator.php +++ b/src/Parameters/BodyParameterGenerator.php @@ -105,6 +105,10 @@ protected function getNewPropObj($type, $rules) $propObj['enum'] = $enums; } + if ($format = $this->getFormatValue($rules)) { + $propObj['format'] = $format; + } + if ($type === 'array') { $propObj['items'] = []; } elseif ($type === 'object') { diff --git a/src/Parameters/Concerns/GeneratesFromRules.php b/src/Parameters/Concerns/GeneratesFromRules.php index 16f60cb..cfff667 100644 --- a/src/Parameters/Concerns/GeneratesFromRules.php +++ b/src/Parameters/Concerns/GeneratesFromRules.php @@ -46,6 +46,15 @@ protected function getArrayKey($param) return current(explode('.', $param)); } + protected function getFormatValue(array $paramRules) + { + if (in_array('date', $paramRules)) { + return 'date'; + } + + return false; + } + protected function getEnumValues(array $paramRules) { $in = $this->getInParameter($paramRules); diff --git a/tests/Parameters/BodyParameterGeneratorTest.php b/tests/Parameters/BodyParameterGeneratorTest.php index 889f48f..98d1c60 100644 --- a/tests/Parameters/BodyParameterGeneratorTest.php +++ b/tests/Parameters/BodyParameterGeneratorTest.php @@ -47,6 +47,14 @@ public function testRequiredParameters() */ public function testDataTypes($bodyParameters) { + //Just testing types here + $properties = array_map(function($property) { + if (!array_key_exists('type', $property)) { + return []; + } + return ['type' => $property['type']]; + }, $bodyParameters['schema']['properties']); + $this->assertEquals([ 'id' => ['type' => 'integer'], 'email' => ['type' => 'string'], @@ -55,7 +63,7 @@ public function testDataTypes($bodyParameters) 'picture' => ['type' => 'string'], 'is_validated' => ['type' => 'boolean'], 'score' => ['type' => 'number'], - ], $bodyParameters['schema']['properties']); + ], $properties); } public function testNoRequiredParameters() @@ -79,6 +87,21 @@ public function testEnumInBody() ], $bodyParameters['schema']['properties']); } + + public function testDateFormatInBody() + { + $bodyParameters = $this->getBodyParameters([ + 'account_type' => 'date', + ]); + + $this->assertEquals([ + 'account_type' => [ + 'type' => 'string', + 'format' => 'date', + ], + ], $bodyParameters['schema']['properties']); + } + public function testArraySyntax() { $bodyParameters = $this->getBodyParameters([ From 3d367f3b284b62eb8f3352b600f36074b67ab366 Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 27 Nov 2020 13:49:44 +1000 Subject: [PATCH 2/3] StyleCI --- tests/Parameters/BodyParameterGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Parameters/BodyParameterGeneratorTest.php b/tests/Parameters/BodyParameterGeneratorTest.php index 98d1c60..13f3015 100644 --- a/tests/Parameters/BodyParameterGeneratorTest.php +++ b/tests/Parameters/BodyParameterGeneratorTest.php @@ -48,7 +48,7 @@ public function testRequiredParameters() public function testDataTypes($bodyParameters) { //Just testing types here - $properties = array_map(function($property) { + $properties = array_map(function ($property) { if (!array_key_exists('type', $property)) { return []; } From 2d71c672f0c7456616f9bd222f06b5fcde59e91e Mon Sep 17 00:00:00 2001 From: "markl@room40.com.au" Date: Fri, 27 Nov 2020 13:50:49 +1000 Subject: [PATCH 3/3] StyleCI #2 --- tests/Parameters/BodyParameterGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Parameters/BodyParameterGeneratorTest.php b/tests/Parameters/BodyParameterGeneratorTest.php index 13f3015..0a1d2e0 100644 --- a/tests/Parameters/BodyParameterGeneratorTest.php +++ b/tests/Parameters/BodyParameterGeneratorTest.php @@ -52,6 +52,7 @@ public function testDataTypes($bodyParameters) if (!array_key_exists('type', $property)) { return []; } + return ['type' => $property['type']]; }, $bodyParameters['schema']['properties']); @@ -87,7 +88,6 @@ public function testEnumInBody() ], $bodyParameters['schema']['properties']); } - public function testDateFormatInBody() { $bodyParameters = $this->getBodyParameters([