From 552a876b575e375adc8ff0cfe580805d4b39fe83 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Tue, 29 Oct 2024 18:09:51 -0500 Subject: [PATCH] More compatibility adjustments --- composer.json | 2 +- src/Repository/BaseRepository.php | 61 ++++++++++++++++--------------- src/Util/OpenApiContext.php | 14 +++++-- 3 files changed, 43 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index adc43b4..5d1f77c 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "prefer-stable": true, "license": "MIT", "require": { - "php": ">=8.1", + "php": ">=8.1 <8.4", "ext-json": "*", "ext-openssl": "*", "ext-curl": "*", diff --git a/src/Repository/BaseRepository.php b/src/Repository/BaseRepository.php index 5b68d6f..d90a452 100644 --- a/src/Repository/BaseRepository.php +++ b/src/Repository/BaseRepository.php @@ -10,8 +10,6 @@ use ByJG\Config\Exception\KeyNotFoundException; use ByJG\MicroOrm\Exception\OrmBeforeInvalidException; use ByJG\MicroOrm\Exception\OrmInvalidFieldsException; -use ByJG\MicroOrm\Exception\RepositoryReadOnlyException; -use ByJG\MicroOrm\Exception\UpdateConstraintException; use ByJG\MicroOrm\FieldMapping; use ByJG\MicroOrm\Literal\HexUuidLiteral; use ByJG\MicroOrm\Literal\Literal; @@ -41,6 +39,11 @@ public function get($itemId) return $this->repository->get(HexUuidLiteral::create($itemId)); } + public function getRepository(): Repository + { + return $this->repository; + } + public function getMapper() { return $this->repository->getMapper(); @@ -84,7 +87,7 @@ public function listGeneric($tableName, $fields = [], $page = 0, $size = 20, $or $object = $query->build($this->repository->getDbDriver()); - $iterator = $this->repository->getDbDriver()->getIterator($object["sql"], $object["params"]); + $iterator = $this->repository->getDbDriver()->getIterator($object->getSql(), $object->getParameters()); return $iterator->toArray(); } @@ -158,26 +161,31 @@ public static function getUuid() protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = 'id', $uuidStrPropertyName = 'uuid') { $fieldMapping = FieldMapping::create($binPropertyName) - ->withUpdateFunction(function ($value, $instance) { - if (empty($value)) { - return null; - } - if (!($value instanceof Literal)) { - $value = new HexUuidLiteral($value); - } - return $value; - }) - ->withSelectFunction(function ($value, $instance) use ($binPropertyName, $uuidStrPropertyName) { - if (!empty($uuidStrPropertyName)) { - $fieldValue = $instance->{'get' . $uuidStrPropertyName}(); - } else { - $fieldValue = HexUuidLiteral::getFormattedUuid($instance->{'get' . $binPropertyName}(), false); + ->withUpdateFunction( + function ($value, $instance) { + if (empty($value)) { + return null; + } + if (!($value instanceof Literal)) { + $value = new HexUuidLiteral($value); + } + return $value; } - if (is_null($fieldValue)) { - return null; + ) + ->withSelectFunction( + function ($value, $instance) use ($binPropertyName, $uuidStrPropertyName) { + if (!empty($uuidStrPropertyName)) { + $fieldValue = $instance->{'get' . $uuidStrPropertyName}(); + } else { + $itemValue = $instance->{'get' . $binPropertyName}(); + $fieldValue = HexUuidLiteral::getFormattedUuid($itemValue, false, $itemValue); + } + if (is_null($fieldValue)) { + return null; + } + return $fieldValue; } - return $fieldValue; - }); + ); if (!empty($mapper)) { $mapper->addFieldMapping($fieldMapping); @@ -187,15 +195,12 @@ protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = ' } /** - * @param $model - * @param UpdateConstraint|null $updateConstraint + * @param $model * @return mixed * @throws InvalidArgumentException * @throws OrmBeforeInvalidException * @throws OrmInvalidFieldsException * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException - * @throws RepositoryReadOnlyException - * @throws UpdateConstraintException */ public function save($model, ?UpdateConstraint $updateConstraint = null) { @@ -203,10 +208,8 @@ public function save($model, ?UpdateConstraint $updateConstraint = null) $primaryKey = $this->repository->getMapper()->getPrimaryKey()[0]; - if ($model->{"get$primaryKey"}() instanceof HexUuidLiteral) { - /** @var HexUuidLiteral $literal */ - $literal = $model->{"get$primaryKey"}(); - $model->{"set$primaryKey"}($literal->formatUuid()); + if ($model->{"get$primaryKey"}() instanceof Literal) { + $model->{"set$primaryKey"}(HexUuidLiteral::create($model->{"get$primaryKey"}())); } return $model; diff --git a/src/Util/OpenApiContext.php b/src/Util/OpenApiContext.php index 5abf66b..480f3b4 100644 --- a/src/Util/OpenApiContext.php +++ b/src/Util/OpenApiContext.php @@ -18,9 +18,6 @@ public static function validateRequest(HttpRequest $request) $path = $request->getRequestPath(); $method = $request->server('REQUEST_METHOD'); - // Returns a SwaggerRequestBody instance - $bodyRequestDef = $schema->getRequestParameters($path, $method); - // Validate the request body (payload) if (str_contains($request->getHeader('Content-Type') ?? "", 'multipart/')) { $requestBody = $request->post(); @@ -31,7 +28,16 @@ public static function validateRequest(HttpRequest $request) } try { - $bodyRequestDef->match($requestBody); + // Validate the request path and query against the OpenAPI schema + $schema->getPathDefinition($path, $method); + + if (!empty($requestBody)) { + // Returns a SwaggerRequestBody instance + $bodyRequestDef = $schema->getRequestParameters($path, $method); + $bodyRequestDef->match($requestBody); + } else { + return []; + } } catch (Exception $ex) { throw new Error400Exception(explode("\n", $ex->getMessage())[0]); }