2
2
3
3
namespace Astral \Serialize \OpenApi \Collections ;
4
4
5
- use Astral \Serialize \Enums \TypeKindEnum ;
6
5
use Astral \Serialize \OpenApi \Annotations \Headers ;
7
6
use Astral \Serialize \OpenApi \Annotations \RequestBody ;
8
7
use Astral \Serialize \OpenApi \Annotations \Response ;
@@ -52,7 +51,6 @@ public function build() : Method
52
51
53
52
$ openAPIMethod ->withRequestBody ($ this ->requestBody !== null ? $ this ->buildRequestBodyByAttribute () : $ this ->buildRequestBodyByParameters ());
54
53
$ openAPIMethod ->addResponse (200 , $ this ->buildResponse ());
55
-
56
54
return $ openAPIMethod ;
57
55
}
58
56
@@ -62,7 +60,7 @@ public function build() : Method
62
60
public function buildRequestBodyByAttribute (): RequestBodyStorage
63
61
{
64
62
$ 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 );
66
64
$ openAPIRequestBody ->withParameter ($ schemaStorage );
67
65
return $ openAPIRequestBody ;
68
66
}
@@ -77,7 +75,7 @@ public function buildRequestBodyByParameters(): RequestBodyStorage
77
75
$ type = $ methodParam ?->getType();
78
76
$ requestBodyClass = $ type instanceof ReflectionNamedType ? $ type ->getName () : '' ;
79
77
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 );
81
79
$ openAPIRequestBody ->withParameter ($ schemaStorage );
82
80
}
83
81
@@ -89,9 +87,23 @@ public function buildRequestBodyByParameters(): RequestBodyStorage
89
87
*/
90
88
public function buildResponse (): ResponseStorage
91
89
{
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
+
92
98
$ 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
+
95
107
return $ responseStorage ;
96
108
}
97
109
@@ -101,13 +113,12 @@ public function buildResponse(): ResponseStorage
101
113
* @return array<ParameterCollection>
102
114
* @throws InvalidArgumentException
103
115
*/
104
- public function buildRequestBodyParameterCollections (string $ className , array $ groups = ['default ' ]): array
116
+ public function buildParameterCollections (string $ className , array $ groups = ['default ' ]): array
105
117
{
106
118
$ serializeContext = ContextFactory::build ($ className );
107
119
$ serializeContext ->from ();
108
120
$ properties = $ serializeContext ->getGroupCollection ()->getProperties ();
109
121
110
-
111
122
$ vols = [];
112
123
foreach ($ properties as $ property ){
113
124
$ vol = new ParameterCollection (
@@ -123,53 +134,7 @@ className: $className,
123
134
if ($ property ->getChildren ()){
124
135
foreach ($ property ->getChildren () as $ children ){
125
136
$ 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 );
173
138
}
174
139
}
175
140
0 commit comments