diff --git a/core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php b/core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php index ba609359dda94883bd7ffb1022f39bf183338989..400d31a6105c3776ea7fa6c3433b83045dfe8701 100644 --- a/core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php +++ b/core/modules/jsonapi/src/ResourceType/ResourceTypeRepository.php @@ -233,8 +233,8 @@ protected static function getFieldMapping(array $field_names, EntityTypeInterfac foreach (array_diff($field_names, array_keys($mapping)) as $field_name) { if ($field_name === 'id' || $field_name === 'type') { $alias = $entity_type->id() . '_' . $field_name; - if (isset($field_name[$alias])) { - throw new \LogicException('The generated alias conflicts with an existing field. Please report this in the JSON:API issue queue!'); + if (in_array($alias, $field_names, TRUE)) { + throw new \LogicException("The generated alias '{$alias}' for field name '{$field_name}' conflicts with an existing field. Please report this in the JSON:API issue queue!"); } $mapping[$field_name] = $alias; continue; diff --git a/core/modules/jsonapi/tests/src/Kernel/ResourceType/ResourceTypeRepositoryTest.php b/core/modules/jsonapi/tests/src/Kernel/ResourceType/ResourceTypeRepositoryTest.php index a87ca929dff7feb32c702e8582acd17a5736361c..011ef97c85c1e595405a41bacdf28eaac7732ce0 100644 --- a/core/modules/jsonapi/tests/src/Kernel/ResourceType/ResourceTypeRepositoryTest.php +++ b/core/modules/jsonapi/tests/src/Kernel/ResourceType/ResourceTypeRepositoryTest.php @@ -111,4 +111,40 @@ public function testCaching() { $this->assertCount(4, $this->resourceTypeRepository->get('node', 'article')->getRelatableResourceTypesByField('field_relationship')); } + /** + * Ensures that a naming conflict in the mapping causes an exception to be + * thrown. + * + * @covers ::getFieldMapping + * @dataProvider getFieldMappingProvider + */ + public function testMappingNameConflictCheck($field_name_list) { + $entity_type = \Drupal::entityTypeManager()->getDefinition('node'); + $bundle = 'article'; + $reflection_class = new \ReflectionClass($this->resourceTypeRepository); + $reflection_method = $reflection_class->getMethod('getFieldMapping'); + $reflection_method->setAccessible(TRUE); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage("The generated alias '{$field_name_list[1]}' for field name '{$field_name_list[0]}' conflicts with an existing field. Please report this in the JSON:API issue queue!"); + $reflection_method->invokeArgs($this->resourceTypeRepository, [$field_name_list, $entity_type, $bundle]); + } + + /** + * Data provider for testGetFieldMapping. + * + * These field name lists are designed to trigger a naming conflict in the + * mapping: the special-cased names "type" or "id", and the name + * "{$entity_type_id}_type" or "{$entity_type_id}_id", respectively. + * + * @returns array + * The data for the test method. + */ + public function getFieldMappingProvider() { + return [ + [['type', 'node_type']], + [['id', 'node_id']], + ]; + } + }