@@ -33,11 +33,21 @@ protected function extractResources(SpecObjectInterface $specObject): array
3333 continue ;
3434 }
3535
36+ $ response = $ route ->responses ->{201 } ?? $ route ->responses ->{200 } ?? null ;
37+ if (!$ response ) {
38+ continue ;
39+ }
40+
41+ $ responseSchema = $ response ->content ?->{'application/json ' }?->schema ?? null ;
42+ if (!$ responseSchema ) {
43+ continue ;
44+ }
45+
3646 $ handler = $ this ->routeHandlerParser ->parse ($ route ->{'x-lg-handler ' });
3747
3848 try {
3949 $ namespace = $ this ->getReplacedNamespace ($ handler ->namespace , $ replaceFrom , $ replaceTo );
40- $ className = $ route ->{'x-lg-resource-class-name ' } ?? $ this ->getReplacedClassName ($ handler ->class , $ replaceFrom , $ replaceTo );
50+ $ className = $ responseSchema ->{'x-lg-resource-class-name ' } ?? $ this ->getReplacedClassName ($ handler ->class , $ replaceFrom , $ replaceTo );
4151 } catch (RuntimeException ) {
4252 continue ;
4353 }
@@ -46,31 +56,35 @@ protected function extractResources(SpecObjectInterface $specObject): array
4656 continue ;
4757 }
4858
49- $ response = $ route ->responses ->{201 } ?? $ route ->responses ->{200 } ?? null ;
50- if (!$ response ) {
51- continue ;
52- }
53-
54- $ responseData = $ response ->content ?->{'application/json ' }?->schema?->properties ?? null ;
55-
56- $ responseKey = $ route ->{'x-lg-resource-response-key ' } ?? null ;
57- if (!$ responseKey ) {
58- $ responseKey = $ this ->options ['response_key ' ] ?? '' ;
59- }
60-
61- $ responseKeyParts = explode ('. ' , $ responseKey );
62- foreach ($ responseKeyParts as $ responseKeyPart ) {
63- $ responseData = $ responseData ?->$ responseKeyPart ?? null ;
59+ $ responseData = $ responseSchema ;
60+
61+ $ responseKey = $ responseSchema ->{'x-lg-resource-response-key ' } ??
62+ $ this ->options ['resources ' ]['response_key ' ] ??
63+ null ;
64+ if ($ responseKey ) {
65+ $ responseKeyParts = explode ('. ' , $ responseKey );
66+ foreach ($ responseKeyParts as $ responseKeyPart ) {
67+ $ flag = false ;
68+ do_with_all_of ($ responseData , function (stdClass $ p ) use (&$ responseData , &$ flag , $ responseKeyPart ) {
69+ if (std_object_has ($ p , 'properties ' )) {
70+ if (std_object_has ($ p ->properties , $ responseKeyPart )) {
71+ $ responseData = $ p ->properties ->$ responseKeyPart ;
72+ $ flag = true ;
73+ }
74+ }
75+ });
76+ if (!$ flag ) {
77+ $ responseData = null ;
78+
79+ break ;
80+ }
81+ }
6482 }
6583
6684 if (!$ responseData ) {
6785 continue ;
6886 }
6987
70- if (isset ($ responseData ->type ) && $ responseData ->type == 'array ' ) {
71- $ responseData = $ responseData ->items ;
72- }
73-
7488 $ properties = $ this ->convertToString ($ this ->getProperties ($ responseData ));
7589
7690 if (empty ($ properties )) {
@@ -103,27 +117,21 @@ protected function createResourcesFiles(array $resources, string $template): voi
103117 }
104118 }
105119
106- private function getProperties (stdClass $ responseData ): array
120+ private function getProperties (stdClass $ object ): array
107121 {
108- if (isset ($ responseData ->allOf )) {
109- $ properties = [];
122+ $ properties = [];
110123
111- /** @var stdClass $partResponseData */
112- foreach ($ responseData ->allOf as $ partResponseData ) {
113- if (!isset ($ partResponseData ->properties )) {
114- continue ;
115- }
116- $ properties = array_merge ($ properties , $ this ->getProperties ($ partResponseData ));
124+ do_with_all_of ($ object , function (stdClass $ p ) use (&$ properties ) {
125+ if (std_object_has ($ p , 'properties ' )) {
126+ $ properties = array_merge ($ properties , array_keys (get_object_vars ($ p ->properties )));
117127 }
118128
119- return $ properties ;
120- }
121-
122- if (!isset ($ responseData ->properties )) {
123- return [];
124- }
129+ if (std_object_has ($ p , 'items ' )) {
130+ $ properties = array_merge ($ properties , $ this ->getProperties ($ p ->items ));
131+ }
132+ });
125133
126- return array_keys ( get_object_vars ( $ responseData -> properties )) ;
134+ return $ properties ;
127135 }
128136
129137 private function convertToString (array $ properties ): string
0 commit comments