Skip to content

Commit a5086cf

Browse files
committed
added support for POST json properties
1 parent bd295a5 commit a5086cf

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

app/commands/SwaggerAnnotator.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ private function getRoutePathParamNames(string $route): array {
206206
return $out[1];
207207
}
208208

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+
209225
public function toSwaggerAnnotations(string $route) {
210226
$httpMethodAnnotation = $this->getHttpMethodAnnotation();
211227
$body = new ParenthesesBuilder();
@@ -221,6 +237,10 @@ public function toSwaggerAnnotations(string $route) {
221237
$body->addValue($queryParam->toParameterAnnotation($location));
222238
}
223239

240+
$jsonProperties = $this->getBodyAnnotation();
241+
if ($jsonProperties !== null)
242+
$body->addValue($jsonProperties);
243+
224244
///TODO: placeholder
225245
$body->addValue('@OA\Response(response="200",description="The data")');
226246
return $httpMethodAnnotation . $body->toString();
@@ -275,16 +295,16 @@ class AnnotationParameterData {
275295
'numericint' => 'integer',
276296
'timestamp' => 'integer',
277297
'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',
282302
'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',
288308
];
289309

290310
public function __construct(
@@ -310,7 +330,7 @@ private function isDatatypeNullable(): bool {
310330
return false;
311331
}
312332

313-
private function generateSchemaAnnotation(): string {
333+
private function getSwaggerType(): string {
314334
# if the type is not specified, default to a string
315335
$type = 'string';
316336
$typename = $this->dataType;
@@ -319,15 +339,20 @@ private function generateSchemaAnnotation(): string {
319339
$typename = substr($typename,0,-strlen(self::$nullableSuffix));
320340

321341
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}");
323345

324346
$type = self::$typeMap[$typename];
325347
}
348+
return $type;
349+
}
326350

351+
private function generateSchemaAnnotation(): string {
327352
$head = "@OA\\Schema";
328353
$body = new ParenthesesBuilder();
329-
$body->addKeyValue("type", $type);
330354

355+
$body->addKeyValue("type", $this->getSwaggerType());
331356
return $head . $body->toString();
332357
}
333358

@@ -349,6 +374,16 @@ public function toParameterAnnotation(string $parameterLocation): string {
349374

350375
return $head . $body->toString();
351376
}
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+
}
352387
}
353388

354389
class AnnotationHelper {

0 commit comments

Comments
 (0)