From 5793eff8040c04b33afe31932b050fb6f0beb578 Mon Sep 17 00:00:00 2001
From: Arnaud MONCEL <arnaud.moncel43@gmail.com>
Date: Mon, 26 Jun 2023 10:17:05 +0200
Subject: [PATCH] fix(rename field decorator): throw when put space inside
 fieldName (#748)

---
 .../src/decorators/rename-field/collection.ts             | 8 ++++++++
 .../test/decorators/rename-field/collection.test.ts       | 7 +++++++
 2 files changed, 15 insertions(+)

diff --git a/packages/datasource-customizer/src/decorators/rename-field/collection.ts b/packages/datasource-customizer/src/decorators/rename-field/collection.ts
index a558220543..08c184fae4 100644
--- a/packages/datasource-customizer/src/decorators/rename-field/collection.ts
+++ b/packages/datasource-customizer/src/decorators/rename-field/collection.ts
@@ -35,6 +35,14 @@ export default class RenameFieldCollectionDecorator extends CollectionDecorator
 
     let initialName = currentName;
 
+    if (/ /.test(newName)) {
+      const sanitizedName = newName.replace(/ (.)/g, (_, s) => s.toUpperCase());
+      throw new Error(
+        `The renaming of field '${currentName}' must not contain space.` +
+          ` Something like '${sanitizedName}' should work has expected.`,
+      );
+    }
+
     // Revert previous renaming (avoids conflicts and need to recurse on this.toSubCollection).
     if (this.toChildCollection[currentName]) {
       const childName = this.toChildCollection[currentName];
diff --git a/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts b/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts
index 8470611809..9290f1c5d3 100644
--- a/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts
+++ b/packages/datasource-customizer/test/decorators/rename-field/collection.test.ts
@@ -140,6 +140,13 @@ describe('RenameFieldCollectionDecorator', () => {
     expect(() => newPersons.renameField('id', 'primaryKey')).toThrow(`No such field 'id'`);
   });
 
+  test('should throw when renaming with a name including space', () => {
+    expect(() => newPersons.renameField('id', 'the key')).toThrow(
+      `The renaming of field 'id' must not contain space.` +
+        ` Something like 'theKey' should work has expected.`,
+    );
+  });
+
   test('should allow renaming multiple times the same field', () => {
     newPersons.renameField('id', 'key');
     newPersons.renameField('key', 'primaryKey');