From 882f658599de4880b4b6490d42ddd7bac97e1cd6 Mon Sep 17 00:00:00 2001 From: Bez Hermoso Date: Thu, 26 Jun 2014 12:27:47 -0700 Subject: [PATCH] Added 'default' parameters in {JmsMetadata,Validator}Parser, and FOSRestHandler. --- Extractor/ApiDocExtractor.php | 3 ++ Extractor/Handler/FosRestHandler.php | 8 +++-- Parser/JmsMetadataParser.php | 5 ++- Parser/ValidationParser.php | 9 +++-- Parser/ValidationParserLegacy.php | 5 +++ Tests/Fixtures/Model/JmsNested.php | 2 +- Tests/Fixtures/Model/Test.php | 2 +- Tests/Fixtures/Model/ValidatorTest.php | 4 +-- Tests/Formatter/MarkdownFormatterTest.php | 1 + Tests/Formatter/SimpleFormatterTest.php | 10 +++--- Tests/Parser/JmsMetadataParserTest.php | 12 +++---- Tests/Parser/ValidationParserTest.php | 40 ++++++++++++++++------- 12 files changed, 71 insertions(+), 30 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index a8283b4..03f272f 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -349,6 +349,7 @@ class ApiDocExtractor * - Requirement parameters are concatenated. * - Other string values are overridden by later parsers when present. * - Array parameters are recursively merged. + * - Non-null default values prevail over null default values. Later values overrides previous defaults. * * @param array $p1 The pre-existing parameters array. * @param array $p2 The newly-returned parameters array. @@ -380,6 +381,8 @@ class ApiDocExtractor } else { $v1[$name] = $value; } + } elseif ($name == 'default') { + $v1[$name] = $value ?: $v1[$name]; } else { $v1[$name] = $value; } diff --git a/Extractor/Handler/FosRestHandler.php b/Extractor/Handler/FosRestHandler.php index b12b583..636e05f 100644 --- a/Extractor/Handler/FosRestHandler.php +++ b/Extractor/Handler/FosRestHandler.php @@ -31,14 +31,18 @@ class FosRestHandler implements HandlerInterface if ($annot instanceof RequestParam) { $requirements = $this->handleRequirements($annot->requirements); - $annotation->addParameter($annot->name, array( + $data = array( 'required' => $annot->strict && $annot->nullable === false && $annot->default === null, 'dataType' => $requirements, 'actualType' => $this->inferType($requirements), 'subType' => null, 'description' => $annot->description, 'readonly' => false - )); + ); + if ($annot->strict === false) { + $data['default'] = $annot->default; + } + $annotation->addParameter($annot->name, $data); } elseif ($annot instanceof QueryParam) { if ($annot->strict && $annot->nullable === false && $annot->default === null) { $annotation->addRequirement($annot->name, array( diff --git a/Parser/JmsMetadataParser.php b/Parser/JmsMetadataParser.php index 33af1ac..5b8f89f 100644 --- a/Parser/JmsMetadataParser.php +++ b/Parser/JmsMetadataParser.php @@ -114,6 +114,9 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface $params = array(); + $reflection = new \ReflectionClass($className); + $defaultProperties = $reflection->getDefaultProperties(); + // iterate over property metadata foreach ($meta->propertyMetadata as $item) { if (!is_null($item->type)) { @@ -133,7 +136,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface 'actualType' => $dataType['actualType'], 'subType' => $dataType['class'], 'required' => false, - 'default' => null, + 'default' => isset($defaultProperties[$item->name]) ? $defaultProperties[$item->name] : null, //TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this 'description' => $this->getDescription($item), 'readonly' => $item->readOnly, diff --git a/Parser/ValidationParser.php b/Parser/ValidationParser.php index bd72c94..1ce1063 100644 --- a/Parser/ValidationParser.php +++ b/Parser/ValidationParser.php @@ -85,8 +85,14 @@ class ValidationParser implements ParserInterface, PostParserInterface $classdata = $this->factory->getMetadataFor($className); $properties = $classdata->getConstrainedProperties(); + $refl = $classdata->getReflectionClass(); + $defaults = $refl->getDefaultProperties(); + foreach ($properties as $property) { $vparams = array(); + + $vparams['default'] = isset($defaults[$property]) ? $defaults[$property] : null; + $pds = $classdata->getPropertyMetadata($property); foreach ($pds as $propdata) { $constraints = $propdata->getConstraints(); @@ -159,8 +165,7 @@ class ValidationParser implements ParserInterface, PostParserInterface $class = substr(get_class($constraint), strlen('Symfony\\Component\\Validator\\Constraints\\')); $vparams['actualType'] = DataTypes::STRING; - $vparams['subType'] = null; - $vparams['default'] = null; + $vparams['subType'] = null; switch ($class) { case 'NotBlank': diff --git a/Parser/ValidationParserLegacy.php b/Parser/ValidationParserLegacy.php index df342e8..173b00d 100644 --- a/Parser/ValidationParserLegacy.php +++ b/Parser/ValidationParserLegacy.php @@ -56,9 +56,14 @@ class ValidationParserLegacy extends ValidationParser $properties = $classdata->getConstrainedProperties(); + $refl = $classdata->getReflectionClass(); + $defaults = $refl->getDefaultProperties(); + foreach ($properties as $property) { $vparams = array(); + $vparams['default'] = isset($defaults[$property]) ? $defaults[$property] : null; + $pds = $classdata->getMemberMetadatas($property); foreach ($pds as $propdata) { diff --git a/Tests/Fixtures/Model/JmsNested.php b/Tests/Fixtures/Model/JmsNested.php index 18877fb..bfc946b 100644 --- a/Tests/Fixtures/Model/JmsNested.php +++ b/Tests/Fixtures/Model/JmsNested.php @@ -15,7 +15,7 @@ class JmsNested /** * @JMS\Type("string"); */ - public $bar; + public $bar = 'baz'; /** * Epic description. diff --git a/Tests/Fixtures/Model/Test.php b/Tests/Fixtures/Model/Test.php index 51febf7..4536fcd 100644 --- a/Tests/Fixtures/Model/Test.php +++ b/Tests/Fixtures/Model/Test.php @@ -20,7 +20,7 @@ class Test * @Assert\NotBlank * @Assert\Type("string") */ - public $a; + public $a = 'nelmio'; /** * @Assert\Type("DateTime"); diff --git a/Tests/Fixtures/Model/ValidatorTest.php b/Tests/Fixtures/Model/ValidatorTest.php index fbef225..fb2b922 100644 --- a/Tests/Fixtures/Model/ValidatorTest.php +++ b/Tests/Fixtures/Model/ValidatorTest.php @@ -18,7 +18,7 @@ class ValidatorTest /** * @Assert\Length(min=10); */ - public $length10; + public $length10 = 'validate this'; /** * @Assert\Length(min=1, max=10) @@ -63,7 +63,7 @@ class ValidatorTest /** * @Assert\Url() */ - public $url; + public $url = 'https://github.com'; /** * @Assert\Ip() diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index 614d1b6..b785a1d 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -223,6 +223,7 @@ nested[bar]: * type: string * required: false + * default value: baz nested[baz][]: diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 5de23fc..38a21ce 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -138,6 +138,7 @@ class SimpleFormatterTest extends WebTestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, + 'default' => null, 'default' => "DefaultTest", 'required' => true, 'description' => '', @@ -197,6 +198,7 @@ class SimpleFormatterTest extends WebTestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, + 'default' => null, 'default' => "DefaultTest", 'required' => true, 'description' => '', @@ -376,7 +378,7 @@ class SimpleFormatterTest extends WebTestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => '', 'readonly' => false, @@ -714,7 +716,7 @@ And, it supports multilines until the first \'@\' char.', 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => '', 'readonly' => false, @@ -1056,7 +1058,7 @@ With multiple lines.', 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'nelmio', 'format' => '{length: min: foo}, {not blank}', 'required' => true, 'readonly' => null @@ -1183,7 +1185,7 @@ With multiple lines.', 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'nelmio', 'format' => '{length: min: foo}, {not blank}', 'required' => true, 'readonly' => null diff --git a/Tests/Parser/JmsMetadataParserTest.php b/Tests/Parser/JmsMetadataParserTest.php index c974760..27a1d00 100644 --- a/Tests/Parser/JmsMetadataParserTest.php +++ b/Tests/Parser/JmsMetadataParserTest.php @@ -96,7 +96,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, @@ -178,7 +178,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, @@ -225,7 +225,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, @@ -250,7 +250,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, @@ -297,7 +297,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, @@ -379,7 +379,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase 'dataType' => 'string', 'actualType' => DataTypes::STRING, 'subType' => null, - 'default' => null, + 'default' => 'baz', 'required' => false, 'description' => null, 'readonly' => false, diff --git a/Tests/Parser/ValidationParserTest.php b/Tests/Parser/ValidationParserTest.php index 0bca5b5..d614b6e 100644 --- a/Tests/Parser/ValidationParserTest.php +++ b/Tests/Parser/ValidationParserTest.php @@ -42,25 +42,29 @@ class ValidationParserTest extends WebTestCase array( 'property' => 'length10', 'expected' => array( - 'format' => '{length: min: 10}' + 'format' => '{length: min: 10}', + 'default' => 'validate this', ) ), array( 'property' => 'length1to10', 'expected' => array( - 'format' => '{length: min: 1, max: 10}' + 'format' => '{length: min: 1, max: 10}', + 'default' => null, ) ), array( 'property' => 'notblank', 'expected' => array( - 'required' => true + 'required' => true, + 'default' => null, ) ), array( 'property' => 'notnull', 'expected' => array( - 'required' => true + 'required' => true, + 'default' => null, ) ), array( @@ -68,6 +72,7 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'dataType' => 'DateTime', 'actualType' => DataTypes::DATETIME, + 'default' => null, ) ), array( @@ -75,6 +80,7 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'format' => '{Date YYYY-MM-DD}', 'actualType' => DataTypes::DATE, + 'default' => null, ) ), array( @@ -82,6 +88,7 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'format' => '{DateTime YYYY-MM-DD HH:MM:SS}', 'actualType' => DataTypes::DATETIME, + 'default' => null, ) ), array( @@ -89,24 +96,28 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'format' => '{Time HH:MM:SS}', 'actualType' => DataTypes::TIME, + 'default' => null, ) ), array( 'property' => 'email', 'expected' => array( - 'format' => '{email address}' + 'format' => '{email address}', + 'default' => null, ) ), array( 'property' => 'url', 'expected' => array( - 'format' => '{url}' + 'format' => '{url}', + 'default' => 'https://github.com', ) ), array( 'property' => 'ip', 'expected' => array( - 'format' => '{ip address}' + 'format' => '{ip address}', + 'default' => null, ) ), array( @@ -114,6 +125,7 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'format' => '[a|b]', 'actualType' => DataTypes::ENUM, + 'default' => null, ) ), array( @@ -122,6 +134,7 @@ class ValidationParserTest extends WebTestCase 'format' => '{choice of [x|y|z]}', 'actualType' => DataTypes::COLLECTION, 'subType' => DataTypes::ENUM, + 'default' => null, ) ), array( @@ -130,18 +143,21 @@ class ValidationParserTest extends WebTestCase 'format' => '{min: 2 max: 3 choice of [foo|bar|baz|qux]}', 'actualType' => DataTypes::COLLECTION, 'subType' => DataTypes::ENUM, + 'default' => null, ) ), array( 'property' => 'regexmatch', 'expected' => array( - 'format' => '{match: /^\d{1,4}\w{1,4}$/}' + 'format' => '{match: /^\d{1,4}\w{1,4}$/}', + 'default' => null, ) ), array( 'property' => 'regexnomatch', 'expected' => array( - 'format' => '{not match: /\d/}' + 'format' => '{not match: /\d/}', + 'default' => null, ) ), array( @@ -149,13 +165,15 @@ class ValidationParserTest extends WebTestCase 'expected' => array( 'required' => true, 'dataType' => 'string', - 'format' => '{email address}' + 'format' => '{email address}', + 'default' => null, ) ), array( 'property' => 'multipleformats', 'expected' => array( - 'format' => '{url}, {length: min: 10}' + 'format' => '{url}, {length: min: 10}', + 'default' => null, ) ) );