Skip to content

Commit 70d9876

Browse files
committed
Merge pull request #1436 from tomekc/swift_enum_naming
Swift enum names capitalization follows Swift convention.
2 parents ea7ef99 + 6247dd0 commit 70d9876

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwiftCodegen.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.swagger.models.properties.Property;
1616
import org.apache.commons.lang.ArrayUtils;
1717
import org.apache.commons.lang.StringUtils;
18+
import org.apache.commons.lang.WordUtils;
1819

1920
import javax.annotation.Nullable;
2021
import java.util.*;
@@ -236,7 +237,7 @@ public CodegenProperty fromProperty(String name, Property p) {
236237
List<String> values = (List<String>) codegenProperty.allowableValues.get("values");
237238
for (String value : values) {
238239
Map<String, String> map = new HashMap<String, String>();
239-
map.put("enum", StringUtils.capitalize(value));
240+
map.put("enum", toSwiftyEnumName(value));
240241
map.put("raw", value);
241242
swiftEnums.add(map);
242243
}
@@ -250,6 +251,16 @@ public CodegenProperty fromProperty(String name, Property p) {
250251
return codegenProperty;
251252
}
252253

254+
public String toSwiftyEnumName(String value) {
255+
// Prevent from breaking properly cased identifier
256+
if (value.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) {
257+
return value;
258+
}
259+
char[] separators = {'-', '_', ' '};
260+
return WordUtils.capitalizeFully(StringUtils.lowerCase(value), separators).replaceAll("[-_ ]", "");
261+
}
262+
263+
253264
@Override
254265
public String toApiName(String name) {
255266
if(name.length() == 0)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.swagger.codegen.languages;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
public class SwiftCodegenTest {
7+
8+
SwiftCodegen swiftCodegen = new SwiftCodegen();
9+
10+
@Test
11+
public void shouldNotBreakCorrectName() throws Exception {
12+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("EntryName"), "EntryName");
13+
}
14+
15+
@Test
16+
public void testSingleWordAllCaps() throws Exception {
17+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("VALUE"), "Value");
18+
}
19+
20+
@Test
21+
public void testSingleWordLowercase() throws Exception {
22+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("value"), "Value");
23+
}
24+
25+
@Test
26+
public void testCapitalsWithUnderscore() throws Exception {
27+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("ENTRY_NAME"), "EntryName");
28+
}
29+
30+
@Test
31+
public void testCapitalsWithDash() throws Exception {
32+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("ENTRY-NAME"), "EntryName");
33+
}
34+
35+
@Test
36+
public void testCapitalsWithSpace() throws Exception {
37+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("ENTRY NAME"), "EntryName");
38+
}
39+
40+
@Test
41+
public void testLowercaseWithUnderscore() throws Exception {
42+
Assert.assertEquals(swiftCodegen.toSwiftyEnumName("entry_name"), "EntryName");
43+
}
44+
45+
}

0 commit comments

Comments
 (0)