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

api-platform / core / 20528769615

26 Dec 2025 08:17PM UTC coverage: 25.119%. First build
20528769615

Pull #7629

github

web-flow
Merge 4691c25d0 into 38d474d1b
Pull Request #7629: fix: add support for normalization/denormalization with attributes

24 of 236 new or added lines in 8 files covered. (10.17%)

14638 of 58274 relevant lines covered (25.12%)

29.5 hits per line

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

71.43
/src/JsonSchema/DefinitionNameFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\JsonSchema;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
18
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
19

20
final class DefinitionNameFactory implements DefinitionNameFactoryInterface
21
{
22
    use ResourceClassInfoTrait;
23

24
    private const GLUE = '.';
25
    private const JSON_MERGE_PATCH_SCHEMA_POSTFIX = 'jsonMergePatch';
26

27
    private array $prefixCache = [];
28

29
    public function __construct(private ?array $distinctFormats = null)
30
    {
31
        if ($distinctFormats) {
821✔
32
            trigger_deprecation('api-platform/json-schema', '4.2', 'The distinctFormats argument is deprecated and will be removed in 5.0.');
×
33
        }
34
    }
35

36
    public function create(string $className, string $format = 'json', ?string $inputOrOutputClass = null, ?Operation $operation = null, array $serializerContext = []): string
37
    {
38
        if ($operation) {
166✔
39
            $prefix = $operation->getShortName();
150✔
40
        }
41

42
        if (!isset($prefix)) {
166✔
43
            $prefix = $this->createPrefixFromClass($className);
92✔
44
        }
45

46
        if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
166✔
47
            $parts = explode('\\', $inputOrOutputClass);
36✔
48
            $shortName = end($parts);
36✔
49
            $prefix .= self::GLUE.$shortName;
36✔
50
        }
51

52
        // TODO: remove in 5.0
53
        $v = $this->distinctFormats ? ($this->distinctFormats[$format] ?? false) : true;
166✔
54

55
        if (!\in_array($format, ['json', 'merge-patch+json'], true) && $v) {
166✔
56
            // JSON is the default, and so isn't included in the definition name
57
            // JSON merge patch is postfixed at the end
58
            $prefix .= self::GLUE.$format;
152✔
59
        }
60

61
        $definitionName = $serializerContext[SchemaFactory::OPENAPI_DEFINITION_NAME] ?? null;
166✔
62
        if (null !== $definitionName) {
166✔
63
            $name = \sprintf('%s%s', $prefix, $definitionName ? '-'.$definitionName : $definitionName);
46✔
64
        } else {
65
            $groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []);
166✔
66
            $attributes = (array) ($serializerContext[AbstractNormalizer::ATTRIBUTES] ?? []);
166✔
67

68
            $parts = [];
166✔
69

70
            if ($groups) {
166✔
71
                $parts[] = implode('_', $groups);
70✔
72
            }
73

74
            if ($attributes) {
166✔
NEW
75
                $parts[] = $this->getAttributesAsString($attributes);
×
76
            }
77

78
            $name = $parts ? \sprintf('%s-%s', $prefix, implode('_', $parts)) : $prefix;
166✔
79
        }
80

81
        if (false === ($serializerContext['gen_id'] ?? true)) {
166✔
82
            $name .= '_noid';
4✔
83
        }
84

85
        if ('merge-patch+json' === $format) {
166✔
86
            $name .= self::GLUE.self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
42✔
87
        }
88

89
        return $this->encodeDefinitionName($name);
166✔
90
    }
91

92
    private function encodeDefinitionName(string $name): string
93
    {
94
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
166✔
95
    }
96

97
    private function createPrefixFromClass(string $fullyQualifiedClassName, int $namespaceParts = 1): string
98
    {
99
        $parts = explode('\\', $fullyQualifiedClassName);
92✔
100
        $name = implode(self::GLUE, \array_slice($parts, -$namespaceParts));
92✔
101

102
        if (!isset($this->prefixCache[$name])) {
92✔
103
            $this->prefixCache[$name] = $fullyQualifiedClassName;
92✔
104

105
            return $name;
92✔
106
        }
107

108
        if ($this->prefixCache[$name] !== $fullyQualifiedClassName) {
44✔
109
            $name = $this->createPrefixFromClass($fullyQualifiedClassName, ++$namespaceParts);
×
110
        }
111

112
        return $name;
44✔
113
    }
114

115
    private function getAttributesAsString(array $attributes): string
116
    {
NEW
117
        $parts = [];
×
118

NEW
119
        foreach ($attributes as $key => $value) {
×
NEW
120
            if (\is_array($value)) {
×
NEW
121
                $childString = $this->getAttributesAsString($value);
×
NEW
122
                $children = explode('_', $childString);
×
123

NEW
124
                foreach ($children as $child) {
×
NEW
125
                    $parts[] = $key.'.'.$child;
×
126
                }
NEW
127
            } elseif (\is_string($key)) {
×
NEW
128
                $parts[] = $key.'_'.$value;
×
129
            } else {
NEW
130
                $parts[] = $value;
×
131
            }
132
        }
133

NEW
134
        return implode('_', $parts);
×
135
    }
136
}
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