Skip to content

Commit

Permalink
fix: do not load Model objects from internal in Endpoint::__construct()
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhendrickson13 committed Jul 6, 2024
1 parent 21aa9cb commit 156c8a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class Endpoint {

# Initialize the assigned model
$model_class = 'RESTAPI\\Models\\' . $this->model_name;
$this->model = new $model_class();
$this->model = new $model_class(skip_init: true);

# Set the default OpenAPI tag for this endpoint if no tag was explicitly assigned
$this->set_default_tag();
Expand Down Expand Up @@ -650,6 +650,7 @@ class Endpoint {
$resp_data = null;
$content_handler = new JSONContentHandler();
$this->restapi_settings = new RESTAPISettings();
$this->model = new $this->model();

try {
# First, ensure the API is enabled before allowing the call
Expand Down
17 changes: 17 additions & 0 deletions pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Core/Model.inc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ class Model {
*/
public string $update_strategy = 'merge';

/**
* @var bool $skip_init
* If set to `true`, this Model object will not be loaded from its internal representation during construction. This
* is useful when you need to create a Model object to be used as a reference without trigger logic that would normally
* occur by loading the object from its internal representation. This must be set as a parameter during object construction
* to have an effect.
*/
public bool $skip_init = false;

/**
* @var string $verbose_name
* Sets the human-readable name for this Model. This value is primarily used where this Model is referenced in Forms
Expand Down Expand Up @@ -333,6 +342,9 @@ class Model {
$this->client = $options['client'] ?: $this->get_default_client();
unset($options['client']);

# Obtain the skip_init flag if given. Ensure this defaults to false.
$this->skip_init = ($options['skip_init']) ?? false;

return $options;
}

Expand All @@ -341,6 +353,11 @@ class Model {
* @param mixed $id The ID of the internal object to construct this object from
*/
private function construct_from_internal(mixed $id = null, mixed $parent_id = null): void {
# Do not try to load the object from internal if the skip_init flag is set
if ($this->skip_init) {
return;
}

# Obtain the parent Model if a parent_id was provided and this Model has a parent Model class assigned
if (isset($parent_id) and $this->parent_model_class) {
$this->parent_id = $parent_id;
Expand Down

0 comments on commit 156c8a0

Please sign in to comment.