@@ -706,6 +706,9 @@ private function processObject($data, Context $options, $path, $result = null)
706706 try {
707707 $ refResult = $ this ->process ($ data , $ options , $ path . '->$ref: ' . $ refString , $ result );
708708 if ($ refResult instanceof ObjectItemContract) {
709+ if ($ refResult ->getFromRefs ()) {
710+ $ refResult = clone $ refResult ; // @todo check performance, consider option
711+ }
709712 $ refResult ->setFromRef ($ refString );
710713 }
711714 $ ref ->setImported ($ refResult );
@@ -1025,24 +1028,39 @@ public function process($data, Context $options, $path = '#', $result = null)
10251028 if ('# ' === $ path ) {
10261029 $ injectDefinitions = new ScopeExit (function () use ($ result , $ options ) {
10271030 foreach ($ options ->exportedDefinitions as $ ref => $ data ) {
1028- JsonPointer::add ($ result , JsonPointer::splitPath ($ ref ), $ data ,
1029- JsonPointer::SKIP_IF_ISSET + JsonPointer::RECURSIVE_KEY_CREATION );
1031+ if ($ data !== null ) {
1032+ JsonPointer::add ($ result , JsonPointer::splitPath ($ ref ), $ data ,
1033+ /*JsonPointer::SKIP_IF_ISSET + */
1034+ JsonPointer::RECURSIVE_KEY_CREATION );
1035+ }
10301036 }
10311037 });
10321038 }
10331039
1034- if (' # ' !== $ path && $ ref = $ data -> getFromRef () ) {
1035- if ( $ ref [ 0 ] === ' # ' ) {
1036- if ( isset ( $ options -> exportedDefinitions [ $ ref ])) {
1037- $ result ->{ self :: PROP_REF } = $ ref ;
1038- return $ result ;
1039- } elseif (!array_key_exists ($ ref , $ options ->exportedDefinitions )) {
1040+ if ($ options -> isRef ) {
1041+ $ options -> isRef = false ;
1042+ } else {
1043+ if ( ' # ' !== $ path && $ refs = $ data -> getFromRefs ()) {
1044+ $ ref = $ refs [ 0 ] ;
1045+ if (!array_key_exists ($ ref , $ options ->exportedDefinitions ) && strpos ( $ ref , ' :// ' ) === false ) {
10401046 $ exported = null ;
10411047 $ options ->exportedDefinitions [$ ref ] = &$ exported ;
1048+ $ options ->isRef = true ;
10421049 $ exported = $ this ->process ($ data , $ options , $ ref );
1043- $ result ->{self ::PROP_REF } = $ ref ;
1044- return $ result ;
1050+ unset($ exported );
1051+ }
1052+
1053+ for ($ i = 1 ; $ i < count ($ refs ); $ i ++) {
1054+ $ ref = $ refs [$ i ];
1055+ if (!array_key_exists ($ ref , $ options ->exportedDefinitions ) && strpos ($ ref , ':// ' ) === false ) {
1056+ $ exported = new \stdClass ();
1057+ $ exported ->{self ::PROP_REF } = $ refs [$ i - 1 ];
1058+ $ options ->exportedDefinitions [$ ref ] = $ exported ;
1059+ }
10451060 }
1061+
1062+ $ result ->{self ::PROP_REF } = $ refs [count ($ refs ) - 1 ];
1063+ return $ result ;
10461064 }
10471065 }
10481066
0 commit comments