@@ -184,17 +184,21 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
184184 $ relatedDefinitions = [];
185185 foreach ($ properties as $ propertyName => $ property ) {
186186 if ($ relation = $ this ->getRelationship ($ className , $ propertyName , $ serializerContext )) {
187- [$ isOne , $ hasOperations , $ relatedClassName ] = $ relation ;
188- if (false === $ hasOperations ) {
189- continue ;
190- }
187+ [$ isOne , $ relatedClasses ] = $ relation ;
188+ $ refs = [];
189+ foreach ($ relatedClasses as $ relatedClassName => $ hasOperations ) {
190+ if (false === $ hasOperations ) {
191+ continue ;
192+ }
191193
192- $ operation = $ this ->findOperation ($ relatedClassName , $ type , $ operation , $ serializerContext );
193- $ inputOrOutputClass = $ this ->findOutputClass ($ relatedClassName , $ type , $ operation , $ serializerContext );
194- $ serializerContext ??= $ this ->getSerializerContext ($ operation , $ type );
195- $ definitionName = $ this ->definitionNameFactory ->create ($ relatedClassName , $ format , $ inputOrOutputClass , $ operation , $ serializerContext );
196- $ ref = Schema::VERSION_OPENAPI === $ schema ->getVersion () ? '#/components/schemas/ ' .$ definitionName : '#/definitions/ ' .$ definitionName ;
197- $ relatedDefinitions [$ propertyName ] = ['$ref ' => $ ref ];
194+ $ operation = $ this ->findOperation ($ relatedClassName , $ type , $ operation , $ serializerContext );
195+ $ inputOrOutputClass = $ this ->findOutputClass ($ relatedClassName , $ type , $ operation , $ serializerContext );
196+ $ serializerContext ??= $ this ->getSerializerContext ($ operation , $ type );
197+ $ definitionName = $ this ->definitionNameFactory ->create ($ relatedClassName , $ format , $ inputOrOutputClass , $ operation , $ serializerContext );
198+ $ ref = Schema::VERSION_OPENAPI === $ schema ->getVersion () ? '#/components/schemas/ ' .$ definitionName : '#/definitions/ ' .$ definitionName ;
199+ $ refs [$ ref ] = '$ref ' ;
200+ }
201+ $ relatedDefinitions [$ propertyName ] = array_flip ($ refs );
198202 if ($ isOne ) {
199203 $ relationships [$ propertyName ]['properties ' ]['data ' ] = self ::RELATION_PROPS ;
200204 continue ;
@@ -203,7 +207,6 @@ private function buildDefinitionPropertiesSchema(string $key, string $className,
203207 'type ' => 'array ' ,
204208 'items ' => self ::RELATION_PROPS ,
205209 ];
206- continue ;
207210 }
208211 if ('id ' === $ propertyName ) {
209212 $ attributes ['_id ' ] = $ property ;
@@ -264,7 +267,7 @@ private function getRelationship(string $resourceClass, string $property, ?array
264267 $ types = $ propertyMetadata ->getBuiltinTypes () ?? [];
265268 $ isRelationship = false ;
266269 $ isOne = $ isMany = false ;
267- $ className = $ hasOperations = null ;
270+ $ relatedClasses = [] ;
268271
269272 foreach ($ types as $ type ) {
270273 if ($ type ->isCollection ()) {
@@ -281,9 +284,9 @@ private function getRelationship(string $resourceClass, string $property, ?array
281284 $ operation = $ resourceMetadata ->getOperation ();
282285 // @see https://github.com/api-platform/core/issues/5501
283286 // @see https://github.com/api-platform/core/pull/5722
284- $ hasOperations ?? = $ operation ->canRead ();
287+ $ relatedClasses [ $ className ] = $ operation ->canRead ();
285288 }
286289
287- return $ isRelationship ? [$ isOne , $ hasOperations , $ className ] : null ;
290+ return $ isRelationship ? [$ isOne , $ relatedClasses ] : null ;
288291 }
289292}
0 commit comments