22
33namespace Astral \Serialize \OpenApi \Collections ;
44
5- use Astral \Serialize \Enums \TypeKindEnum ;
65use Astral \Serialize \OpenApi \Annotations \Headers ;
76use Astral \Serialize \OpenApi \Annotations \RequestBody ;
87use Astral \Serialize \OpenApi \Annotations \Response ;
@@ -52,7 +51,6 @@ public function build() : Method
5251
5352 $ openAPIMethod ->withRequestBody ($ this ->requestBody !== null ? $ this ->buildRequestBodyByAttribute () : $ this ->buildRequestBodyByParameters ());
5453 $ openAPIMethod ->addResponse (200 , $ this ->buildResponse ());
55-
5654 return $ openAPIMethod ;
5755 }
5856
@@ -62,7 +60,7 @@ public function build() : Method
6260 public function buildRequestBodyByAttribute (): RequestBodyStorage
6361 {
6462 $ openAPIRequestBody = new RequestBodyStorage ($ this ->requestBody ->contentType );
65- $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildRequestBodyParameterCollections ($ this ->requestBody ->className ,$ this ->requestBody ->group ),$ n );
63+ $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildParameterCollections ($ this ->requestBody ->className ,$ this ->requestBody ->group ),$ n );
6664 $ openAPIRequestBody ->withParameter ($ schemaStorage );
6765 return $ openAPIRequestBody ;
6866 }
@@ -77,7 +75,7 @@ public function buildRequestBodyByParameters(): RequestBodyStorage
7775 $ type = $ methodParam ?->getType();
7876 $ requestBodyClass = $ type instanceof ReflectionNamedType ? $ type ->getName () : '' ;
7977 if (is_subclass_of ($ requestBodyClass , Serialize::class)) {
80- $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildRequestBodyParameterCollections ($ requestBodyClass ),$ node );
78+ $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildParameterCollections ($ requestBodyClass ),$ node );
8179 $ openAPIRequestBody ->withParameter ($ schemaStorage );
8280 }
8381
@@ -89,9 +87,23 @@ public function buildRequestBodyByParameters(): RequestBodyStorage
8987 */
9088 public function buildResponse (): ResponseStorage
9189 {
90+ $ returnClass = $ this ->reflectionMethod ->getReturnType ();
91+ $ returnClass = $ returnClass instanceof ReflectionNamedType ? $ returnClass ->getName () : null ;
92+ $ responseClass = match (true ){
93+ $ this ->response !== null => $ this ->response ->className ,
94+ $ returnClass && is_subclass_of ($ returnClass ,Serialize::class) => $ returnClass ,
95+ default => null ,
96+ };
97+
9298 $ responseStorage = new ResponseStorage ();
93- $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildResponseParameterCollections ());
94- $ responseStorage ->withParameter ($ schemaStorage );
99+
100+
101+ if ($ responseClass ) {
102+ $ groups = $ this ->response && is_array ($ this ->response ->groups ) ? $ this ->response ->groups : ['default ' ];
103+ $ schemaStorage = (new SchemaStorage ())->build ($ this ->buildParameterCollections ($ responseClass , $ groups ));
104+ $ responseStorage ->withParameter ($ schemaStorage );
105+ }
106+
95107 return $ responseStorage ;
96108 }
97109
@@ -101,13 +113,12 @@ public function buildResponse(): ResponseStorage
101113 * @return array<ParameterCollection>
102114 * @throws InvalidArgumentException
103115 */
104- public function buildRequestBodyParameterCollections (string $ className , array $ groups = ['default ' ]): array
116+ public function buildParameterCollections (string $ className , array $ groups = ['default ' ]): array
105117 {
106118 $ serializeContext = ContextFactory::build ($ className );
107119 $ serializeContext ->from ();
108120 $ properties = $ serializeContext ->getGroupCollection ()->getProperties ();
109121
110-
111122 $ vols = [];
112123 foreach ($ properties as $ property ){
113124 $ vol = new ParameterCollection (
@@ -123,53 +134,7 @@ className: $className,
123134 if ($ property ->getChildren ()){
124135 foreach ($ property ->getChildren () as $ children ){
125136 $ className = $ children ->getClassName ();
126- $ vol ->children [$ className ] = $ this ->buildRequestBodyParameterCollections ($ className );
127- }
128- }
129-
130- $ vols [] = $ vol ;
131- }
132-
133- return $ vols ;
134- }
135-
136- /**
137- * @return array<ParameterCollection>
138- * @throws InvalidArgumentException
139- */
140- public function buildResponseParameterCollections (): array
141- {
142- $ returnClass = $ this ->reflectionMethod ->getReturnType ();
143- $ responseClass = match (true ){
144- $ this ->response !== null => $ this ->response ->className ,
145- $ returnClass && is_subclass_of ($ returnClass ,Serialize::class) => $ returnClass ,
146- default => null ,
147- };
148-
149- if (!$ responseClass ){
150- return [];
151- }
152-
153- $ groups = $ this ->response && is_array ($ this ->response ->groups ) ? $ this ->response ->groups : ['default ' ];
154- $ serializeContext = ContextFactory::build ($ responseClass );
155- $ serializeContext ->from ();
156- $ properties = $ serializeContext ->getGroupCollection ()->getProperties ();
157-
158- $ vols = [];
159- foreach ($ properties as $ property ){
160- $ vol = new ParameterCollection (
161- className: $ responseClass ,
162- name: current ($ property ->getOutNamesByGroups ($ groups ,$ responseClass )),
163- types: $ property ->getTypes (),
164- type: ParameterTypeEnum::getByTypes ($ property ->getTypes ()),
165- descriptions: '' ,
166- required: !$ property ->isNullable (),
167- ignore: false ,
168- );
169-
170- if ($ property ->getChildren ()){
171- foreach ($ property ->getChildren () as $ children ){
172- $ vol ->children [] = $ this ->buildRequestBodyParameterCollections ($ children ->getClassName ());
137+ $ vol ->children [$ className ] = $ this ->buildParameterCollections ($ className );
173138 }
174139 }
175140
0 commit comments