@@ -206,6 +206,22 @@ private function getRoutePathParamNames(string $route): array {
206
206
return $ out [1 ];
207
207
}
208
208
209
+ private function getBodyAnnotation (): string |null {
210
+ if (count ($ this ->bodyParams ) === 0 ) {
211
+ return null ;
212
+ }
213
+
214
+ ///TODO: only supports JSON
215
+ $ head = '@OA\RequestBody(@OA\MediaType(mediaType="application/json",@OA\Schema ' ;
216
+ $ body = new ParenthesesBuilder ();
217
+
218
+ foreach ($ this ->bodyParams as $ bodyParam ) {
219
+ $ body ->addValue ($ bodyParam ->toPropertyAnnotation ());
220
+ }
221
+
222
+ return $ head . $ body ->toString () . ")) " ;
223
+ }
224
+
209
225
public function toSwaggerAnnotations (string $ route ) {
210
226
$ httpMethodAnnotation = $ this ->getHttpMethodAnnotation ();
211
227
$ body = new ParenthesesBuilder ();
@@ -221,6 +237,10 @@ public function toSwaggerAnnotations(string $route) {
221
237
$ body ->addValue ($ queryParam ->toParameterAnnotation ($ location ));
222
238
}
223
239
240
+ $ jsonProperties = $ this ->getBodyAnnotation ();
241
+ if ($ jsonProperties !== null )
242
+ $ body ->addValue ($ jsonProperties );
243
+
224
244
///TODO: placeholder
225
245
$ body ->addValue ('@OA\Response(response="200",description="The data") ' );
226
246
return $ httpMethodAnnotation . $ body ->toString ();
@@ -275,16 +295,16 @@ class AnnotationParameterData {
275
295
'numericint ' => 'integer ' ,
276
296
'timestamp ' => 'integer ' ,
277
297
'string ' => 'string ' ,
278
- 'unicode ' => [ 'string ' , ' unicode ' ] ,
279
- 'email ' => [ 'string ' , ' email ' ] ,
280
- 'url ' => [ 'string ' , ' url ' ] ,
281
- 'uri ' => [ 'string ' , ' uri ' ] ,
298
+ 'unicode ' => 'string ' ,
299
+ 'email ' => 'string ' ,
300
+ 'url ' => 'string ' ,
301
+ 'uri ' => 'string ' ,
282
302
'pattern ' => null ,
283
- 'alnum ' => [ 'string ' , ' alphanumeric ' ] ,
284
- 'alpha ' => [ 'string ' , ' alphabetic ' ] ,
285
- 'digit ' => [ 'string ' , ' numeric ' ] ,
286
- 'lower ' => [ 'string ' , ' lowercase ' ] ,
287
- 'upper ' => [ 'string ' , ' uppercase ' ]
303
+ 'alnum ' => 'string ' ,
304
+ 'alpha ' => 'string ' ,
305
+ 'digit ' => 'string ' ,
306
+ 'lower ' => 'string ' ,
307
+ 'upper ' => 'string ' ,
288
308
];
289
309
290
310
public function __construct (
@@ -310,7 +330,7 @@ private function isDatatypeNullable(): bool {
310
330
return false ;
311
331
}
312
332
313
- private function generateSchemaAnnotation (): string {
333
+ private function getSwaggerType (): string {
314
334
# if the type is not specified, default to a string
315
335
$ type = 'string ' ;
316
336
$ typename = $ this ->dataType ;
@@ -319,15 +339,20 @@ private function generateSchemaAnnotation(): string {
319
339
$ typename = substr ($ typename ,0 ,-strlen (self ::$ nullableSuffix ));
320
340
321
341
if (self ::$ typeMap [$ typename ] === null )
322
- throw new \InvalidArgumentException ("Error in SwaggerTypeConverter: Unknown typename: {$ typename }" );
342
+ ///TODO: return the commented exception
343
+ return 'string ' ;
344
+ //throw new \InvalidArgumentException("Error in getSwaggerType: Unknown typename: {$typename}");
323
345
324
346
$ type = self ::$ typeMap [$ typename ];
325
347
}
348
+ return $ type ;
349
+ }
326
350
351
+ private function generateSchemaAnnotation (): string {
327
352
$ head = "@OA \\Schema " ;
328
353
$ body = new ParenthesesBuilder ();
329
- $ body ->addKeyValue ("type " , $ type );
330
354
355
+ $ body ->addKeyValue ("type " , $ this ->getSwaggerType ());
331
356
return $ head . $ body ->toString ();
332
357
}
333
358
@@ -349,6 +374,16 @@ public function toParameterAnnotation(string $parameterLocation): string {
349
374
350
375
return $ head . $ body ->toString ();
351
376
}
377
+
378
+ public function toPropertyAnnotation (): string {
379
+ $ head = "@OA \\Property " ;
380
+ $ body = new ParenthesesBuilder ();
381
+
382
+ ///TODO: handle nullability
383
+ $ body ->addKeyValue ("property " , $ this ->name );
384
+ $ body ->addKeyValue ("type " , $ this ->getSwaggerType ());
385
+ return $ head . $ body ->toString ();
386
+ }
352
387
}
353
388
354
389
class AnnotationHelper {
0 commit comments