Skip to content

Commit ad9be8a

Browse files
committed
fix: šŸ› if resource doesn't have 'json' schema, embed it instead of referencing it
It cannot always be referenced, as there may be resources that don't have a JSON schema but only a JSON-LD schema. It's not certain at the time the JSON-LD schema is being generated whether a JSON schema will ultimately be defined. Therefore, the JSON schema will be referenced only if it's already defined.
1 parent cfc217e commit ad9be8a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

ā€Žsrc/Hydra/JsonSchema/SchemaFactory.phpā€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ public function buildSchema(string $className, string $format = 'jsonld', string
137137
$definitionName .= '.input';
138138
}
139139

140-
$jsonSchema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, null, $serializerContext, $forceCollection);
140+
$jsonSchema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, new Schema(version: $schema->getVersion()), $serializerContext, $forceCollection);
141141
$jsonKey = $jsonSchema->getRootDefinitionKey() ?? $jsonSchema->getItemsDefinitionKey();
142+
$jsonDefinition = $jsonSchema->getDefinitions()[$jsonKey] ?? null;
142143

143144
if (!$collectionKey) {
144145
$schema['$ref'] = $prefix.$definitionName;
@@ -157,7 +158,10 @@ public function buildSchema(string $className, string $format = 'jsonld', string
157158

158159
$allOf = new \ArrayObject(['allOf' => [
159160
['$ref' => $prefix.$baseName],
160-
['$ref' => $prefix.$jsonKey],
161+
// It cannot always be referenced, as there may be resources that don't have a JSON schema but only a JSON-LD schema.
162+
// It's not certain at the time the JSON-LD schema is being generated whether a JSON schema will ultimately be defined.
163+
// Therefore, the JSON schema will be referenced only if it's already defined.
164+
isset($definitions[$jsonKey]) ? ['$ref' => $prefix.$jsonKey] : $jsonDefinition,
161165
]]);
162166

163167
if (isset($definitions[$jsonKey]['description'])) {

0 commit comments

Comments
Ā (0)