-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathResponseType.php
More file actions
312 lines (291 loc) · 7.35 KB
/
ResponseType.php
File metadata and controls
312 lines (291 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OpenAPIExtractor;
use Exception;
use PHPStan\PhpDocParser\Ast\Type\ConstTypeNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
class ResponseType {
public function __construct(
public string $className,
public bool $hasContentTypeTemplate,
public bool $hasTypeTemplate,
public ?string $defaultContentType,
public ?OpenApiType $defaultType,
public ?array $defaultHeaders,
) {
}
/** @return ResponseType[] */
public static function getAll(): array {
$context = 'Response Types';
$stringType = new OpenApiType(context: $context, type: 'string');
$binaryType = new OpenApiType(context: $context, type: 'string', format: 'binary');
return [
new ResponseType(
'DataDisplayResponse',
false,
false,
null,
$binaryType,
null,
),
new ResponseType(
'DataDownloadResponse',
true,
false,
null,
$binaryType,
null,
),
new ResponseType(
'DataResponse',
false,
true,
'application/json',
$stringType,
null,
),
new ResponseType(
'DownloadResponse',
true,
false,
null,
$binaryType,
null,
),
new ResponseType(
'FileDisplayResponse',
false,
false,
null,
$binaryType,
null,
),
new ResponseType(
'JSONResponse',
false,
true,
'application/json',
$stringType,
null,
),
new ResponseType(
'NotFoundResponse',
false,
false,
'text/html',
$stringType,
null,
),
new ResponseType(
'RedirectResponse',
false,
false,
null,
null,
['Location' => $stringType],
),
new ResponseType(
'RedirectToDefaultAppResponse',
false,
false,
null,
null,
['Location' => $stringType],
),
new ResponseType(
'Response',
false,
false,
null,
null,
null,
),
new ResponseType(
'StandaloneTemplateResponse',
false,
false,
'text/html',
$stringType,
null,
),
new ResponseType(
'StreamResponse',
false,
false,
null,
$binaryType,
null,
),
new ResponseType(
'StreamGeneratorResponse',
false,
false,
null,
null,
null,
),
new ResponseType(
'StreamTraversableResponse',
false,
false,
null,
null,
null,
),
new ResponseType(
'TemplateResponse',
false,
false,
'text/html',
$stringType,
null,
),
new ResponseType(
'TextPlainResponse',
false,
false,
'text/plain',
$stringType,
null,
),
new ResponseType(
'TooManyRequestsResponse',
false,
false,
'text/html',
$stringType,
null,
),
new ResponseType(
'ZipResponse',
false,
false,
null,
$binaryType,
null,
),
];
}
/**
* @return list<ControllerMethodResponse|null>
* @throws Exception
*/
public static function resolve(string $context, TypeNode $obj): array {
global $definitions;
$responseTypes = self::getAll();
$responses = [];
if ($obj instanceof UnionTypeNode) {
foreach ($obj->types as $subType) {
$responses = array_merge($responses, self::resolve($context, $subType));
}
return $responses;
}
if ($obj instanceof IdentifierTypeNode) {
$className = $obj->name;
$args = [];
} elseif ($obj instanceof GenericTypeNode) {
$className = $obj->type->name;
$args = $obj->genericTypes;
} else {
Logger::panic($context, 'Failed to get class name for ' . $obj);
}
$classNameParts = explode('\\', $className);
$className = end($classNameParts);
if ($className == 'void') {
$responses[] = null;
} else {
if (count(array_filter($responseTypes, fn (\OpenAPIExtractor\ResponseType $responseType): bool => $responseType->className == $className)) === 0) {
Logger::error($context, "Invalid return type '" . $obj . "'");
return [];
}
foreach ($responseTypes as $responseType) {
if ($responseType->className == $className) {
// +2 for status code and headers which are always present
$expectedArgs = count(array_filter([$responseType->hasContentTypeTemplate, $responseType->hasTypeTemplate], fn (bool $value): bool => $value)) + 2;
if (count($args) !== $expectedArgs) {
Logger::error($context, "'" . $className . "' needs " . $expectedArgs . ' parameters');
continue;
}
$statusCodes = StatusCodes::resolveType($context, $args[0]);
$i = 1;
if ($responseType->hasContentTypeTemplate) {
if ($args[$i] instanceof ConstTypeNode) {
$contentTypes = [$args[$i]->constExpr->value];
} elseif ($args[$i] instanceof IdentifierTypeNode && $args[$i]->name === 'string') {
$contentTypes = ['*/*'];
} elseif ($args[$i] instanceof UnionTypeNode) {
$contentTypes = array_map(fn (\PHPStan\PhpDocParser\Ast\Type\TypeNode $arg) => $arg->constExpr->value, $args[$i]->types);
} else {
Logger::panic($context, 'Unable to parse content type from ' . $args[$i]::class);
}
$i++;
} else {
$contentTypes = $responseType->defaultContentType != null ? [$responseType->defaultContentType] : [];
}
if ($responseType->hasTypeTemplate) {
$type = OpenApiType::resolve($context, $definitions, $args[$i]);
$i++;
} else {
$type = $responseType->defaultType;
}
$headersType = OpenApiType::resolve($context, $definitions, $args[$i]);
if ($headersType->additionalProperties !== null) {
Logger::error($context, 'Use array{} instead of array<string, mixed> for empty headers');
}
$headers = $headersType->properties ?? [];
if ($responseType->defaultHeaders != null) {
$headers = array_merge($responseType->defaultHeaders, $headers);
}
if (array_key_exists('Content-Type', $headers)) {
/** @var OpenApiType $value */
$values = $headers['Content-Type'];
$values = $values->oneOf != null ? $values->oneOf : [$values];
foreach ($values as $value) {
if ($value->type == 'string' && $value->enum != null) {
$contentTypes = array_merge($contentTypes, $value->enum);
}
}
// Content-Type is an illegal response header
unset($headers['Content-Type']);
}
$contentTypes = $contentTypes !== [] ? $contentTypes : [$type != null ? '*/*' : null];
foreach ($statusCodes as $statusCode) {
if ($statusCode === 204 || $statusCode === 304) {
if ($statusCode === 304) {
$customHeaders = array_filter(array_keys($headers), static fn (string $header): bool => str_starts_with(strtolower($header), 'x-'));
if ($customHeaders !== []) {
Logger::error($context, 'Custom headers are not allowed for responses with status code 304. Found: ' . implode(', ', $customHeaders));
}
}
$responses[] = new ControllerMethodResponse(
$className,
$statusCode,
null,
null,
$headers,
);
} else {
foreach ($contentTypes as $contentType) {
$responses[] = new ControllerMethodResponse(
$className,
$statusCode,
$contentType,
$type,
$headers,
);
}
}
}
break;
}
}
}
return $responses;
}
}