Skip to content

Commit 834d183

Browse files
committed
feat: 🎸 refer to the 'json' schema and separate the schema for input and output
1 parent 7d61721 commit 834d183

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

‎src/Hydra/JsonSchema/SchemaFactory.php‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function __construct(
104104
*/
105105
public function buildSchema(string $className, string $format = 'jsonld', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
106106
{
107-
if ('jsonld' !== $format || 'input' === $type) {
107+
if ('jsonld' !== $format) {
108108
return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
109109
}
110110
if (!$this->isResourceClass($className)) {
@@ -122,8 +122,6 @@ public function buildSchema(string $className, string $format = 'jsonld', string
122122
return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
123123
}
124124

125-
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
126-
127125
// JSON-LD is slightly different then JSON:API or HAL
128126
// All the references that are resources must also be in JSON-LD therefore combining
129127
// the HydraItemBaseSchema and the JSON schema is harder (unless we loop again through all relationship)
@@ -132,10 +130,23 @@ public function buildSchema(string $className, string $format = 'jsonld', string
132130
$definitions = $schema->getDefinitions();
133131

134132
$prefix = $this->getSchemaUriPrefix($schema->getVersion());
133+
$key = $schema->getRootDefinitionKey();
135134
$collectionKey = $schema->getItemsDefinitionKey();
136-
$key = $schema->getRootDefinitionKey() ?? $collectionKey;
135+
136+
// remove the original definition, it will be replaced by the input or output one
137+
if (isset($key)) {
138+
unset($definitions[$key]);
139+
}
140+
141+
$definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
142+
$definitionName .= sprintf('.%s', $type);
143+
144+
$jsonSchema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, null, $serializerContext, $forceCollection);
145+
$jsonKey = $jsonSchema->getRootDefinitionKey() ?? $jsonSchema->getItemsDefinitionKey();
137146

138147
if (!$collectionKey) {
148+
$schema['$ref'] = $prefix.$definitionName;
149+
139150
if ($this->transformed[$definitionName] ?? false) {
140151
return $schema;
141152
}
@@ -150,18 +161,16 @@ public function buildSchema(string $className, string $format = 'jsonld', string
150161

151162
$allOf = new \ArrayObject(['allOf' => [
152163
['$ref' => $prefix.$baseName],
153-
$definitions[$key],
164+
['$ref' => $prefix.$jsonKey],
154165
]]);
155166

156-
if (isset($definitions[$key]['description'])) {
157-
$allOf['description'] = $definitions[$key]['description'];
167+
if (isset($definitions[$jsonKey]['description'])) {
168+
$allOf['description'] = $definitions[$jsonKey]['description'];
158169
}
159170

160171
$definitions[$definitionName] = $allOf;
161172
unset($definitions[$definitionName]['allOf'][1]['description']);
162173

163-
$schema['$ref'] = $prefix.$definitionName;
164-
165174
$this->transformed[$definitionName] = true;
166175

167176
return $schema;

0 commit comments

Comments
 (0)