Skip to content

Commit

Permalink
the swagger generator is now attribute-based, added empty validators …
Browse files Browse the repository at this point in the history
…for remaining types
  • Loading branch information
eceltov committed Jan 5, 2025
1 parent 73ee0a1 commit ba23bfe
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 20 deletions.
8 changes: 3 additions & 5 deletions app/commands/MetaTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function test(string $arg)
// $format = new UserFormat();
// var_dump($format->checkedAssign("email", "[email protected]"));

/*
$inDir = __DIR__ . "/../V1Module/presenters";
$outDir = __DIR__ . "/../V1Module/presenters2";

Expand Down Expand Up @@ -80,10 +79,9 @@ public function test(string $arg)
fwrite($newFile, $newContent);
fclose($newFile);
}
*/

$reflection = AnnotationHelper::getMethod("App\V1Module\Presenters\RegistrationPresenter", "actionCreateAccount");
$attrs = MetaFormatHelper::extractRequestParamData($reflection);
var_dump($attrs);
// $reflection = AnnotationHelper::getMethod("App\V1Module\Presenters\RegistrationPresenter", "actionCreateAccount");
// $attrs = MetaFormatHelper::extractRequestParamData($reflection);
// var_dump($attrs);
}
}
25 changes: 25 additions & 0 deletions app/helpers/MetaFormats/AnnotationToAttributeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,31 @@ private static function convertAnnotationValidationToValidatorString(string $val
return "new StringValidator()";
}

///TODO: this ignores nullability
if (str_ends_with($validation, "|null")) {
$validation = substr($validation, 0, -5);
}

switch ($validation) {
case "email":
// there is one occurrence of this
case "email:1..":
return "new EmailValidator()";
case "numericint":
return "new IntValidator()";
case "bool":
case "boolean":
return "new BoolValidator()";
case "array":
case "list":
return "new ArrayValidator()";
case "timestamp":
return "new TimestampValidator()";
case "numeric":
return "new FloatValidator()";
default:
///TODO
var_dump("unsupported: $validation");
return "\"UNSUPPORTED\"";
}
}
Expand All @@ -165,6 +185,11 @@ public static function convertFile(string $path)
$lines[] = "use App\Helpers\MetaFormats\Validators\StringValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\EmailValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\UuidValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\BoolValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\ArrayValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\FloatValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\IntValidator;";
$lines[] = "use App\Helpers\MetaFormats\Validators\TimestampValidator;";
$lines[] = $line;
$usingsAdded = true;
// store attribute lines in the buffer and do not write them
Expand Down
15 changes: 8 additions & 7 deletions app/helpers/MetaFormats/MetaFormatHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ public static function extractRequestParamData(ReflectionMethod $reflectionMetho
$data = [];
foreach ($attrs as $attr) {
$paramAttr = $attr->newInstance();
$type = $paramAttr->type;
$description = $paramAttr->description;
$required = $paramAttr->required;
$validators = $paramAttr->validators;

$data[] = new RequestParamData($type, $description, $required, $validators);
$data[] = new RequestParamData(
$paramAttr->type,
$paramAttr->paramName,
$paramAttr->description,
$paramAttr->required,
$paramAttr->validators
);
}

return $data;
Expand All @@ -123,7 +124,7 @@ public static function extractRequestAttributeData(
$description = array_key_exists("description", $requestArguments) ? $requestArguments["description"] : "";
$required = array_key_exists("required", $requestArguments) ? $requestArguments["required"] : true;

return new RequestParamData($type, $description, $required);
return new RequestParamData($type, "TODO_IMPLEMENT_FOR_FormatParameterAttribute", $description, $required);
}

/**
Expand Down
29 changes: 27 additions & 2 deletions app/helpers/MetaFormats/RequestParamData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,43 @@

namespace App\Helpers\MetaFormats;

use App\Helpers\MetaFormats\Validators\StringValidator;
use App\Helpers\Swagger\AnnotationParameterData;

class RequestParamData
{
public RequestParamType $type;
public string $name;
public string $description;
public bool $required;
public array $validators;

public function __construct(RequestParamType $type, string $description, bool $required, array $validators = [])
{
public function __construct(
RequestParamType $type,
string $name,
string $description,
bool $required,
array $validators = []
) {
$this->type = $type;
$this->name = $name;
$this->description = $description;
$this->required = $required;
$this->validators = $validators;
}

public function toAnnotationParameterData()
{
$dataType = null;
if (count($this->validators) > 0) {
$dataType = $this->validators[0]::SWAGGER_TYPE;
}

return new AnnotationParameterData(
$dataType,
$this->name,
$this->description,
strtolower($this->type->name)
);
}
}
17 changes: 17 additions & 0 deletions app/helpers/MetaFormats/Validators/ArrayValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Helpers\MetaFormats\Validators;

use App\Helpers\MetaFormats\PhpTypes;
use App\Helpers\MetaFormats\PrimitiveFormatValidators;

class ArrayValidator
{
public const SWAGGER_TYPE = "array";

public function validate(string $value)
{
///TODO: check if array, check content
return true;
}
}
17 changes: 17 additions & 0 deletions app/helpers/MetaFormats/Validators/BoolValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Helpers\MetaFormats\Validators;

use App\Helpers\MetaFormats\PhpTypes;
use App\Helpers\MetaFormats\PrimitiveFormatValidators;

class BoolValidator
{
public const SWAGGER_TYPE = "boolean";

public function validate(string $value)
{
///TODO: check if bool
return true;
}
}
17 changes: 17 additions & 0 deletions app/helpers/MetaFormats/Validators/FloatValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Helpers\MetaFormats\Validators;

use App\Helpers\MetaFormats\PhpTypes;
use App\Helpers\MetaFormats\PrimitiveFormatValidators;

class FloatValidator
{
public const SWAGGER_TYPE = "number";

public function validate(string $value)
{
///TODO: check if float
return true;
}
}
17 changes: 17 additions & 0 deletions app/helpers/MetaFormats/Validators/IntValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Helpers\MetaFormats\Validators;

use App\Helpers\MetaFormats\PhpTypes;
use App\Helpers\MetaFormats\PrimitiveFormatValidators;

class IntValidator
{
public const SWAGGER_TYPE = "integer";

public function validate(string $value)
{
///TODO: check if int
return true;
}
}
17 changes: 17 additions & 0 deletions app/helpers/MetaFormats/Validators/TimestampValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Helpers\MetaFormats\Validators;

use App\Helpers\MetaFormats\PhpTypes;
use App\Helpers\MetaFormats\PrimitiveFormatValidators;

class TimestampValidator
{
public const SWAGGER_TYPE = "string";

public function validate(string $value)
{
///TODO: check if timestamp
return true;
}
}
14 changes: 10 additions & 4 deletions app/helpers/Swagger/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Helpers\Swagger;

use App\Helpers\MetaFormats\MetaFormatHelper;
use ReflectionClass;
use ReflectionMethod;
use Exception;
Expand Down Expand Up @@ -39,8 +40,10 @@ private static function extractAnnotationHttpMethod(array $annotations): HttpMet

// check if the annotations have an http method
foreach ($methods as $methodString => $methodEnum) {
if (in_array($methodString, $annotations)) {
return $methodEnum;
foreach ($annotations as $annotation) {
if (str_starts_with($annotation, $methodString)) {
return $methodEnum;
}
}
}

Expand Down Expand Up @@ -218,8 +221,11 @@ public static function extractAnnotationData(string $className, string $methodNa

$httpMethod = self::extractAnnotationHttpMethod($methodAnnotations);
$standardAnnotationParams = self::extractStandardAnnotationParams($methodAnnotations, $route);
$netteAnnotationParams = self::extractNetteAnnotationParams($methodAnnotations);
$params = array_merge($standardAnnotationParams, $netteAnnotationParams);
$attributeData = MetaFormatHelper::extractRequestParamData(self::getMethod($className, $methodName));
$attributeParams = array_map(function ($data) {
return $data->toAnnotationParameterData();
}, $attributeData);
$params = array_merge($standardAnnotationParams, $attributeParams);

$pathParams = [];
$queryParams = [];
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/Swagger/AnnotationParameterData.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ private function getSwaggerType(): string
if ($this->isDatatypeNullable()) {
$typename = substr($typename, 0, -strlen(self::$nullableSuffix));
}

if (self::$typeMap[$typename] === null) {
///TODO: Return the commented exception below once the meta-view formats are implemented.
/// This detaults to strings because custom types like 'email' are not supported yet.
return 'string';
}
//throw new \InvalidArgumentException("Error in getSwaggerType: Unknown typename: {$typename}");

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

0 comments on commit ba23bfe

Please sign in to comment.