Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/JsonSchema/DefinitionNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ public function create(string $className, string $format = 'json', ?string $inpu
}

if (null !== $inputOrOutputClass && $className !== $inputOrOutputClass) {
$parts = explode('\\', $inputOrOutputClass);
$shortName = end($parts);
$prefix .= self::GLUE.$shortName;
// Use createPrefixFromClass so DTOs with identical short names but different
// FQCNs (e.g. App\...\Input\ThingCreate and App\...\Output\ThingCreate) get
// disambiguated suffixes instead of overwriting each other in the schema map.
$prefix .= self::GLUE.$this->createPrefixFromClass($inputOrOutputClass);
}

// TODO: remove in 5.0
Expand Down
32 changes: 32 additions & 0 deletions src/JsonSchema/Tests/DefinitionNameFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

use ApiPlatform\JsonSchema\DefinitionNameFactory;
use ApiPlatform\JsonSchema\SchemaFactory;
use ApiPlatform\JsonSchema\Tests\Fixtures\DefinitionNameFactory\InputOutputCollision\Input\ThingCreate as InputThingCreate;
use ApiPlatform\JsonSchema\Tests\Fixtures\DefinitionNameFactory\InputOutputCollision\Output\ThingCreate as OutputThingCreate;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\DtoOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -146,6 +149,35 @@ public function testCreateDifferentPrefixesForClassesWithTheSameShortName(): voi
);
}

public function testCreateDistinctDefinitionNamesWhenInputAndOutputShareShortName(): void
{
$definitionNameFactory = new DefinitionNameFactory();

$operation = new Post(class: Dummy::class, shortName: 'Thing');

$inputName = $definitionNameFactory->create(
Dummy::class,
'jsonld',
InputThingCreate::class,
$operation,
['schema_type' => \ApiPlatform\JsonSchema\Schema::TYPE_INPUT]
);

$outputName = $definitionNameFactory->create(
Dummy::class,
'jsonld',
OutputThingCreate::class,
$operation,
['schema_type' => \ApiPlatform\JsonSchema\Schema::TYPE_OUTPUT]
);

self::assertNotSame(
$inputName,
$outputName,
'Input and Output DTO classes sharing the same short name must produce distinct definition names.'
);
}

public function testCreateDifferentPrefixesForClassesWithTheSameOperationShortName(): void
{
$definitionNameFactory = new DefinitionNameFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

declare(strict_types=1);

namespace ApiPlatform\JsonSchema\Tests\Fixtures\DefinitionNameFactory\InputOutputCollision\Input;

class ThingCreate
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

declare(strict_types=1);

namespace ApiPlatform\JsonSchema\Tests\Fixtures\DefinitionNameFactory\InputOutputCollision\Output;

class ThingCreate
{
}
Loading