src/Normalizer/ConfigEntityNormalizer.php | 4 ++-- src/ResourceType/ResourceType.php | 16 ++++++++++++++-- tests/src/Functional/ActionTest.php | 4 +--- tests/src/Functional/FieldStorageConfigTest.php | 2 +- tests/src/Functional/NodeTypeTest.php | 2 +- tests/src/Functional/WorkflowTest.php | 2 +- 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Normalizer/ConfigEntityNormalizer.php b/src/Normalizer/ConfigEntityNormalizer.php index 44fb54c..1508ccb 100644 --- a/src/Normalizer/ConfigEntityNormalizer.php +++ b/src/Normalizer/ConfigEntityNormalizer.php @@ -49,8 +49,8 @@ class ConfigEntityNormalizer extends EntityNormalizer { // mandatory "id" attribute in JSON API) // @see http://jsonapi.org/format/#document-resource-identifier-objects $id_field_name = $entity->getEntityType()->getKey('id'); - unset($enabled_public_fields[$id_field_name]); - unset($enabled_public_fields['uuid']); + unset($enabled_public_fields[$resource_type->getPublicName($id_field_name)]); + unset($enabled_public_fields[$resource_type->getPublicName('uuid')]); return $enabled_public_fields; } diff --git a/src/ResourceType/ResourceType.php b/src/ResourceType/ResourceType.php index 3dc15b2..e45ac8e 100644 --- a/src/ResourceType/ResourceType.php +++ b/src/ResourceType/ResourceType.php @@ -118,7 +118,14 @@ class ResourceType { * The public field name. */ public function getPublicName($field_name) { - // By default the entity field name is the public field name. + // By default the entity field name is the public field name. Unless the + // public name is "id" or "type" — then they're prefixed with an underscore, + // because these are reserved by the JSON API spec. + // @see http://jsonapi.org/format/#document-resource-object-fields + if ($field_name === 'id' || $field_name === 'type') { + return "_$field_name"; + } + return $field_name; } @@ -132,7 +139,12 @@ class ResourceType { * The internal field name as defined in the entity. */ public function getInternalName($field_name) { - // By default the entity field name is the public field name. + // By default the entity field name is the public field name. Unless: see + // ::getPublicName(). + if ($field_name === '_id' || $field_name === '_type') { + return substr($field_name, 1); + } + return $field_name; } diff --git a/tests/src/Functional/ActionTest.php b/tests/src/Functional/ActionTest.php index cc57864..e7ef223 100644 --- a/tests/src/Functional/ActionTest.php +++ b/tests/src/Functional/ActionTest.php @@ -91,13 +91,11 @@ class ActionTest extends ResourceTestBase { 'config' => ['user.role.anonymous'], 'module' => ['user'], ], - 'id' => 'user_add_role_action.anonymous', 'label' => 'Add the anonymous role to the selected users', 'langcode' => 'en', 'plugin' => 'user_add_role_action', 'status' => TRUE, - 'type' => 'user', - 'uuid' => $this->entity->uuid(), + '_type' => 'user', ], ], ]; diff --git a/tests/src/Functional/FieldStorageConfigTest.php b/tests/src/Functional/FieldStorageConfigTest.php index 2bf7bf7..cfaaa8b 100644 --- a/tests/src/Functional/FieldStorageConfigTest.php +++ b/tests/src/Functional/FieldStorageConfigTest.php @@ -95,7 +95,7 @@ class FieldStorageConfigTest extends ResourceTestBase { 'settings' => [], 'status' => TRUE, 'translatable' => TRUE, - 'type' => 'boolean', + '_type' => 'boolean', ], ], ]; diff --git a/tests/src/Functional/NodeTypeTest.php b/tests/src/Functional/NodeTypeTest.php index c10fa0e..46bb7d2 100644 --- a/tests/src/Functional/NodeTypeTest.php +++ b/tests/src/Functional/NodeTypeTest.php @@ -90,7 +90,7 @@ class NodeTypeTest extends ResourceTestBase { 'new_revision' => TRUE, 'preview_mode' => 1, 'status' => TRUE, - 'type' => 'camelids', + '_type' => 'camelids', ], ], ]; diff --git a/tests/src/Functional/WorkflowTest.php b/tests/src/Functional/WorkflowTest.php index 0766a80..e5e6923 100644 --- a/tests/src/Functional/WorkflowTest.php +++ b/tests/src/Functional/WorkflowTest.php @@ -94,7 +94,7 @@ class WorkflowTest extends ResourceTestBase { 'label' => 'REST Worklow', 'langcode' => 'en', 'status' => TRUE, - 'type' => 'workflow_type_complex_test', + '_type' => 'workflow_type_complex_test', 'type_settings' => [ 'states' => [ 'draft' => [