Skip to content

Commit e59561c

Browse files
fix(UIDField): use default callable to generate default instead of a fixed default
1 parent 3aadbc2 commit e59561c

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Fields/UIDField.inc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class UIDField extends RESTAPI\Core\Field {
4040
* @param string $help_text Set a description for this field. This description will be used in API documentation.
4141
*/
4242
public function __construct(
43-
string $prefix = '',
43+
public string $prefix = '',
4444
bool $sensitive = false,
4545
bool $representation_only = false,
4646
string $verbose_name = '',
@@ -55,7 +55,7 @@ class UIDField extends RESTAPI\Core\Field {
5555
parent::__construct(
5656
type: 'string',
5757
unique: true,
58-
default: uniqid(prefix: $prefix),
58+
default_callable: 'generate_uid',
5959
editable: false,
6060
read_only: true,
6161
sensitive: $sensitive,
@@ -71,6 +71,10 @@ class UIDField extends RESTAPI\Core\Field {
7171
);
7272
}
7373

74+
public function generate_uid(): string {
75+
return uniqid(prefix: $this->prefix);
76+
}
77+
7478
/**
7579
* Converts the field value to its representation form from it's internal pfSense configuration value.
7680
* @param mixed $internal_value The internal value from the pfSense configuration.

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Tests/APIFieldsUIDFieldTestCase.inc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ class APIFieldsUIDFieldTestCase extends TestCase {
1212
public function test_default(): void {
1313
$field1 = new UIDField();
1414
$field2 = new UIDField();
15-
$this->assert_not_equals($field1->default, $field2->default);
15+
$this->assert_not_equals($field1->get_default(), $field2->get_default());
1616
}
1717

1818
/**
1919
* Checks that the 'prefix' attributes works as intended.
2020
*/
2121
public function test_prefix(): void {
2222
$field = new UIDField(prefix: 'test-');
23-
$this->assert_is_true(str_starts_with($field->default, 'test-'));
23+
$this->assert_is_true(str_starts_with($field->get_default(), 'test-'));
24+
}
25+
26+
/**
27+
* Checks that the generate_uid method correctly generates a UID
28+
*/
29+
public function test_generate_uid(): void {
30+
$field = new UIDField();
31+
$this->assert_is_not_empty($field->generate_uid());
32+
$this->assert_not_equals($field->generate_uid(), $field->generate_uid());
2433
}
2534
}

0 commit comments

Comments
 (0)