Skip to content

Commit 137621a

Browse files
authored
Merge pull request #20 from Ehyiah/update-body-generation
add choices when generating body from formType in ChoiceType field
2 parents 5a5e90b + 3601499 commit 137621a

File tree

6 files changed

+141
-6
lines changed

6 files changed

+141
-6
lines changed

phpunit.xml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
bootstrap="vendor/autoload.php"
66
cacheResult="false"
77
executionOrder="depends,defects"
8-
failOnRisky="true"
98
verbose="true">
109

1110
<testsuites>

src/Command/ComponentGeneration/AbstractGenerateComponentCommand.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,19 @@ public static function handleEnum(array &$array, ReflectionClass $reflectionClas
271271
}
272272

273273
/**
274+
* @param array<mixed> $property
275+
*
274276
* @return array<mixed>
275277
*/
276-
public static function guessTypeFromFormPrefix(FormInterface $form): array
278+
public function guessTypeFromFormPrefix(FormInterface $form, ?array &$property = null): array
277279
{
278280
$config = $form->getConfig();
279281
$type = $config->getType();
280282
$blockPrefix = $type->getBlockPrefix();
281283

282-
$property = [];
284+
if (null === $property) {
285+
$property = [];
286+
}
283287

284288
if ('text' === $blockPrefix) {
285289
$property['type'] = 'string';
@@ -329,9 +333,20 @@ public static function guessTypeFromFormPrefix(FormInterface $form): array
329333
if ('choice' === $blockPrefix) {
330334
if (true === $config->getOption('multiple')) {
331335
$property['type'] = 'array';
332-
$property['enum'] = [];
336+
$choices = $config->getOption('choices');
337+
if (is_array($choices) && count($choices) > 0) {
338+
$property['enum'] = $choices;
339+
} else {
340+
$property['enum'] = [];
341+
}
333342
} else {
334343
$property['type'] = 'string';
344+
$choices = $config->getOption('choices');
345+
if (is_array($choices) && count($choices) > 0) {
346+
$property['enum'] = $choices;
347+
} else {
348+
$property['enum'] = [];
349+
}
335350
}
336351

337352
return $property;
@@ -369,6 +384,16 @@ protected static function addPropertyFromFormType(array &$array, string $propert
369384
$format = $informations['format'];
370385
$array[$property]['format'] = $format;
371386
}
387+
388+
if (isset($informations['enum'])) {
389+
$enum = $informations['enum'];
390+
$array[$property]['enum'] = $enum;
391+
}
392+
393+
if (isset($informations['items'])) {
394+
$enum = $informations['items'];
395+
$array[$property]['items'] = $enum;
396+
}
372397
}
373398

374399
return false;

src/Command/ComponentGeneration/GenerateComponentRequestBodyCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7474

7575
foreach ($form->all() as $child) {
7676
$name = $child->getName();
77-
$typeInformations = self::guessTypeFromFormPrefix($child);
77+
$typeInformations = $this->guessTypeFromFormPrefix($child);
7878

7979
if (!self::addPropertyFromFormType($propertiesArray, $name, $typeInformations)) {
8080
$ignoredProperty[] = $name;

tests/Command/ComponentGeneration/GenerateComponentRequestBodyCommandTest.php

+55-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
final class GenerateComponentRequestBodyCommandTest extends TestCase
1717
{
18-
public function testRequestBodyGeneration(): void
18+
public function testRequestBodyGenerationFromObject(): void
1919
{
2020
$kernel = new AppKernelTest('test', true);
2121
$application = new Application($kernel);
@@ -46,4 +46,58 @@ public function testRequestBodyGeneration(): void
4646
$this->assertArrayHasKey('required', $arrayFromYaml['documentation']['components']['requestBodies']['DummyObject']['content']['application/json']['schema']);
4747
$this->assertArrayHasKey('properties', $arrayFromYaml['documentation']['components']['requestBodies']['DummyObject']['content']['application/json']['schema']);
4848
}
49+
50+
public function testRequestBodyGenerationFromType(): void
51+
{
52+
$kernel = new AppKernelTest('test', true);
53+
$application = new Application($kernel);
54+
$kernel->boot();
55+
56+
$command = $application->find('apidocbundle:component:body');
57+
$commandTester = new CommandTester($command);
58+
$commandTester->execute([
59+
'class' => 'Ehyiah\\ApiDocBundle\\Tests\\Dummy\\DummyType',
60+
]);
61+
62+
$commandTester->assertCommandIsSuccessful();
63+
64+
$output = $commandTester->getDisplay();
65+
$this->assertStringContainsString('File generated', $output);
66+
67+
$filePath = $kernel->getProjectDir() . '/var/Swagger/requestBodies/DummyType.yaml';
68+
$arrayFromYaml = Yaml::parseFile($filePath);
69+
70+
$this->assertIsArray($arrayFromYaml);
71+
$this->assertArrayHasKey('documentation', $arrayFromYaml);
72+
$this->assertArrayHasKey('components', $arrayFromYaml['documentation']);
73+
$this->assertArrayHasKey('requestBodies', $arrayFromYaml['documentation']['components']);
74+
$this->assertArrayHasKey('DummyType', $arrayFromYaml['documentation']['components']['requestBodies']);
75+
$this->assertArrayHasKey('required', $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']);
76+
$this->assertArrayHasKey('content', $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']);
77+
$this->assertArrayHasKey('application/json', $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']['content']);
78+
$this->assertArrayHasKey('schema', $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']['content']['application/json']);
79+
$this->assertArrayHasKey('properties', $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']['content']['application/json']['schema']);
80+
81+
$properties = $arrayFromYaml['documentation']['components']['requestBodies']['DummyType']['content']['application/json']['schema']['properties'];
82+
$this->assertArrayHasKey('textField', $properties);
83+
$this->assertEquals('string', $properties['textField']['type']);
84+
$this->assertArrayHasKey('NumberField', $properties);
85+
$this->assertEquals('number', $properties['NumberField']['type']);
86+
$this->assertArrayHasKey('integerField', $properties);
87+
$this->assertEquals('integer', $properties['integerField']['type']);
88+
$this->assertArrayHasKey('dateField', $properties);
89+
$this->assertEquals('string', $properties['dateField']['type']);
90+
$this->assertEquals('date', $properties['dateField']['format']);
91+
$this->assertArrayHasKey('datetimeField', $properties);
92+
$this->assertEquals('string', $properties['datetimeField']['type']);
93+
$this->assertEquals('date-time', $properties['datetimeField']['format']);
94+
$this->assertArrayHasKey('choiceMultipleField', $properties);
95+
$this->assertEquals('array', $properties['choiceMultipleField']['type']);
96+
$this->assertArrayHasKey('enum', $properties['choiceMultipleField']);
97+
$this->assertArrayHasKey('choiceNotMultipleField', $properties);
98+
$this->assertEquals('string', $properties['choiceNotMultipleField']['type']);
99+
$this->assertArrayHasKey('enum', $properties['choiceNotMultipleField']);
100+
$this->assertArrayHasKey('collectionField', $properties);
101+
$this->assertEquals('array', $properties['collectionField']['type']);
102+
}
49103
}

tests/Dummy/DummyNumberType.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Ehyiah\ApiDocBundle\Tests\Dummy;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
final class DummyNumberType extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options): void
12+
{
13+
$builder
14+
->add('subNumberField', NumberType::class)
15+
;
16+
}
17+
}

tests/Dummy/DummyType.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Ehyiah\ApiDocBundle\Tests\Dummy;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
7+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
9+
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
10+
use Symfony\Component\Form\Extension\Core\Type\DateType;
11+
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
12+
use Symfony\Component\Form\Extension\Core\Type\NumberType;
13+
use Symfony\Component\Form\Extension\Core\Type\TextType;
14+
use Symfony\Component\Form\FormBuilderInterface;
15+
16+
final class DummyType extends AbstractType
17+
{
18+
public function buildForm(FormBuilderInterface $builder, array $options): void
19+
{
20+
$builder
21+
->add('textField', TextType::class)
22+
->add('NumberField', NumberType::class)
23+
->add('integerField', IntegerType::class)
24+
->add('dateField', DateType::class)
25+
->add('datetimeField', DateTimeType::class)
26+
->add('birthDayField', BirthdayType::class)
27+
->add('choiceMultipleField', ChoiceType::class, [
28+
'choices' => ['choice-1', 'choice-2', 'choice-3'],
29+
'multiple' => true,
30+
])
31+
->add('choiceNotMultipleField', ChoiceType::class, [
32+
'choices' => ['choice-1', 'choice-2', 'choice-3'],
33+
'multiple' => false,
34+
])
35+
->add('collectionField', CollectionType::class, [
36+
'entry_type' => DummyNumberType::class,
37+
])
38+
;
39+
}
40+
}

0 commit comments

Comments
 (0)