19
19
use ApiPlatform \Core \Api \IriConverterInterface as LegacyIriConverterInterface ;
20
20
use ApiPlatform \Core \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
21
21
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
22
+ use ApiPlatform \JsonLd \AnonymousContextBuilderInterface ;
22
23
use ApiPlatform \JsonLd \ContextBuilderInterface ;
23
24
use ApiPlatform \Metadata \HttpOperation ;
24
25
use ApiPlatform \Serializer \AbstractItemNormalizer ;
@@ -74,17 +75,23 @@ public function supportsNormalization($data, $format = null, array $context = []
74
75
*/
75
76
public function normalize ($ object , $ format = null , array $ context = [])
76
77
{
77
- $ objectClass = $ this ->getObjectClass ($ object );
78
- $ outputClass = $ this -> getOutputClass ( $ objectClass , $ context );
79
- if (null !== $ outputClass && !isset ($ context [self ::IS_TRANSFORMED_TO_SAME_CLASS ])) {
78
+ $ resourceClass = $ this ->getObjectClass ($ object );
79
+
80
+ if ($ outputClass = $ this -> getOutputClass ( $ resourceClass , $ context ) && !isset ($ context [self ::IS_TRANSFORMED_TO_SAME_CLASS ])) {
80
81
return parent ::normalize ($ object , $ format , $ context );
81
82
}
82
83
83
84
// TODO: we should not remove the resource_class in the normalizeRawCollection as we would find out anyway that it's not the same as the requested one
84
85
$ previousResourceClass = $ context ['resource_class ' ] ?? null ;
85
- $ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ object , $ context ['resource_class ' ] ?? null );
86
- $ context = $ this ->initContext ($ resourceClass , $ context );
87
- $ metadata = $ this ->addJsonLdContext ($ this ->contextBuilder , $ resourceClass , $ context );
86
+ $ metadata = [];
87
+ if ($ isResourceClass = $ this ->resourceClassResolver ->isResourceClass ($ resourceClass )) {
88
+ $ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ object , $ resourceClass );
89
+ $ context = $ this ->initContext ($ resourceClass , $ context );
90
+ $ metadata = $ this ->addJsonLdContext ($ this ->contextBuilder , $ resourceClass , $ context );
91
+ } elseif ($ this ->contextBuilder instanceof AnonymousContextBuilderInterface) {
92
+ // We should improve what's behind the context creation, its probably more complicated then it should
93
+ $ metadata = $ this ->createJsonLdContext ($ this ->contextBuilder , $ object , $ context );
94
+ }
88
95
89
96
if (isset ($ context ['operation ' ]) && $ previousResourceClass !== $ resourceClass ) {
90
97
unset($ context ['operation ' ], $ context ['operation_name ' ]);
@@ -96,8 +103,11 @@ public function normalize($object, $format = null, array $context = [])
96
103
$ iri = $ this ->iriConverter ->getIriFromResource ($ object , UrlGeneratorInterface::ABS_PATH , $ context ['operation ' ] ?? null , $ context );
97
104
}
98
105
99
- $ context ['iri ' ] = $ iri ;
100
- $ metadata ['@id ' ] = $ iri ;
106
+ if (isset ($ iri )) {
107
+ $ context ['iri ' ] = $ iri ;
108
+ $ metadata ['@id ' ] = $ iri ;
109
+ }
110
+
101
111
$ context ['api_normalize ' ] = true ;
102
112
103
113
$ data = parent ::normalize ($ object , $ format , $ context );
0 commit comments