From 5785ee1e6d4fcfd222798ed386673f5e697d1862 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Thu, 19 Sep 2024 11:23:20 +0100 Subject: [PATCH] Updates --- src/Drivers/EloquentEntitySet.php | 2 +- src/Helper/ObjectArray.php | 1 + src/PathSegment/OpenAPI.php | 2 +- src/Primitive.php | 10 ++++++++-- src/PrimitiveType.php | 5 +++++ src/Type.php | 5 +++++ src/Type/Guid.php | 3 ++- tests/Queries/GuidTest.php | 9 +++++++++ .../Queries/DefaultValueTest__test_default__4.json | 4 ++-- .../Queries/GuidTest__test_filter_guid__6.json | 9 +++++++++ 10 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tests/__snapshots__/Queries/GuidTest__test_filter_guid__6.json diff --git a/src/Drivers/EloquentEntitySet.php b/src/Drivers/EloquentEntitySet.php index 4f4dd73b6..e75afbe78 100644 --- a/src/Drivers/EloquentEntitySet.php +++ b/src/Drivers/EloquentEntitySet.php @@ -187,7 +187,7 @@ public function read(PropertyValue $key): Entity */ protected function setModelAttributes(Model $model, PropertyValues $propertyValues): Model { - return $propertyValues->getDeclaredPropertyValues()->reduce(function(Model $model, PropertyValue $value) { + return $propertyValues->getDeclaredPropertyValues()->reduce(function (Model $model, PropertyValue $value) { return $model->setAttribute( $this->getPropertySourceName($value->getProperty()), $value->getPrimitive()->toMixed(), diff --git a/src/Helper/ObjectArray.php b/src/Helper/ObjectArray.php index ac36ded28..15eb4ab5f 100644 --- a/src/Helper/ObjectArray.php +++ b/src/Helper/ObjectArray.php @@ -303,6 +303,7 @@ public function map(callable $callback) } /** + * Reduce the objects in the array * @param callable(mixed $initial, mixed $value, mixed $key): mixed $callback * @param $initial * @return mixed|null diff --git a/src/PathSegment/OpenAPI.php b/src/PathSegment/OpenAPI.php index c9596f967..0e0a7d968 100644 --- a/src/PathSegment/OpenAPI.php +++ b/src/PathSegment/OpenAPI.php @@ -1105,7 +1105,7 @@ public static function applyProperty(?Property $property = null, array $schema = $schema['nullable'] = $property->isNullable(); if ($property->hasStaticDefaultValue()) { - $schema['default'] = $property->computeDefaultValue(); + $schema['default'] = $property->computeDefaultValue()->toJson(); } if ($property->hasMaxLength()) { diff --git a/src/Primitive.php b/src/Primitive.php index fa03e9c84..50e6f9b84 100644 --- a/src/Primitive.php +++ b/src/Primitive.php @@ -18,6 +18,7 @@ use Flat3\Lodata\Interfaces\ResponseInterface; use Flat3\Lodata\Interfaces\SerializeInterface; use Flat3\Lodata\Traits\HasIdentifier; +use Stringable; /** * Primitive @@ -57,7 +58,12 @@ public function __construct($value = null) * @return Primitive */ abstract public function set($value); - + + /** + * Test the provided value is formatted correctly for this type + * @param $value + * @return bool + */ public function allows($value): bool { return true; @@ -186,7 +192,7 @@ public function matches($value): bool $value = $value->get(); } - if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) { + if (is_scalar($value) || is_null($value) || $value instanceof Stringable) { return (string) $this->get() === (string) $value; } diff --git a/src/PrimitiveType.php b/src/PrimitiveType.php index ad026ee8c..2f833dcc2 100644 --- a/src/PrimitiveType.php +++ b/src/PrimitiveType.php @@ -100,6 +100,11 @@ public function getOpenAPISchema(): array return $this->instance()->getOpenAPISchema(); } + /** + * Test the provided value is correctly formatted for this type + * @param $value + * @return bool + */ public function allowsValue($value): bool { return $this->instance()->allows($value); diff --git a/src/Type.php b/src/Type.php index 9bfb1c88e..8e63f8d81 100644 --- a/src/Type.php +++ b/src/Type.php @@ -230,6 +230,11 @@ public function is(string $class): bool return is_a($this->factory, $class, true); } + /** + * Test the provided value is correctly formatted for this type + * @param $value + * @return bool + */ public function allowsValue($value): bool { return true; diff --git a/src/Type/Guid.php b/src/Type/Guid.php index e4677254c..1ddc1a7d1 100644 --- a/src/Type/Guid.php +++ b/src/Type/Guid.php @@ -9,6 +9,7 @@ use Flat3\Lodata\PathSegment\OpenAPI; use Flat3\Lodata\Primitive; use Flat3\Lodata\Property; +use Stringable; /** * Guid @@ -27,7 +28,7 @@ public function matches($value): bool $value = $value->get(); } - if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) { + if (is_scalar($value) || is_null($value) || $value instanceof Stringable) { return (string) $this->get() === strtoupper((string) $value); } diff --git a/tests/Queries/GuidTest.php b/tests/Queries/GuidTest.php index da2ab7896..e5ecc07d7 100644 --- a/tests/Queries/GuidTest.php +++ b/tests/Queries/GuidTest.php @@ -53,5 +53,14 @@ public function test_filter_guid() (new Request) ->path('/examples(00000000-0000-0000-0000-0012ea25eefb)') ); + + $this->assertBadRequest( + (new Request) + ->path('/examples') + ->body([ + 'id' => 'hello', + ]) + ->post(), + ); } } diff --git a/tests/__snapshots__/Queries/DefaultValueTest__test_default__4.json b/tests/__snapshots__/Queries/DefaultValueTest__test_default__4.json index 360dfe6b1..3154c9c27 100644 --- a/tests/__snapshots__/Queries/DefaultValueTest__test_default__4.json +++ b/tests/__snapshots__/Queries/DefaultValueTest__test_default__4.json @@ -245,7 +245,7 @@ "a": { "type": "string", "nullable": false, - "default": {} + "default": "a" }, "b": { "type": "string", @@ -260,7 +260,7 @@ "a": { "type": "string", "nullable": false, - "default": {} + "default": "a" }, "b": { "type": "string", diff --git a/tests/__snapshots__/Queries/GuidTest__test_filter_guid__6.json b/tests/__snapshots__/Queries/GuidTest__test_filter_guid__6.json new file mode 100644 index 000000000..e60f45fc2 --- /dev/null +++ b/tests/__snapshots__/Queries/GuidTest__test_filter_guid__6.json @@ -0,0 +1,9 @@ +{ + "error": { + "code": "property_value_invalid", + "message": "The value property 'id' is not valid", + "target": null, + "details": [], + "innererror": {} + } +}