Skip to content

Commit d1f66b0

Browse files
committed
Cleanup Swift 5 classes
1 parent 42ad411 commit d1f66b0

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

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

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package io.swagger.codegen.languages;
22

3-
import com.google.common.base.Predicate;
4-
import com.google.common.collect.Iterators;
5-
import com.google.common.collect.Lists;
6-
73
import io.swagger.codegen.CliOption;
84
import io.swagger.codegen.CodegenConfig;
95
import io.swagger.codegen.CodegenConstants;
@@ -15,10 +11,6 @@
1511

1612
import io.swagger.models.Model;
1713
import io.swagger.models.ModelImpl;
18-
import io.swagger.models.Operation;
19-
import io.swagger.models.Swagger;
20-
import io.swagger.models.parameters.HeaderParameter;
21-
import io.swagger.models.parameters.Parameter;
2214
import io.swagger.models.properties.ArrayProperty;
2315
import io.swagger.models.properties.MapProperty;
2416
import io.swagger.models.properties.Property;
@@ -37,7 +29,6 @@
3729
import java.util.Map;
3830
import java.util.regex.Matcher;
3931
import java.util.regex.Pattern;
40-
import javax.annotation.Nullable;
4132

4233
public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
4334
public static final String PROJECT_NAME = "projectName";
@@ -57,15 +48,15 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
5748
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
5849
public static final String DEFAULT_POD_AUTHORS = "Swagger Codegen";
5950
public static final String LENIENT_TYPE_CAST = "lenientTypeCast";
60-
protected static final String LIBRARY_PROMISE_KIT = "PromiseKit";
61-
protected static final String LIBRARY_RX_SWIFT = "RxSwift";
62-
protected static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
51+
private static final String LIBRARY_PROMISE_KIT = "PromiseKit";
52+
private static final String LIBRARY_RX_SWIFT = "RxSwift";
53+
private static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
6354
protected String projectName = "SwaggerClient";
64-
protected boolean unwrapRequired;
65-
protected boolean objcCompatible = false;
66-
protected boolean lenientTypeCast = false;
55+
private boolean unwrapRequired;
56+
private boolean objcCompatible = false;
57+
private boolean lenientTypeCast = false;
6758
protected boolean swiftUseApiNamespace;
68-
protected String[] responseAs = new String[0];
59+
private String[] responseAs = new String[0];
6960
protected String sourceFolder = "Classes" + File.separator + "Swaggers";
7061

7162
@Override
@@ -476,8 +467,7 @@ public String toDefaultValue(Property prop) {
476467
public String toInstantiationType(Property prop) {
477468
if (prop instanceof MapProperty) {
478469
MapProperty ap = (MapProperty) prop;
479-
String inner = getSwaggerType(ap.getAdditionalProperties());
480-
return inner;
470+
return getSwaggerType(ap.getAdditionalProperties());
481471
} else if (prop instanceof ArrayProperty) {
482472
ArrayProperty ap = (ArrayProperty) prop;
483473
String inner = getSwaggerType(ap.getItems());
@@ -577,7 +567,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
577567
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent,
578568
parentModel,
579569
allDefinitions);
580-
codegenModel = Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);
570+
Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);
581571

582572
// get the next parent
583573
parentSchema = parentCodegenModel.parentSchema;
@@ -642,7 +632,7 @@ public String toEnumVarName(String name, String datatype) {
642632
}
643633

644634
// Camelize only when we have a structure defined below
645-
Boolean camelized = false;
635+
boolean camelized = false;
646636
if (name.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) {
647637
name = camelize(name, true);
648638
camelized = true;
@@ -672,7 +662,7 @@ public String toEnumVarName(String name, String datatype) {
672662

673663
char[] separators = {'-', '_', ' ', ':', '(', ')'};
674664
return camelize(WordUtils.capitalizeFully(StringUtils.lowerCase(name), separators)
675-
.replaceAll("[-_ :\\(\\)]", ""),
665+
.replaceAll("[-_ :()]", ""),
676666
true);
677667
}
678668

@@ -772,8 +762,8 @@ public String escapeUnsafeCharacters(String input) {
772762
return input.replace("*/", "*_/").replace("/*", "/_*");
773763
}
774764

775-
private static CodegenModel reconcileProperties(CodegenModel codegenModel,
776-
CodegenModel parentCodegenModel) {
765+
private static void reconcileProperties(CodegenModel codegenModel,
766+
CodegenModel parentCodegenModel) {
777767
// To support inheritance in this generator, we will analyze
778768
// the parent and child models, look for properties that match, and remove
779769
// them from the child models and leave them in the parent.
@@ -783,7 +773,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
783773
// Get the properties for the parent and child models
784774
final List<CodegenProperty> parentModelCodegenProperties = parentCodegenModel.vars;
785775
List<CodegenProperty> codegenProperties = codegenModel.vars;
786-
codegenModel.allVars = new ArrayList<CodegenProperty>(codegenProperties);
776+
codegenModel.allVars = new ArrayList<>(codegenProperties);
787777
codegenModel.parentVars = parentCodegenModel.allVars;
788778

789779
// Iterate over all of the parent model properties
@@ -795,7 +785,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
795785
Iterator<CodegenProperty> iterator = codegenProperties.iterator();
796786
while (iterator.hasNext()) {
797787
CodegenProperty codegenProperty = iterator.next();
798-
if (codegenProperty.baseName == parentModelCodegenProperty.baseName) {
788+
if (codegenProperty.baseName.equals(parentModelCodegenProperty.baseName)) {
799789
// We found a property in the child class that is
800790
// a duplicate of the one in the parent, so remove it.
801791
iterator.remove();
@@ -810,12 +800,9 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
810800
int numVars = codegenProperties.size();
811801
for (CodegenProperty codegenProperty : codegenProperties) {
812802
count += 1;
813-
codegenProperty.hasMore = (count < numVars) ? true : false;
803+
codegenProperty.hasMore = count < numVars;
814804
}
815805
codegenModel.vars = codegenProperties;
816806
}
817-
818-
819-
return codegenModel;
820807
}
821808
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/swift5/Swift5CodegenTest.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,66 @@
77
import io.swagger.models.Operation;
88
import io.swagger.models.Swagger;
99
import io.swagger.parser.SwaggerParser;
10+
import org.junit.experimental.theories.suppliers.TestedOn;
1011
import org.testng.Assert;
1112
import org.testng.annotations.Test;
1213

1314
public class Swift5CodegenTest {
1415

15-
Swift5Codegen swiftCodegen = new Swift5Codegen();
16+
private Swift5Codegen swiftCodegen = new Swift5Codegen();
1617

1718
@Test
18-
public void testCapitalizedReservedWord() throws Exception {
19+
public void testCapitalizedReservedWord() {
1920
Assert.assertEquals(swiftCodegen.toEnumVarName("AS", null), "_as");
2021
}
2122

2223
@Test
23-
public void testReservedWord() throws Exception {
24+
public void testReservedWord() {
2425
Assert.assertEquals(swiftCodegen.toEnumVarName("Public", null), "_public");
2526
}
2627

2728
@Test
28-
public void shouldNotBreakNonReservedWord() throws Exception {
29+
public void shouldNotBreakNonReservedWord() {
2930
Assert.assertEquals(swiftCodegen.toEnumVarName("Error", null), "error");
3031
}
3132

3233
@Test
33-
public void shouldNotBreakCorrectName() throws Exception {
34+
public void shouldNotBreakCorrectName() {
3435
Assert.assertEquals(swiftCodegen.toEnumVarName("EntryName", null), "entryName");
3536
}
3637

3738
@Test
38-
public void testSingleWordAllCaps() throws Exception {
39+
public void testSingleWordAllCaps() {
3940
Assert.assertEquals(swiftCodegen.toEnumVarName("VALUE", null), "value");
4041
}
4142

4243
@Test
43-
public void testSingleWordLowercase() throws Exception {
44+
public void testSingleWordLowercase() {
4445
Assert.assertEquals(swiftCodegen.toEnumVarName("value", null), "value");
4546
}
4647

4748
@Test
48-
public void testCapitalsWithUnderscore() throws Exception {
49+
public void testCapitalsWithUnderscore() {
4950
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY_NAME", null), "entryName");
5051
}
5152

5253
@Test
53-
public void testCapitalsWithDash() throws Exception {
54+
public void testCapitalsWithDash() {
5455
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY-NAME", null), "entryName");
5556
}
5657

5758
@Test
58-
public void testCapitalsWithSpace() throws Exception {
59+
public void testCapitalsWithSpace() {
5960
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY NAME", null), "entryName");
6061
}
6162

6263
@Test
63-
public void testLowercaseWithUnderscore() throws Exception {
64+
public void testLowercaseWithUnderscore() {
6465
Assert.assertEquals(swiftCodegen.toEnumVarName("entry_name", null), "entryName");
6566
}
6667

6768
@Test
68-
public void testStartingWithNumber() throws Exception {
69+
public void testStartingWithNumber() {
6970
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName", null), "_123entryName");
7071
Assert.assertEquals(swiftCodegen.toEnumVarName("123Entry_name", null), "_123entryName");
7172
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName123", null), "_123entryName123");
@@ -98,7 +99,7 @@ public void dateTest() {
9899
}
99100

100101
@Test
101-
public void testDefaultPodAuthors() throws Exception {
102+
public void testDefaultPodAuthors() {
102103
// Given
103104

104105
// When
@@ -110,7 +111,7 @@ public void testDefaultPodAuthors() throws Exception {
110111
}
111112

112113
@Test
113-
public void testPodAuthors() throws Exception {
114+
public void testPodAuthors() {
114115
// Given
115116
final String swaggerDevs = "Swagger Devs";
116117
swiftCodegen.additionalProperties().put(Swift5Codegen.POD_AUTHORS, swaggerDevs);
@@ -129,7 +130,7 @@ public void testInitialConfigValues() throws Exception {
129130
codegen.processOpts();
130131

131132
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
132-
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
133+
Assert.assertTrue(codegen.isHideGenerationTimestamp());
133134
}
134135

135136
@Test
@@ -139,7 +140,7 @@ public void testSettersForConfigValues() throws Exception {
139140
codegen.processOpts();
140141

141142
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
142-
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
143+
Assert.assertFalse(codegen.isHideGenerationTimestamp());
143144
}
144145

145146
@Test
@@ -149,7 +150,6 @@ public void testAdditionalPropertiesPutForConfigValues() throws Exception {
149150
codegen.processOpts();
150151

151152
Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
152-
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
153+
Assert.assertFalse(codegen.isHideGenerationTimestamp());
153154
}
154-
155155
}

0 commit comments

Comments
 (0)