Skip to content

Commit a9e12ea

Browse files
authoredMar 21, 2025
Prevent null values from being saved to data (#412)
1 parent 01acf35 commit a9e12ea

File tree

9 files changed

+56
-4
lines changed

9 files changed

+56
-4
lines changed
 

‎src/Entries/Entry.php

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public static function makeModelFromContract(EntryContract $source)
132132
$attributes['data']->put('template', $source->template);
133133
}
134134

135+
$attributes['data'] = $attributes['data']->filter(fn ($v) => $v !== null);
136+
135137
foreach ($dataMappings as $key) {
136138
$attributes[$key] = $data->get($key);
137139
}

‎src/Forms/Form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function makeModelFromContract(Contract $source)
4040
'store' => $source->store(),
4141
'email' => $source->email(),
4242
'honeypot' => $source->honeypot(),
43-
'data' => $source->data(),
43+
'data' => $source->data()->filter(fn ($v) => $v !== null),
4444
],
4545
]);
4646
}

‎src/Forms/Submission.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function toModel()
3535
$model = $class::findOrNew($this->id());
3636

3737
return (! empty($model->id)) ? $model->fill([
38-
'data' => $this->data,
38+
'data' => $this->data->filter(fn ($v) => $v !== null),
3939
]) : $model->fill([
4040
'id' => $this->id(),
4141
'data' => $this->data,

‎src/Globals/Variables.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function makeModelFromContract(Contract $source)
3939
'handle' => $source->globalSet()->handle(),
4040
'locale' => $source->locale,
4141
])->fill([
42-
'data' => $data,
42+
'data' => $data->filter(fn ($v) => $v !== null),
4343
'origin' => $source->hasOrigin() ? $source->origin()->locale() : null,
4444
]);
4545
}

‎src/Taxonomies/Term.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function makeModelFromContract(Contract $source)
8282
])->fill([
8383
'slug' => $source->slug(),
8484
'uri' => $source->uri(),
85-
'data' => $data,
85+
'data' => collect($data)->filter(fn ($v) => $v !== null),
8686
'updated_at' => $source->lastModified(),
8787
]);
8888
}

‎tests/Entries/EntryTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,22 @@ public function saving_an_entry_updates_the_uri()
328328
$this->assertSame('/the-new-slug', $entry->uri());
329329
$this->assertSame('/the-new-slug', $entry->model()->uri);
330330
}
331+
332+
#[Test]
333+
public function null_values_are_removed_from_data()
334+
{
335+
Collection::make('blog')->title('blog')
336+
->routes('{parent_uri}/{slug}')
337+
->save();
338+
339+
$entry = (new Entry)
340+
->id('1.0')
341+
->collection('blog')
342+
->slug('the-slug')
343+
->data(['foo' => 'bar', 'null_value' => null]);
344+
345+
$entry->save();
346+
347+
$this->assertArrayNotHasKey('null_value', $entry->model()->data);
348+
}
331349
}

‎tests/Forms/FormSubmissionTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,18 @@ public function it_should_not_save_date_in_data()
101101
$this->assertInstanceOf(Carbon::class, $fresh->date());
102102
$this->assertSame($fresh->date()->format('u'), $submission->date()->format('u'));
103103
}
104+
105+
#[Test]
106+
public function null_values_are_removed_from_data()
107+
{
108+
$form = tap(Facades\Form::make('test')->title('Test'))
109+
->save();
110+
111+
$submission = tap($form->makeSubmission([
112+
'name' => 'John Doe',
113+
'null_value' => null,
114+
]))->save();
115+
116+
$this->assertArrayNotHasKey('null_value', $submission->model()->data);
117+
}
104118
}

‎tests/Forms/FormTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@ public function it_stores_form_data()
7171

7272
$this->assertSame(['some' => 'data'], Arr::get($form->model(), 'settings.data'));
7373
}
74+
75+
#[Test]
76+
public function null_values_are_removed_from_data()
77+
{
78+
$form = tap(Facades\Form::make('test')->title('Test form')->data(['some' => 'data', 'null_value' => null]))->save();
79+
80+
$this->assertSame(['some' => 'data'], Arr::get($form->model(), 'settings.data'));
81+
}
7482
}

‎tests/Terms/TermTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function it_doesnt_create_a_new_model_when_slug_is_changed()
3030
$this->assertSame('new-slug', TermModel::first()->slug);
3131
}
3232

33+
#[Test]
34+
public function null_values_are_removed_from_data()
35+
{
36+
Taxonomy::make('test')->title('test')->save();
37+
38+
$term = tap(TermFacade::make('test-term')->taxonomy('test')->data(['null_value' => null]))->save();
39+
40+
$this->assertArrayNotHasKey('null_value', $term->model()->data);
41+
}
42+
3343
#[Test]
3444
public function it_saves_updated_at_value_correctly()
3545
{

0 commit comments

Comments
 (0)