Skip to content

Commit 6793597

Browse files
authored
Merge pull request #1 from Tovkal/issue-9386-code-cleanup
Cleanup Swift 5 classes
2 parents 4293af3 + 14e21b6 commit 6793597

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

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

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

3-
import io.swagger.codegen.*;
3+
import io.swagger.codegen.CliOption;
4+
import io.swagger.codegen.CodegenConfig;
5+
import io.swagger.codegen.CodegenConstants;
6+
import io.swagger.codegen.CodegenModel;
7+
import io.swagger.codegen.CodegenProperty;
8+
import io.swagger.codegen.CodegenType;
9+
import io.swagger.codegen.DefaultCodegen;
10+
import io.swagger.codegen.SupportingFile;
11+
412
import io.swagger.models.Model;
513
import io.swagger.models.ModelImpl;
6-
import io.swagger.models.Operation;
7-
import io.swagger.models.Swagger;
814
import io.swagger.models.properties.ArrayProperty;
915
import io.swagger.models.properties.MapProperty;
1016
import io.swagger.models.properties.Property;
@@ -35,15 +41,15 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
3541
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
3642
public static final String DEFAULT_POD_AUTHORS = "Swagger Codegen";
3743
public static final String LENIENT_TYPE_CAST = "lenientTypeCast";
38-
protected static final String LIBRARY_PROMISE_KIT = "PromiseKit";
39-
protected static final String LIBRARY_RX_SWIFT = "RxSwift";
40-
protected static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
44+
private static final String LIBRARY_PROMISE_KIT = "PromiseKit";
45+
private static final String LIBRARY_RX_SWIFT = "RxSwift";
46+
private static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
4147
protected String projectName = "SwaggerClient";
42-
protected boolean unwrapRequired;
43-
protected boolean objcCompatible = false;
44-
protected boolean lenientTypeCast = false;
48+
private boolean unwrapRequired;
49+
private boolean objcCompatible = false;
50+
private boolean lenientTypeCast = false;
4551
protected boolean swiftUseApiNamespace;
46-
protected String[] responseAs = new String[0];
52+
private String[] responseAs = new String[0];
4753
protected String sourceFolder = "Classes" + File.separator + "Swaggers";
4854

4955
@Override
@@ -454,8 +460,7 @@ public String toDefaultValue(Property prop) {
454460
public String toInstantiationType(Property prop) {
455461
if (prop instanceof MapProperty) {
456462
MapProperty ap = (MapProperty) prop;
457-
String inner = getSwaggerType(ap.getAdditionalProperties());
458-
return inner;
463+
return getSwaggerType(ap.getAdditionalProperties());
459464
} else if (prop instanceof ArrayProperty) {
460465
ArrayProperty ap = (ArrayProperty) prop;
461466
String inner = getSwaggerType(ap.getItems());
@@ -555,7 +560,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
555560
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent,
556561
parentModel,
557562
allDefinitions);
558-
codegenModel = Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);
563+
Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);
559564

560565
// get the next parent
561566
parentSchema = parentCodegenModel.parentSchema;
@@ -631,7 +636,7 @@ public String toEnumVarName(String name, String datatype) {
631636
}
632637

633638
// Camelize only when we have a structure defined below
634-
Boolean camelized = false;
639+
boolean camelized = false;
635640
if (name.matches("[A-Z][a-z0-9]+[a-zA-Z0-9]*")) {
636641
name = camelize(name, true);
637642
camelized = true;
@@ -661,7 +666,7 @@ public String toEnumVarName(String name, String datatype) {
661666

662667
char[] separators = {'-', '_', ' ', ':', '(', ')'};
663668
return camelize(WordUtils.capitalizeFully(StringUtils.lowerCase(name), separators)
664-
.replaceAll("[-_ :\\(\\)]", ""),
669+
.replaceAll("[-_ :()]", ""),
665670
true);
666671
}
667672

@@ -761,8 +766,8 @@ public String escapeUnsafeCharacters(String input) {
761766
return input.replace("*/", "*_/").replace("/*", "/_*");
762767
}
763768

764-
private static CodegenModel reconcileProperties(CodegenModel codegenModel,
765-
CodegenModel parentCodegenModel) {
769+
private static void reconcileProperties(CodegenModel codegenModel,
770+
CodegenModel parentCodegenModel) {
766771
// To support inheritance in this generator, we will analyze
767772
// the parent and child models, look for properties that match, and remove
768773
// them from the child models and leave them in the parent.
@@ -772,7 +777,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
772777
// Get the properties for the parent and child models
773778
final List<CodegenProperty> parentModelCodegenProperties = parentCodegenModel.vars;
774779
List<CodegenProperty> codegenProperties = codegenModel.vars;
775-
codegenModel.allVars = new ArrayList<CodegenProperty>(codegenProperties);
780+
codegenModel.allVars = new ArrayList<>(codegenProperties);
776781
codegenModel.parentVars = parentCodegenModel.allVars;
777782

778783
// Iterate over all of the parent model properties
@@ -784,7 +789,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
784789
Iterator<CodegenProperty> iterator = codegenProperties.iterator();
785790
while (iterator.hasNext()) {
786791
CodegenProperty codegenProperty = iterator.next();
787-
if (codegenProperty.baseName == parentModelCodegenProperty.baseName) {
792+
if (codegenProperty.baseName.equals(parentModelCodegenProperty.baseName)) {
788793
// We found a property in the child class that is
789794
// a duplicate of the one in the parent, so remove it.
790795
iterator.remove();
@@ -799,12 +804,9 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
799804
int numVars = codegenProperties.size();
800805
for (CodegenProperty codegenProperty : codegenProperties) {
801806
count += 1;
802-
codegenProperty.hasMore = (count < numVars) ? true : false;
807+
codegenProperty.hasMore = count < numVars;
803808
}
804809
codegenModel.vars = codegenProperties;
805810
}
806-
807-
808-
return codegenModel;
809811
}
810812
}

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)