• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

onmoon / openapi-server-bundle / 15881876638

25 Jun 2025 04:28PM UTC coverage: 80.585% (-0.5%) from 81.095%
15881876638

Pull #196

github

web-flow
Merge 90ce8070d into c35fba5f2
Pull Request #196: Fix minimum versions install

9 of 29 new or added lines in 5 files covered. (31.03%)

44 existing lines in 12 files now uncovered.

1378 of 1710 relevant lines covered (80.58%)

3.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.5
/src/Serializer/ArrayDtoSerializer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace OnMoon\OpenApiServerBundle\Serializer;
6

7
use OnMoon\OpenApiServerBundle\Interfaces\Dto;
8
use OnMoon\OpenApiServerBundle\Specification\Definitions\ObjectSchema;
9
use OnMoon\OpenApiServerBundle\Specification\Definitions\Operation;
10
use OnMoon\OpenApiServerBundle\Types\ScalarTypesResolver;
11
use Symfony\Component\HttpFoundation\Request;
12

13
use function array_key_exists;
14
use function array_map;
15
use function Safe\json_decode;
16

17
final class ArrayDtoSerializer implements DtoSerializer
18
{
19
    private ScalarTypesResolver $resolver;
20
    private bool $sendNotRequiredNullableNulls;
21

22
    public function __construct(ScalarTypesResolver $resolver, bool $sendNulls)
23
    {
24
        $this->resolver                     = $resolver;
16✔
25
        $this->sendNotRequiredNullableNulls = $sendNulls;
16✔
26
    }
27

28
    public function createRequestDto(
29
        Request $request,
30
        Operation $operation,
31
        string $inputDtoFQCN
32
    ): Dto {
33
        /** @var mixed[] $input */
34
        $input  = [];
7✔
35
        $params = $operation->getRequestParameters();
7✔
36
        if (array_key_exists('query', $params)) {
7✔
37
            /** @var string[] $source */
38
            $source                   = $request->query->all();
7✔
39
            $input['queryParameters'] = $this->convert(true, $source, $params['query']);
7✔
40
        }
41

42
        if (array_key_exists('path', $params)) {
7✔
43
            /** @var string[] $source */
44
            $source                  = (array) $request->attributes->get('_route_params', []);
7✔
45
            $input['pathParameters'] = $this->convert(true, $source, $params['path']);
7✔
46
        }
47

48
        $bodyType = $operation->getRequestBody();
7✔
49
        if ($bodyType !== null) {
7✔
50
            $source = $request->getContent();
7✔
51

52
            /** @var mixed[] $rawBody */
53
            $rawBody       = json_decode($source, true);
7✔
54
            $input['body'] = $this->convert(true, $rawBody, $bodyType->getSchema());
7✔
55
        }
56

57
        /** @var Dto $inputDto */
58
        $inputDto = $inputDtoFQCN::{'fromArray'}($input);
7✔
59

60
        return $inputDto;
7✔
61
    }
62

63
    /** @inheritDoc */
64
    public function createResponseFromDto(Dto $responseDto, ObjectSchema $definition): array
65
    {
66
        return $this->convert(false, $responseDto->toArray(), $definition);
9✔
67
    }
68

69
    /**
70
     * @param mixed[] $source
71
     *
72
     * @return mixed[]
73
     *
74
     * @psalm-suppress MissingClosureReturnType
75
     * @psalm-suppress MissingParamType
76
     * @psalm-suppress MixedArgument
77
     * @psalm-suppress MixedAssignment
78
     */
79
    private function convert(bool $deserialize, array $source, ObjectSchema $params): array
80
    {
81
        $result = [];
16✔
82
        foreach ($params->getProperties() as $property) {
16✔
83
            $name = $property->getName();
16✔
84
            if ($deserialize && ! array_key_exists($name, $source)) {
16✔
85
                $result[$name] = $property->getDefaultValue();
7✔
86
                continue;
7✔
87
            }
88

89
            if (! $deserialize && $source[$name] === null) {
14✔
90
                $value =  $property->getDefaultValue();
8✔
91
                if (
92
                    $property->isRequired() || $value !== null ||
8✔
93
                    ($this->sendNotRequiredNullableNulls && $property->isNullable())
8✔
94
                ) {
95
                    $result[$name] = $value;
5✔
96
                }
97

98
                continue;
8✔
99
            }
100

101
            $typeId     = $property->getScalarTypeId();
7✔
102
            $objectType = $property->getObjectTypeDefinition();
7✔
103

104
            if ($objectType !== null) {
7✔
105
                /** @psalm-suppress MissingClosureParamType */
106
                $converter = fn ($v) => $this->convert($deserialize, $v, $objectType->getSchema());
5✔
107
            } else {
108
                $outputClass = $property->getOutputType();
6✔
109

110
                /** @psalm-suppress MissingClosureParamType */
111
                $converter = fn ($v) => $this->resolver->convert($deserialize, $typeId ?? 0, $v, $outputClass);
6✔
112
            }
113

114
            if ($property->isArray()) {
7✔
115
                /** @psalm-suppress MissingClosureParamType */
UNCOV
116
                $converter = static fn ($v) => array_map(static fn ($i) => $converter($i), $v);
×
117
            }
118

119
            $result[$name] = $converter($source[$name]);
7✔
120
        }
121

122
        return $result;
16✔
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc