Skip to content

Commit 97f7dff

Browse files
fix(jsonschema): correct _embedded property schema output generation
1 parent a303524 commit 97f7dff

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/Hal/JsonSchema/SchemaFactory.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,21 @@ public function buildSchema(string $className, string $format = 'jsonhal', strin
7878
$schema['type'] = 'object';
7979
$schema['properties'] = [
8080
'_embedded' => [
81-
'type' => 'array',
82-
'items' => $items,
81+
'anyOf' => [
82+
[
83+
'type' => 'object',
84+
'properties' => [
85+
'items' => [
86+
'type' => 'array',
87+
'items' => $items,
88+
],
89+
],
90+
],
91+
[
92+
'type' => 'array',
93+
'items' => $items,
94+
],
95+
],
8396
],
8497
'totalItems' => [
8598
'type' => 'integer',

tests/Symfony/Bundle/Test/ApiTestCaseTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,20 @@ public function testAssertMatchesJsonSchema(): void
122122
$this->assertMatchesJsonSchema(json_decode($jsonSchema, true));
123123
}
124124

125-
public function testAssertMatchesResourceCollectionJsonSchema(): void
125+
public static function providerFormats(): iterable
126126
{
127-
self::createClient()->request('GET', '/resource_interfaces');
128-
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class);
127+
// yield 'jsonapi' => ['jsonapi', 'application/vnd.api+json'];
128+
yield 'jsonhal' => ['jsonhal', 'application/hal+json'];
129+
yield 'jsonld' => ['jsonld', 'application/ld+json'];
130+
}
131+
132+
/**
133+
* @dataProvider providerFormats
134+
*/
135+
public function testAssertMatchesResourceCollectionJsonSchema(string $format, string $mimeType): void
136+
{
137+
self::createClient()->request('GET', '/resource_interfaces', ['headers' => ['Accept' => $mimeType]]);
138+
$this->assertMatchesResourceCollectionJsonSchema(ResourceInterface::class, format: $format);
129139
}
130140

131141
public function testAssertMatchesResourceCollectionJsonSchemaKeepSerializationContext(): void
@@ -173,7 +183,10 @@ public function testAssertMatchesResourceItemJsonSchemaWithCustomJson(): void
173183
$this->assertMatchesResourceItemJsonSchema(JsonSchemaContextDummy::class);
174184
}
175185

176-
public function testAssertMatchesResourceItemJsonSchemaOutput(): void
186+
/**
187+
* @dataProvider providerFormats
188+
*/
189+
public function testAssertMatchesResourceItemJsonSchemaOutput(string $format, string $mimeType): void
177190
{
178191
$this->recreateSchema();
179192

@@ -184,8 +197,8 @@ public function testAssertMatchesResourceItemJsonSchemaOutput(): void
184197
$dummyDtoInputOutput->num = 54;
185198
$manager->persist($dummyDtoInputOutput);
186199
$manager->flush();
187-
self::createClient()->request('GET', '/dummy_dto_input_outputs/1');
188-
$this->assertMatchesResourceItemJsonSchema(DummyDtoInputOutput::class);
200+
self::createClient()->request('GET', '/dummy_dto_input_outputs/1', ['headers' => ['Accept' => $mimeType]]);
201+
$this->assertMatchesResourceItemJsonSchema(DummyDtoInputOutput::class, format: $format);
189202
}
190203

191204
public function testAssertMatchesResourceItemAndCollectionJsonSchemaOutputWithContext(): void

0 commit comments

Comments
 (0)