Skip to content

Commit 7f78140

Browse files
authored
fix(graphql): fix issue where content types with Collection in the name (dotCMS#32163)
This pull request introduces a constant for the "Collection" string in `TypeUtil` to improve maintainability and adds comprehensive unit tests for its methods to ensure correctness. The changes enhance code readability and robustness. ### Code improvements: * [`dotCMS/src/main/java/com/dotcms/graphql/util/TypeUtil.java`](diffhunk://#diff-b81500a068d7284b7576cc4c9e886ad3266d129ecfa072c6115551112edef8fdR128-R135): Introduced a `COLLECTION` constant to replace the hardcoded "Collection" string in the `collectionizedName` and `singularizeCollectionName` methods, improving maintainability and reducing potential errors. ### Testing enhancements: * [`dotCMS/src/test/java/com/dotcms/graphql/util/TypeUtilTest.java`](diffhunk://#diff-9f4561c3a9170e5e7b2e180e4d26e7a739fd9c1256c8d3efa5d16147e5720749R1-R65): Added a new test class `TypeUtilTest` with unit tests for the `collectionizedName`, `singularizeCollectionName`, and `singularizeBaseTypeCollectionName` methods. These tests cover various edge cases, such as empty strings, multiple suffixes, and strings without the "Collection" suffix, ensuring the methods behave as expected. ref: dotCMS#32159 ### Checklist - [x] Tests - [x] Translations - [x] Security Implications Contemplated (add notes if applicable) ### Additional Info ** any additional useful context or info ** ### Screenshots Original | Updated :-------------------------:|:-------------------------: ** original screenshot ** | ** updated screenshot **
1 parent f3585b5 commit 7f78140

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

dotCMS/src/main/java/com/dotcms/graphql/util/TypeUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,14 @@ public static GraphQLInterfaceType createInterfaceType(final String typeName,
125125
return builder.build();
126126
}
127127

128+
final static String COLLECTION="Collection";
129+
128130
public static String collectionizedName(final String typeName) {
129-
return typeName + "Collection";
131+
return typeName + COLLECTION;
130132
}
131133

132134
public static String singularizeCollectionName(final String collectionName) {
133-
return collectionName.replaceAll("Collection", "");
135+
return collectionName.endsWith(COLLECTION) ? collectionName.substring(0,collectionName.lastIndexOf(COLLECTION)) : collectionName;
134136
}
135137

136138
public static String singularizeBaseTypeCollectionName(final String baseTypeCollectionName) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.dotcms.graphql.util;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TypeUtilTest {
8+
9+
10+
11+
@Test
12+
void testCollectionizedName() {
13+
// Basic functionality
14+
assertEquals("TestCollection", TypeUtil.collectionizedName("Test"));
15+
16+
// With empty string
17+
assertEquals("Collection", TypeUtil.collectionizedName(""));
18+
19+
// Already has Collection suffix
20+
assertEquals("TestCollectionCollection", TypeUtil.collectionizedName("TestCollection"));
21+
}
22+
23+
@Test
24+
void testSingularizeCollectionName() {
25+
// Basic functionality
26+
assertEquals("Test", TypeUtil.singularizeCollectionName("TestCollection"));
27+
28+
// Without Collection suffix
29+
assertEquals("Test", TypeUtil.singularizeCollectionName("Test"));
30+
31+
// Collection in the middle
32+
assertEquals("TestCollectionSuffix", TypeUtil.singularizeCollectionName("TestCollectionSuffix"));
33+
34+
// Collection in the middle and End
35+
assertEquals("TestCollectionSuffix", TypeUtil.singularizeCollectionName("TestCollectionSuffixCollection"));
36+
37+
38+
// Multiple Collections, only removes from end
39+
assertEquals("TestCollection", TypeUtil.singularizeCollectionName("TestCollectionCollection"));
40+
41+
// Empty string
42+
assertEquals("", TypeUtil.singularizeCollectionName(""));
43+
}
44+
45+
@Test
46+
void testSingularizeBaseTypeCollectionName() {
47+
// Both Collection and BaseType
48+
assertEquals("Test", TypeUtil.singularizeBaseTypeCollectionName("TestBaseTypeCollection"));
49+
50+
// Only Collection suffix
51+
assertEquals("Test", TypeUtil.singularizeBaseTypeCollectionName("TestCollection"));
52+
53+
// Only BaseType
54+
assertEquals("Test", TypeUtil.singularizeBaseTypeCollectionName("TestBaseType"));
55+
56+
// Multiple BaseType occurrences
57+
assertEquals("TestTest", TypeUtil.singularizeBaseTypeCollectionName("TestBaseTypeTestBaseType"));
58+
59+
// Neither Collection nor BaseType
60+
assertEquals("Test", TypeUtil.singularizeBaseTypeCollectionName("Test"));
61+
62+
// Empty string
63+
assertEquals("", TypeUtil.singularizeBaseTypeCollectionName(""));
64+
}
65+
}

0 commit comments

Comments
 (0)