Skip to content

Commit

Permalink
fix(jsonschema): handle @id when genId is false (api-platform#6716)
Browse files Browse the repository at this point in the history
Co-authored-by: soyuka <[email protected]>
  • Loading branch information
ViPErCZ and soyuka authored Oct 14, 2024
1 parent e96623e commit 99262dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
continue;
}

if (false === $propertyMetadata->getGenId()) {
$subDefinitionName = $this->definitionNameFactory->create($className, $format, $className, null, $serializerContext);

if (isset($subSchema->getDefinitions()[$subDefinitionName])) {
unset($subSchema->getDefinitions()[$subDefinitionName]['properties']['@id']);
}
}

if ($isCollection) {
$propertySchema['items']['$ref'] = $subSchema['$ref'];
unset($propertySchema['items']['type']);
Expand Down
11 changes: 11 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,15 @@ public function testResourceWithEnumPropertiesSchema(): void
$properties['genders']
);
}

/**
* Test feature #6716.
*/
public function testGenId(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\Entity\DisableIdGeneration', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$this->assertArrayNotHasKey('@id', $json['definitions']['DisableIdGenerationItem.jsonld']['properties']);
}
}

0 comments on commit 99262dc

Please sign in to comment.