diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
index 448a353..e35540a 100644
--- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
+++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php
@@ -51,7 +51,7 @@ public function get(EntityInterface $entity) {
     }
     foreach ($entity as $field_name => $field) {
       if (!$field->access('view')) {
-        unset($entity->{$field_name});
+        $entity->set($field_name, NULL);
       }
     }
 
diff --git a/core/modules/rest/src/Tests/CreateTest.php b/core/modules/rest/src/Tests/CreateTest.php
index 98b944a..7485fea 100644
--- a/core/modules/rest/src/Tests/CreateTest.php
+++ b/core/modules/rest/src/Tests/CreateTest.php
@@ -195,8 +195,8 @@ public function testCreateNode() {
       }
       else {
         // Changed and revision_timestamp fields can never be added.
-        unset($entity->changed);
-        unset($entity->revision_timestamp);
+        $entity->set('changed', NULL);
+        $entity->set('revision_timestamp', NULL);
       }
 
       $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]);
@@ -249,7 +249,7 @@ public function testCreateUser() {
       }
 
       // Changed field can never be added.
-      unset($entity->changed);
+      $entity->set('changed', NULL);
 
       $serialized = $this->serializer->serialize($entity, $this->defaultFormat, ['account' => $account]);
 
diff --git a/core/modules/rest/src/Tests/UpdateTest.php b/core/modules/rest/src/Tests/UpdateTest.php
index 481e33b..c46d0e3 100644
--- a/core/modules/rest/src/Tests/UpdateTest.php
+++ b/core/modules/rest/src/Tests/UpdateTest.php
@@ -53,7 +53,7 @@ public function testPatchUpdate() {
     ));
     $patch_entity = entity_create($entity_type, $patch_values);
     // We don't want to overwrite the UUID.
-    unset($patch_entity->uuid);
+    $patch_entity->set('uuid', NULL);
     $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, $context);
 
     // Update the entity over the REST API.
