Skip to content

Commit 3423930

Browse files
Set attribute type based on validations used, or fallback to default (limosa-io#115)
Closes limosa-io#114
1 parent 7ece4d7 commit 3423930

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/Attribute/Attribute.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
use Illuminate\Database\Eloquent\Builder;
1010
use Illuminate\Database\Eloquent\Model;
1111
use Tmilos\ScimFilterParser\Ast\AttributePath;
12-
use Tmilos\ScimFilterParser\Ast\ComparisonExpression;
13-
use Tmilos\ScimSchema\Model\Resource;
12+
use Illuminate\Support\Str;
1413

1514
class Attribute
1615
{
@@ -36,7 +35,7 @@ class Attribute
3635

3736
protected $multiValued = false;
3837
protected $mutability = 'readWrite';
39-
protected $type = 'string';
38+
protected $type;
4039
protected $description = null;
4140
protected $defaultValue = null;
4241
protected $returned = 'default';
@@ -64,10 +63,11 @@ public function getValidations()
6463
/**
6564
* Return SCIM schema for this attribute
6665
*/
67-
public function generateSchema(){
66+
public function generateSchema()
67+
{
6868
return [
6969
'name' => $this->name,
70-
'type' => $this->type,
70+
'type' => $this->getType(),
7171
'mutability' => $this->mutability,
7272
'returned' => $this->returned,
7373
'uniqueness' => 'server',
@@ -103,7 +103,8 @@ public function ensure(...$validations)
103103
return $this;
104104
}
105105

106-
public function default($value){
106+
public function default($value)
107+
{
107108
$this->defaultValue = $value;
108109

109110
return $this;
@@ -119,6 +120,23 @@ public function nullable()
119120
return in_array('nullable', $this->validations);
120121
}
121122

123+
public function getType()
124+
{
125+
$result = $this->type;
126+
127+
if ($result == null) {
128+
if (in_array('boolean', $this->validations)) {
129+
$result = 'boolean';
130+
} elseif (in_array('integer', $this->validations)) {
131+
$result = 'integer';
132+
} else {
133+
$result = 'string';
134+
}
135+
}
136+
137+
return $result;
138+
}
139+
122140
public function isReadSupported()
123141
{
124142
return $this->readEnabled;
@@ -134,7 +152,8 @@ public function shouldReturn(&$object)
134152
return true;
135153
}
136154

137-
public function setReturned($returned){
155+
public function setReturned($returned)
156+
{
138157
$this->returned = $returned;
139158
return $this;
140159
}
@@ -145,7 +164,8 @@ public function setParent($parent)
145164
return $this;
146165
}
147166

148-
protected function isRequested($attributes){
167+
protected function isRequested($attributes)
168+
{
149169
return empty($attributes) || in_array($this->name, $attributes) || in_array($this->getFullKey(), $attributes) || ($this->parent != null && $this->parent->isRequested($attributes));
150170
}
151171

@@ -156,7 +176,7 @@ public function read(&$object, array $attributes = []): ?AttributeValue
156176
return null;
157177
}
158178

159-
if($this->returned == 'never'){
179+
if ($this->returned == 'never') {
160180
return null;
161181
}
162182

tests/SchemaTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ public function testGet()
3737
{
3838
$response = $this->get('/scim/v2/Schemas');
3939
$response->assertStatus(200);
40+
41+
$jsonResponse = $response->json();
42+
$this->assertNotEmpty($jsonResponse);
43+
44+
// Find the User schema
45+
$userSchema = null;
46+
foreach ($jsonResponse['Resources'] as $schema) {
47+
if ($schema['id'] === 'urn:ietf:params:scim:schemas:core:2.0:User') {
48+
$userSchema = $schema;
49+
break;
50+
}
51+
}
52+
53+
$this->assertNotNull($userSchema, "User schema not found");
54+
55+
// Find the active attribute in the schema attributes
56+
$activeAttribute = null;
57+
foreach ($userSchema['attributes'] as $attribute) {
58+
if ($attribute['name'] === 'active') {
59+
$activeAttribute = $attribute;
60+
break;
61+
}
62+
}
63+
64+
$this->assertNotNull($activeAttribute, "Active attribute not found");
65+
$this->assertEquals('boolean', $activeAttribute['type'], "Active attribute is not of type boolean");
66+
4067
}
4168

4269
public function getSchemaGenerator(){

0 commit comments

Comments
 (0)