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.
|
* - Requirement parameters are concatenated.
|
||||||
* - Other string values are overridden by later parsers when present.
|
* - Other string values are overridden by later parsers when present.
|
||||||
* - Array parameters are recursively merged.
|
* - 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 $p1 The pre-existing parameters array.
|
||||||
* @param array $p2 The newly-returned parameters array.
|
* @param array $p2 The newly-returned parameters array.
|
||||||
@ -380,6 +381,8 @@ class ApiDocExtractor
|
|||||||
} else {
|
} else {
|
||||||
$v1[$name] = $value;
|
$v1[$name] = $value;
|
||||||
}
|
}
|
||||||
|
} elseif ($name == 'default') {
|
||||||
|
$v1[$name] = $value ?: $v1[$name];
|
||||||
} else {
|
} else {
|
||||||
$v1[$name] = $value;
|
$v1[$name] = $value;
|
||||||
}
|
}
|
||||||
|
@ -31,14 +31,18 @@ class FosRestHandler implements HandlerInterface
|
|||||||
if ($annot instanceof RequestParam) {
|
if ($annot instanceof RequestParam) {
|
||||||
|
|
||||||
$requirements = $this->handleRequirements($annot->requirements);
|
$requirements = $this->handleRequirements($annot->requirements);
|
||||||
$annotation->addParameter($annot->name, array(
|
$data = array(
|
||||||
'required' => $annot->strict && $annot->nullable === false && $annot->default === null,
|
'required' => $annot->strict && $annot->nullable === false && $annot->default === null,
|
||||||
'dataType' => $requirements,
|
'dataType' => $requirements,
|
||||||
'actualType' => $this->inferType($requirements),
|
'actualType' => $this->inferType($requirements),
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'description' => $annot->description,
|
'description' => $annot->description,
|
||||||
'readonly' => false
|
'readonly' => false
|
||||||
));
|
);
|
||||||
|
if ($annot->strict === false) {
|
||||||
|
$data['default'] = $annot->default;
|
||||||
|
}
|
||||||
|
$annotation->addParameter($annot->name, $data);
|
||||||
} elseif ($annot instanceof QueryParam) {
|
} elseif ($annot instanceof QueryParam) {
|
||||||
if ($annot->strict && $annot->nullable === false && $annot->default === null) {
|
if ($annot->strict && $annot->nullable === false && $annot->default === null) {
|
||||||
$annotation->addRequirement($annot->name, array(
|
$annotation->addRequirement($annot->name, array(
|
||||||
|
@ -114,6 +114,9 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface
|
|||||||
|
|
||||||
$params = array();
|
$params = array();
|
||||||
|
|
||||||
|
$reflection = new \ReflectionClass($className);
|
||||||
|
$defaultProperties = $reflection->getDefaultProperties();
|
||||||
|
|
||||||
// iterate over property metadata
|
// iterate over property metadata
|
||||||
foreach ($meta->propertyMetadata as $item) {
|
foreach ($meta->propertyMetadata as $item) {
|
||||||
if (!is_null($item->type)) {
|
if (!is_null($item->type)) {
|
||||||
@ -133,7 +136,7 @@ class JmsMetadataParser implements ParserInterface, PostParserInterface
|
|||||||
'actualType' => $dataType['actualType'],
|
'actualType' => $dataType['actualType'],
|
||||||
'subType' => $dataType['class'],
|
'subType' => $dataType['class'],
|
||||||
'required' => false,
|
'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
|
//TODO: can't think of a good way to specify this one, JMS doesn't have a setting for this
|
||||||
'description' => $this->getDescription($item),
|
'description' => $this->getDescription($item),
|
||||||
'readonly' => $item->readOnly,
|
'readonly' => $item->readOnly,
|
||||||
|
@ -85,8 +85,14 @@ class ValidationParser implements ParserInterface, PostParserInterface
|
|||||||
$classdata = $this->factory->getMetadataFor($className);
|
$classdata = $this->factory->getMetadataFor($className);
|
||||||
$properties = $classdata->getConstrainedProperties();
|
$properties = $classdata->getConstrainedProperties();
|
||||||
|
|
||||||
|
$refl = $classdata->getReflectionClass();
|
||||||
|
$defaults = $refl->getDefaultProperties();
|
||||||
|
|
||||||
foreach ($properties as $property) {
|
foreach ($properties as $property) {
|
||||||
$vparams = array();
|
$vparams = array();
|
||||||
|
|
||||||
|
$vparams['default'] = isset($defaults[$property]) ? $defaults[$property] : null;
|
||||||
|
|
||||||
$pds = $classdata->getPropertyMetadata($property);
|
$pds = $classdata->getPropertyMetadata($property);
|
||||||
foreach ($pds as $propdata) {
|
foreach ($pds as $propdata) {
|
||||||
$constraints = $propdata->getConstraints();
|
$constraints = $propdata->getConstraints();
|
||||||
@ -159,8 +165,7 @@ class ValidationParser implements ParserInterface, PostParserInterface
|
|||||||
$class = substr(get_class($constraint), strlen('Symfony\\Component\\Validator\\Constraints\\'));
|
$class = substr(get_class($constraint), strlen('Symfony\\Component\\Validator\\Constraints\\'));
|
||||||
|
|
||||||
$vparams['actualType'] = DataTypes::STRING;
|
$vparams['actualType'] = DataTypes::STRING;
|
||||||
$vparams['subType'] = null;
|
$vparams['subType'] = null;
|
||||||
$vparams['default'] = null;
|
|
||||||
|
|
||||||
switch ($class) {
|
switch ($class) {
|
||||||
case 'NotBlank':
|
case 'NotBlank':
|
||||||
|
@ -56,9 +56,14 @@ class ValidationParserLegacy extends ValidationParser
|
|||||||
|
|
||||||
$properties = $classdata->getConstrainedProperties();
|
$properties = $classdata->getConstrainedProperties();
|
||||||
|
|
||||||
|
$refl = $classdata->getReflectionClass();
|
||||||
|
$defaults = $refl->getDefaultProperties();
|
||||||
|
|
||||||
foreach ($properties as $property) {
|
foreach ($properties as $property) {
|
||||||
$vparams = array();
|
$vparams = array();
|
||||||
|
|
||||||
|
$vparams['default'] = isset($defaults[$property]) ? $defaults[$property] : null;
|
||||||
|
|
||||||
$pds = $classdata->getMemberMetadatas($property);
|
$pds = $classdata->getMemberMetadatas($property);
|
||||||
|
|
||||||
foreach ($pds as $propdata) {
|
foreach ($pds as $propdata) {
|
||||||
|
@ -15,7 +15,7 @@ class JmsNested
|
|||||||
/**
|
/**
|
||||||
* @JMS\Type("string");
|
* @JMS\Type("string");
|
||||||
*/
|
*/
|
||||||
public $bar;
|
public $bar = 'baz';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Epic description.
|
* Epic description.
|
||||||
|
@ -20,7 +20,7 @@ class Test
|
|||||||
* @Assert\NotBlank
|
* @Assert\NotBlank
|
||||||
* @Assert\Type("string")
|
* @Assert\Type("string")
|
||||||
*/
|
*/
|
||||||
public $a;
|
public $a = 'nelmio';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Assert\Type("DateTime");
|
* @Assert\Type("DateTime");
|
||||||
|
@ -18,7 +18,7 @@ class ValidatorTest
|
|||||||
/**
|
/**
|
||||||
* @Assert\Length(min=10);
|
* @Assert\Length(min=10);
|
||||||
*/
|
*/
|
||||||
public $length10;
|
public $length10 = 'validate this';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Assert\Length(min=1, max=10)
|
* @Assert\Length(min=1, max=10)
|
||||||
@ -63,7 +63,7 @@ class ValidatorTest
|
|||||||
/**
|
/**
|
||||||
* @Assert\Url()
|
* @Assert\Url()
|
||||||
*/
|
*/
|
||||||
public $url;
|
public $url = 'https://github.com';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Assert\Ip()
|
* @Assert\Ip()
|
||||||
|
@ -223,6 +223,7 @@ nested[bar]:
|
|||||||
|
|
||||||
* type: string
|
* type: string
|
||||||
* required: false
|
* required: false
|
||||||
|
* default value: baz
|
||||||
|
|
||||||
nested[baz][]:
|
nested[baz][]:
|
||||||
|
|
||||||
|
@ -138,6 +138,7 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
|
'default' => null,
|
||||||
'default' => "DefaultTest",
|
'default' => "DefaultTest",
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'description' => '',
|
'description' => '',
|
||||||
@ -197,6 +198,7 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
|
'default' => null,
|
||||||
'default' => "DefaultTest",
|
'default' => "DefaultTest",
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'description' => '',
|
'description' => '',
|
||||||
@ -376,7 +378,7 @@ class SimpleFormatterTest extends WebTestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => '',
|
'description' => '',
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -714,7 +716,7 @@ And, it supports multilines until the first \'@\' char.',
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => '',
|
'description' => '',
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -1056,7 +1058,7 @@ With multiple lines.',
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'nelmio',
|
||||||
'format' => '{length: min: foo}, {not blank}',
|
'format' => '{length: min: foo}, {not blank}',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'readonly' => null
|
'readonly' => null
|
||||||
@ -1183,7 +1185,7 @@ With multiple lines.',
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'nelmio',
|
||||||
'format' => '{length: min: foo}, {not blank}',
|
'format' => '{length: min: foo}, {not blank}',
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'readonly' => null
|
'readonly' => null
|
||||||
|
@ -96,7 +96,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -178,7 +178,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -225,7 +225,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -250,7 +250,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -297,7 +297,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
@ -379,7 +379,7 @@ class JmsMetadataParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'actualType' => DataTypes::STRING,
|
'actualType' => DataTypes::STRING,
|
||||||
'subType' => null,
|
'subType' => null,
|
||||||
'default' => null,
|
'default' => 'baz',
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'readonly' => false,
|
'readonly' => false,
|
||||||
|
@ -42,25 +42,29 @@ class ValidationParserTest extends WebTestCase
|
|||||||
array(
|
array(
|
||||||
'property' => 'length10',
|
'property' => 'length10',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{length: min: 10}'
|
'format' => '{length: min: 10}',
|
||||||
|
'default' => 'validate this',
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'length1to10',
|
'property' => 'length1to10',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{length: min: 1, max: 10}'
|
'format' => '{length: min: 1, max: 10}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'notblank',
|
'property' => 'notblank',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'required' => true
|
'required' => true,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'notnull',
|
'property' => 'notnull',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'required' => true
|
'required' => true,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -68,6 +72,7 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'dataType' => 'DateTime',
|
'dataType' => 'DateTime',
|
||||||
'actualType' => DataTypes::DATETIME,
|
'actualType' => DataTypes::DATETIME,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -75,6 +80,7 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{Date YYYY-MM-DD}',
|
'format' => '{Date YYYY-MM-DD}',
|
||||||
'actualType' => DataTypes::DATE,
|
'actualType' => DataTypes::DATE,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -82,6 +88,7 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{DateTime YYYY-MM-DD HH:MM:SS}',
|
'format' => '{DateTime YYYY-MM-DD HH:MM:SS}',
|
||||||
'actualType' => DataTypes::DATETIME,
|
'actualType' => DataTypes::DATETIME,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -89,24 +96,28 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{Time HH:MM:SS}',
|
'format' => '{Time HH:MM:SS}',
|
||||||
'actualType' => DataTypes::TIME,
|
'actualType' => DataTypes::TIME,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'email',
|
'property' => 'email',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{email address}'
|
'format' => '{email address}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'url',
|
'property' => 'url',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{url}'
|
'format' => '{url}',
|
||||||
|
'default' => 'https://github.com',
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'ip',
|
'property' => 'ip',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{ip address}'
|
'format' => '{ip address}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -114,6 +125,7 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '[a|b]',
|
'format' => '[a|b]',
|
||||||
'actualType' => DataTypes::ENUM,
|
'actualType' => DataTypes::ENUM,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -122,6 +134,7 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'format' => '{choice of [x|y|z]}',
|
'format' => '{choice of [x|y|z]}',
|
||||||
'actualType' => DataTypes::COLLECTION,
|
'actualType' => DataTypes::COLLECTION,
|
||||||
'subType' => DataTypes::ENUM,
|
'subType' => DataTypes::ENUM,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -130,18 +143,21 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'format' => '{min: 2 max: 3 choice of [foo|bar|baz|qux]}',
|
'format' => '{min: 2 max: 3 choice of [foo|bar|baz|qux]}',
|
||||||
'actualType' => DataTypes::COLLECTION,
|
'actualType' => DataTypes::COLLECTION,
|
||||||
'subType' => DataTypes::ENUM,
|
'subType' => DataTypes::ENUM,
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'regexmatch',
|
'property' => 'regexmatch',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{match: /^\d{1,4}\w{1,4}$/}'
|
'format' => '{match: /^\d{1,4}\w{1,4}$/}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'regexnomatch',
|
'property' => 'regexnomatch',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{not match: /\d/}'
|
'format' => '{not match: /\d/}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
@ -149,13 +165,15 @@ class ValidationParserTest extends WebTestCase
|
|||||||
'expected' => array(
|
'expected' => array(
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'dataType' => 'string',
|
'dataType' => 'string',
|
||||||
'format' => '{email address}'
|
'format' => '{email address}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'property' => 'multipleformats',
|
'property' => 'multipleformats',
|
||||||
'expected' => array(
|
'expected' => array(
|
||||||
'format' => '{url}, {length: min: 10}'
|
'format' => '{url}, {length: min: 10}',
|
||||||
|
'default' => null,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user