@@ -104,7 +104,7 @@ public function __construct(
104
104
*/
105
105
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
106
106
{
107
- if ('jsonld ' !== $ format || ' input ' === $ type ) {
107
+ if ('jsonld ' !== $ format ) {
108
108
return $ this ->schemaFactory ->buildSchema ($ className , $ format , $ type , $ operation , $ schema , $ serializerContext , $ forceCollection );
109
109
}
110
110
if (!$ this ->isResourceClass ($ className )) {
@@ -122,8 +122,6 @@ public function buildSchema(string $className, string $format = 'jsonld', string
122
122
return $ this ->schemaFactory ->buildSchema ($ className , $ format , $ type , $ operation , $ schema , $ serializerContext , $ forceCollection );
123
123
}
124
124
125
- $ definitionName = $ this ->definitionNameFactory ->create ($ className , $ format , $ inputOrOutputClass , $ operation , $ serializerContext );
126
-
127
125
// JSON-LD is slightly different then JSON:API or HAL
128
126
// All the references that are resources must also be in JSON-LD therefore combining
129
127
// 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
132
130
$ definitions = $ schema ->getDefinitions ();
133
131
134
132
$ prefix = $ this ->getSchemaUriPrefix ($ schema ->getVersion ());
133
+ $ key = $ schema ->getRootDefinitionKey ();
135
134
$ 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 ();
137
146
138
147
if (!$ collectionKey ) {
148
+ $ schema ['$ref ' ] = $ prefix .$ definitionName ;
149
+
139
150
if ($ this ->transformed [$ definitionName ] ?? false ) {
140
151
return $ schema ;
141
152
}
@@ -150,18 +161,16 @@ public function buildSchema(string $className, string $format = 'jsonld', string
150
161
151
162
$ allOf = new \ArrayObject (['allOf ' => [
152
163
['$ref ' => $ prefix .$ baseName ],
153
- $ definitions [ $ key ],
164
+ [ ' $ref ' => $ prefix . $ jsonKey ],
154
165
]]);
155
166
156
- if (isset ($ definitions [$ key ]['description ' ])) {
157
- $ allOf ['description ' ] = $ definitions [$ key ]['description ' ];
167
+ if (isset ($ definitions [$ jsonKey ]['description ' ])) {
168
+ $ allOf ['description ' ] = $ definitions [$ jsonKey ]['description ' ];
158
169
}
159
170
160
171
$ definitions [$ definitionName ] = $ allOf ;
161
172
unset($ definitions [$ definitionName ]['allOf ' ][1 ]['description ' ]);
162
173
163
- $ schema ['$ref ' ] = $ prefix .$ definitionName ;
164
-
165
174
$ this ->transformed [$ definitionName ] = true ;
166
175
167
176
return $ schema ;
0 commit comments