From f020592c956706fe39d7de507f4ae2c36544307a Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Tue, 6 Sep 2022 18:30:35 +0100 Subject: [PATCH] WIP --- src/Attributes/LodataProperty.php | 13 ++++++++++ src/Property.php | 7 ++++++ tests/Laravel/Models/AllAttribute.php | 3 ++- tests/Laravel/Models/AllAttributeEnum.php | 3 ++- tests/Setup/AttributeDiscoveryTest.php | 12 +++++++++ ...tributeDiscoveryTest__test_metadata__1.xml | 3 +++ ...ributeDiscoveryTest__test_metadata__3.json | 5 ++++ ...ributeDiscoveryTest__test_metadata__4.json | 25 ++++++++++++++++--- 8 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Attributes/LodataProperty.php b/src/Attributes/LodataProperty.php index e3835e280..63c5bd7db 100644 --- a/src/Attributes/LodataProperty.php +++ b/src/Attributes/LodataProperty.php @@ -5,6 +5,7 @@ namespace Flat3\Lodata\Attributes; use Flat3\Lodata\Annotation\Core\V1\Computed; +use Flat3\Lodata\Annotation\Core\V1\Description; use Flat3\Lodata\DeclaredProperty; use Flat3\Lodata\EntitySet; use Flat3\Lodata\Property; @@ -13,6 +14,7 @@ abstract class LodataProperty { protected ?string $name; + protected ?string $description; protected ?string $source = null; protected bool $key = false; protected bool $computed = false; @@ -26,6 +28,7 @@ abstract class LodataProperty public function __construct( string $name, + ?string $description = null, ?string $source = null, ?bool $key = false, ?bool $computed = false, @@ -38,6 +41,7 @@ public function __construct( $filterable = true ) { $this->name = $name; + $this->description = $description; $this->source = $source; $this->key = $key; $this->computed = $computed; @@ -55,6 +59,11 @@ public function getName(): ?string return $this->name; } + public function getDescription(): ?string + { + return $this->description; + } + public function getSource(): ?string { return $this->source; @@ -141,6 +150,10 @@ public function addProperty(EntitySet $entitySet): Property $property->setNullable($this->isNullable()); + if ($this->getDescription()) { + $property->addAnnotation(new Description($this->getDescription())); + } + if ($this->isComputed()) { $property->addAnnotation(new Computed); } diff --git a/src/Property.php b/src/Property.php index 98f1becf9..e0da441ad 100644 --- a/src/Property.php +++ b/src/Property.php @@ -4,6 +4,7 @@ namespace Flat3\Lodata; +use Flat3\Lodata\Annotation\Core\V1\Description; use Flat3\Lodata\Exception\Protocol\BadRequestException; use Flat3\Lodata\Exception\Protocol\ConfigurationException; use Flat3\Lodata\Helper\CollectionType; @@ -403,6 +404,12 @@ public function getOpenAPISchema(): array $schema['multipleOf'] = 1 / (10 ** $scale); } + /** @var Description $description */ + $description = $this->getAnnotations()->firstByClass(Description::class); + if ($description) { + $schema['description'] = $description->toJson(); + } + if ($this->hasPrecision()) { $precision = $this->getPrecision(); diff --git a/tests/Laravel/Models/AllAttribute.php b/tests/Laravel/Models/AllAttribute.php index 7e78a44c8..f0d1e2f43 100644 --- a/tests/Laravel/Models/AllAttribute.php +++ b/tests/Laravel/Models/AllAttribute.php @@ -60,7 +60,8 @@ LodataTimeOfDay(name: 'Sixteen'), LodataUInt16(name: 'Seventeen'), LodataUInt32(name: 'Eighteen'), - LodataUInt64(name: 'Nineteen') + LodataUInt64(name: 'Nineteen'), + LodataString(name: 'Twenty', description: 'This is the *description*') ] class AllAttribute extends Model { diff --git a/tests/Laravel/Models/AllAttributeEnum.php b/tests/Laravel/Models/AllAttributeEnum.php index b14ff0082..6758db2b4 100644 --- a/tests/Laravel/Models/AllAttributeEnum.php +++ b/tests/Laravel/Models/AllAttributeEnum.php @@ -62,7 +62,8 @@ LodataTimeOfDay(name: 'Sixteen'), LodataUInt16(name: 'Seventeen'), LodataUInt32(name: 'Eighteen'), - LodataUInt64(name: 'Nineteen') + LodataUInt64(name: 'Nineteen'), + LodataString(name: 'Twenty', description: 'This is the *description*') ] class AllAttributeEnum extends Model { diff --git a/tests/Setup/AttributeDiscoveryTest.php b/tests/Setup/AttributeDiscoveryTest.php index 90b09e1d7..bb6c58a1f 100644 --- a/tests/Setup/AttributeDiscoveryTest.php +++ b/tests/Setup/AttributeDiscoveryTest.php @@ -4,6 +4,7 @@ namespace Flat3\Lodata\Tests\Setup; +use Flat3\Lodata\Annotation\Core\V1\Description; use Flat3\Lodata\ComplexType; use Flat3\Lodata\Facades\Lodata; use Flat3\Lodata\Helper\Discovery; @@ -180,6 +181,13 @@ public function attributes() 'Nineteen', UInt64::class, ], + 'Twenty' => [ + 'Twenty', + String_::class, + null, + null, + ['description' => 'This is the *description*'], + ], ]; } @@ -233,6 +241,10 @@ public function test_attributes($name, $type, $key = null, $source = null, $extr if ($extra['maxLength'] ?? null) { $this->assertEquals($extra['maxLength'], $property->getMaxLength()); } + + if ($extra['description'] ?? null) { + $this->assertEquals($extra['description'], $property->getAnnotations()->firstByClass(Description::class)->toJson()); + } } public function test_metadata() diff --git a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__1.xml b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__1.xml index 99759420a..8cd09c607 100644 --- a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__1.xml +++ b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__1.xml @@ -118,6 +118,9 @@ + + + diff --git a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__3.json b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__3.json index 124f28124..3518bd5d8 100644 --- a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__3.json +++ b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__3.json @@ -250,6 +250,11 @@ "Nineteen": { "$Type": "com.example.odata.UInt64", "$Nullable": true + }, + "Twenty": { + "$Type": "Edm.String", + "$Nullable": true, + "@Org.OData.Core.V1.Description": "This is the *description*" } } } diff --git a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__4.json b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__4.json index b1d77c774..ba27ce2ee 100644 --- a/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__4.json +++ b/tests/__snapshots__/Setup/AttributeDiscoveryTest__test_metadata__4.json @@ -64,7 +64,8 @@ "Sixteen", "Seventeen", "Eighteen", - "Nineteen" + "Nineteen", + "Twenty" ] } } @@ -144,7 +145,9 @@ "Eighteen", "Eighteen desc", "Nineteen", - "Nineteen desc" + "Nineteen desc", + "Twenty", + "Twenty desc" ] } } @@ -277,7 +280,8 @@ "Sixteen", "Seventeen", "Eighteen", - "Nineteen" + "Nineteen", + "Twenty" ] } } @@ -759,6 +763,11 @@ "minimum": 0, "maximum": 9223372036854775807, "nullable": true + }, + "Twenty": { + "type": "string", + "nullable": true, + "description": "This is the *description*" } } }, @@ -1037,6 +1046,11 @@ "minimum": 0, "maximum": 9223372036854775807, "nullable": true + }, + "Twenty": { + "type": "string", + "nullable": true, + "description": "This is the *description*" } } }, @@ -1309,6 +1323,11 @@ "minimum": 0, "maximum": 9223372036854775807, "nullable": true + }, + "Twenty": { + "type": "string", + "nullable": true, + "description": "This is the *description*" } } },