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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

93.75
/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) {
789✔
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
            $name = $groups ? \sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix;
166✔
67
        }
68

69
        if (false === ($serializerContext['gen_id'] ?? true)) {
166✔
70
            $name .= '_noid';
4✔
71
        }
72

73
        if ('merge-patch+json' === $format) {
166✔
74
            $name .= self::GLUE.self::JSON_MERGE_PATCH_SCHEMA_POSTFIX;
42✔
75
        }
76

77
        return $this->encodeDefinitionName($name);
166✔
78
    }
79

80
    private function encodeDefinitionName(string $name): string
81
    {
82
        return preg_replace('/[^a-zA-Z0-9.\-_]/', '.', $name);
166✔
83
    }
84

85
    private function createPrefixFromClass(string $fullyQualifiedClassName, int $namespaceParts = 1): string
86
    {
87
        $parts = explode('\\', $fullyQualifiedClassName);
92✔
88
        $name = implode(self::GLUE, \array_slice($parts, -$namespaceParts));
92✔
89

90
        if (!isset($this->prefixCache[$name])) {
92✔
91
            $this->prefixCache[$name] = $fullyQualifiedClassName;
92✔
92

93
            return $name;
92✔
94
        }
95

96
        if ($this->prefixCache[$name] !== $fullyQualifiedClassName) {
44✔
97
            $name = $this->createPrefixFromClass($fullyQualifiedClassName, ++$namespaceParts);
×
98
        }
99

100
        return $name;
44✔
101
    }
102
}
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