mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Added 'default' parameters in {JmsMetadata,Validator}Parser, and FOSRestHandler.
This commit is contained in:
parent
0d1bde9f8a
commit
882f658599
@ -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;
|
||||
}
|
||||
|
@ -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(
|
||||
|
@ -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,
|
||||
|
@ -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':
|
||||
|
@ -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) {
|
||||
|
@ -15,7 +15,7 @@ class JmsNested
|
||||
/**
|
||||
* @JMS\Type("string");
|
||||
*/
|
||||
public $bar;
|
||||
public $bar = 'baz';
|
||||
|
||||
/**
|
||||
* Epic description.
|
||||
|
@ -20,7 +20,7 @@ class Test
|
||||
* @Assert\NotBlank
|
||||
* @Assert\Type("string")
|
||||
*/
|
||||
public $a;
|
||||
public $a = 'nelmio';
|
||||
|
||||
/**
|
||||
* @Assert\Type("DateTime");
|
||||
|
@ -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()
|
||||
|
@ -223,6 +223,7 @@ nested[bar]:
|
||||
|
||||
* type: string
|
||||
* required: false
|
||||
* default value: baz
|
||||
|
||||
nested[baz][]:
|
||||
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
)
|
||||
)
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user