Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Sep 6, 2022
1 parent 46fddd0 commit f020592
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Attributes/LodataProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -26,6 +28,7 @@ abstract class LodataProperty

public function __construct(
string $name,
?string $description = null,
?string $source = null,
?bool $key = false,
?bool $computed = false,
Expand All @@ -38,6 +41,7 @@ public function __construct(
$filterable = true
) {
$this->name = $name;
$this->description = $description;
$this->source = $source;
$this->key = $key;
$this->computed = $computed;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion tests/Laravel/Models/AllAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Laravel/Models/AllAttributeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 12 additions & 0 deletions tests/Setup/AttributeDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +181,13 @@ public function attributes()
'Nineteen',
UInt64::class,
],
'Twenty' => [
'Twenty',
String_::class,
null,
null,
['description' => 'This is the *description*'],
],
];
}

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<Property Name="Seventeen" Type="com.example.odata.UInt16" Nullable="true"/>
<Property Name="Eighteen" Type="com.example.odata.UInt32" Nullable="true"/>
<Property Name="Nineteen" Type="com.example.odata.UInt64" Nullable="true"/>
<Property Name="Twenty" Type="Edm.String" Nullable="true">
<Annotation Term="Org.OData.Core.V1.Description" String="This is the *description*"/>
</Property>
</EntityType>
<Annotations Target="com.example.odata.DefaultContainer">
<Annotation Term="Org.OData.Core.V1.ConventionalIDs" Bool="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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*"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
"Nineteen",
"Twenty"
]
}
}
Expand Down Expand Up @@ -144,7 +145,9 @@
"Eighteen",
"Eighteen desc",
"Nineteen",
"Nineteen desc"
"Nineteen desc",
"Twenty",
"Twenty desc"
]
}
}
Expand Down Expand Up @@ -277,7 +280,8 @@
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
"Nineteen",
"Twenty"
]
}
}
Expand Down Expand Up @@ -759,6 +763,11 @@
"minimum": 0,
"maximum": 9223372036854775807,
"nullable": true
},
"Twenty": {
"type": "string",
"nullable": true,
"description": "This is the *description*"
}
}
},
Expand Down Expand Up @@ -1037,6 +1046,11 @@
"minimum": 0,
"maximum": 9223372036854775807,
"nullable": true
},
"Twenty": {
"type": "string",
"nullable": true,
"description": "This is the *description*"
}
}
},
Expand Down Expand Up @@ -1309,6 +1323,11 @@
"minimum": 0,
"maximum": 9223372036854775807,
"nullable": true
},
"Twenty": {
"type": "string",
"nullable": true,
"description": "This is the *description*"
}
}
},
Expand Down

0 comments on commit f020592

Please sign in to comment.