Skip to content

Commit e52e825

Browse files
Cafeine42Thibaut Cholley
andauthored
fix(openapi): allow assertMatchesJsonSchema with custom output dto (#7438)
Co-authored-by: Thibaut Cholley <[email protected]>
1 parent d1ca69a commit e52e825

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/Hydra/JsonSchema/SchemaFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ public function buildSchema(string $className, string $format = 'jsonld', string
142142

143143
$baseName = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_NAME : self::ITEM_BASE_SCHEMA_OUTPUT_NAME;
144144

145-
if ($this->isResourceClass($inputOrOutputClass)) {
146-
if (!isset($definitions[$baseName])) {
147-
$definitions[$baseName] = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_OUTPUT : self::ITEM_BASE_SCHEMA;
148-
}
145+
if (!isset($definitions[$baseName])) {
146+
$definitions[$baseName] = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_OUTPUT : self::ITEM_BASE_SCHEMA;
149147
}
150148

151149
$allOf = new \ArrayObject(['allOf' => [

tests/Fixtures/TestBundle/Entity/DummyDtoCustom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
new Delete(),
3636
new Post(input: CustomInputDto::class, processor: CustomInputDtoProcessor::class),
3737
new GetCollection(),
38-
new GetCollection(output: CustomOutputDto::class, uriTemplate: 'dummy_dto_custom_output', provider: CustomOutputDtoProvider::class),
38+
new GetCollection(output: CustomOutputDto::class, uriTemplate: 'dummy_dto_custom_output', provider: CustomOutputDtoProvider::class, name: 'dummy_dto_custom_output_collection'),
3939
new Get(output: CustomOutputDto::class, uriTemplate: 'dummy_dto_custom_output/{id}', provider: CustomOutputDtoProvider::class),
4040
new Post(output: false, uriTemplate: 'dummy_dto_custom_post_without_output'),
4141
]

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Address;
2121
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DirectMercure;
2222
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
23+
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoCustom;
2324
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyDtoInputOutput;
2425
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6041\NumericValidated;
2526
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6146\Issue6146Child;
@@ -193,6 +194,13 @@ public function testAssertMatchesResourceItemJsonSchema(string $format, string $
193194
$this->assertMatchesResourceItemJsonSchema(ResourceInterface::class, format: $format);
194195
}
195196

197+
#[\PHPUnit\Framework\Attributes\DataProvider('providerFormats')]
198+
public function testAssertMatchesResourceItemJsonSchemaWithCustomOutput(string $format, string $mimeType): void
199+
{
200+
self::createClient()->request('GET', '/dummy_dto_custom_output/some-id', ['headers' => ['Accept' => $mimeType]]);
201+
$this->assertMatchesResourceItemJsonSchema(DummyDtoCustom::class, operationName: 'dummy_dto_custom_output_collection', format: $format);
202+
}
203+
196204
#[\PHPUnit\Framework\Attributes\DataProvider('providerFormats')]
197205
#[\PHPUnit\Framework\Attributes\Group('orm')]
198206
public function testAssertMatchesResourceItemJsonSchemaWithCustomJson(string $format, string $mimeType): void

0 commit comments

Comments
 (0)