mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
FormModelDescriber: set type to array with correct example for multiple choices form. (#1212)
* FormModelDescriber: set type to array with correct example for multiple choices form. * Remove arrays examples and add test for multiple ChoiceType form * Use of "items" for multiple ChoiceType form. Detect "choices" type to determine property or items type. Remove unused variable declaration.
This commit is contained in:
parent
0012b9438a
commit
72010c1d98
@ -45,7 +45,6 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
}
|
||||
|
||||
$schema->setType('object');
|
||||
$properties = $schema->getProperties();
|
||||
|
||||
$class = $model->getType()->getClassName();
|
||||
|
||||
@ -101,9 +100,19 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
}
|
||||
|
||||
if ('choice' === $blockPrefix) {
|
||||
$property->setType('string');
|
||||
if ($config->getOption('multiple')) {
|
||||
$property->setType('array');
|
||||
} else {
|
||||
$property->setType('string');
|
||||
}
|
||||
if (($choices = $config->getOption('choices')) && is_array($choices) && count($choices)) {
|
||||
$property->setEnum(array_values($choices));
|
||||
$enums = array_values($choices);
|
||||
$type = $this->isNumbersArray($enums) ? 'number' : 'string';
|
||||
if ($config->getOption('multiple')) {
|
||||
$property->getItems()->setType($type)->setEnum($enums);
|
||||
} else {
|
||||
$property->setType($type)->setEnum($enums);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
@ -130,7 +139,6 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
if ($config->getOption('multiple')) {
|
||||
$property->setFormat(sprintf('[%s id]', $entityClass));
|
||||
$property->setType('array');
|
||||
$property->setExample('[1, 2, 3]');
|
||||
} else {
|
||||
$property->setType('string');
|
||||
$property->setFormat(sprintf('%s id', $entityClass));
|
||||
@ -157,6 +165,22 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return bool true if $array contains only numbers, false otherwise
|
||||
*/
|
||||
private function isNumbersArray(array $array): bool
|
||||
{
|
||||
foreach ($array as $item) {
|
||||
if (!is_numeric($item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function isBuiltinType(string $type): bool
|
||||
{
|
||||
return 0 === strpos($type, 'Symfony\Component\Form\Extension\Core\Type');
|
||||
|
@ -24,6 +24,7 @@ class DummyType extends AbstractType
|
||||
{
|
||||
$builder->add('bar', TextType::class, ['required' => false]);
|
||||
$builder->add('foo', ChoiceType::class, ['choices' => ['male', 'female']]);
|
||||
$builder->add('foz', ChoiceType::class, ['choices' => ['male', 'female'], 'multiple' => true]);
|
||||
$builder->add('baz', CheckboxType::class, ['required' => false]);
|
||||
$builder->add('bey', IntegerType::class, ['required' => false]);
|
||||
}
|
||||
|
@ -228,6 +228,13 @@ class FunctionalTest extends WebTestCase
|
||||
'type' => 'string',
|
||||
'enum' => ['male', 'female'],
|
||||
],
|
||||
'foz' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => ['male', 'female'],
|
||||
],
|
||||
],
|
||||
'baz' => [
|
||||
'type' => 'boolean',
|
||||
],
|
||||
@ -235,7 +242,7 @@ class FunctionalTest extends WebTestCase
|
||||
'type' => 'integer',
|
||||
],
|
||||
],
|
||||
'required' => ['foo'],
|
||||
'required' => ['foo', 'foz'],
|
||||
], $this->getModel('DummyType')->toArray());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user