Skip to content

Commit

Permalink
added support for POST json properties
Browse files Browse the repository at this point in the history
  • Loading branch information
eceltov committed Oct 8, 2024
1 parent 887da6a commit 174abc3
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions app/commands/SwaggerAnnotator.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ private function getRoutePathParamNames(string $route): array {
return $out[1];
}

private function getBodyAnnotation(): string|null {
if (count($this->bodyParams) === 0) {
return null;
}

///TODO: only supports JSON
$head = '@OA\RequestBody(@OA\MediaType(mediaType="application/json",@OA\Schema';
$body = new ParenthesesBuilder();

foreach ($this->bodyParams as $bodyParam) {
$body->addValue($bodyParam->toPropertyAnnotation());
}

return $head . $body->toString() . "))";
}

public function toSwaggerAnnotations(string $route) {
$httpMethodAnnotation = $this->getHttpMethodAnnotation();
$body = new ParenthesesBuilder();
Expand All @@ -221,6 +237,10 @@ public function toSwaggerAnnotations(string $route) {
$body->addValue($queryParam->toParameterAnnotation($location));
}

$jsonProperties = $this->getBodyAnnotation();
if ($jsonProperties !== null)
$body->addValue($jsonProperties);

///TODO: placeholder
$body->addValue('@OA\Response(response="200",description="The data")');
return $httpMethodAnnotation . $body->toString();
Expand Down Expand Up @@ -275,16 +295,16 @@ class AnnotationParameterData {
'numericint' => 'integer',
'timestamp' => 'integer',
'string' => 'string',
'unicode' => ['string', 'unicode'],
'email' => ['string', 'email'],
'url' => ['string', 'url'],
'uri' => ['string', 'uri'],
'unicode' => 'string',
'email' => 'string',
'url' => 'string',
'uri' => 'string',
'pattern' => null,
'alnum' => ['string', 'alphanumeric'],
'alpha' => ['string', 'alphabetic'],
'digit' => ['string', 'numeric'],
'lower' => ['string', 'lowercase'],
'upper' => ['string', 'uppercase']
'alnum' => 'string',
'alpha' => 'string',
'digit' => 'string',
'lower' => 'string',
'upper' => 'string',
];

public function __construct(
Expand All @@ -310,7 +330,7 @@ private function isDatatypeNullable(): bool {
return false;
}

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

if (self::$typeMap[$typename] === null)
throw new \InvalidArgumentException("Error in SwaggerTypeConverter: Unknown typename: {$typename}");
///TODO: return the commented exception
return 'string';
//throw new \InvalidArgumentException("Error in getSwaggerType: Unknown typename: {$typename}");

$type = self::$typeMap[$typename];
}
return $type;
}

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

$body->addKeyValue("type", $this->getSwaggerType());
return $head . $body->toString();
}

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

return $head . $body->toString();
}

public function toPropertyAnnotation(): string {
$head = "@OA\\Property";
$body = new ParenthesesBuilder();

///TODO: handle nullability
$body->addKeyValue("property", $this->name);
$body->addKeyValue("type", $this->getSwaggerType());
return $head . $body->toString();
}
}

class AnnotationHelper {
Expand Down

0 comments on commit 174abc3

Please sign in to comment.