Skip to content

Commit

Permalink
Merge pull request #1 from Tovkal/issue-9386-code-cleanup
Browse files Browse the repository at this point in the history
Cleanup Swift 5 classes
  • Loading branch information
plam4u authored Oct 24, 2019
2 parents 4293af3 + 14e21b6 commit 6793597
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.swagger.codegen.languages;

import io.swagger.codegen.*;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.CodegenType;
import io.swagger.codegen.DefaultCodegen;
import io.swagger.codegen.SupportingFile;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
Expand Down Expand Up @@ -35,15 +41,15 @@ public class Swift5Codegen extends DefaultCodegen implements CodegenConfig {
public static final String SWIFT_USE_API_NAMESPACE = "swiftUseApiNamespace";
public static final String DEFAULT_POD_AUTHORS = "Swagger Codegen";
public static final String LENIENT_TYPE_CAST = "lenientTypeCast";
protected static final String LIBRARY_PROMISE_KIT = "PromiseKit";
protected static final String LIBRARY_RX_SWIFT = "RxSwift";
protected static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
private static final String LIBRARY_PROMISE_KIT = "PromiseKit";
private static final String LIBRARY_RX_SWIFT = "RxSwift";
private static final String[] RESPONSE_LIBRARIES = {LIBRARY_PROMISE_KIT, LIBRARY_RX_SWIFT};
protected String projectName = "SwaggerClient";
protected boolean unwrapRequired;
protected boolean objcCompatible = false;
protected boolean lenientTypeCast = false;
private boolean unwrapRequired;
private boolean objcCompatible = false;
private boolean lenientTypeCast = false;
protected boolean swiftUseApiNamespace;
protected String[] responseAs = new String[0];
private String[] responseAs = new String[0];
protected String sourceFolder = "Classes" + File.separator + "Swaggers";

@Override
Expand Down Expand Up @@ -454,8 +460,7 @@ public String toDefaultValue(Property prop) {
public String toInstantiationType(Property prop) {
if (prop instanceof MapProperty) {
MapProperty ap = (MapProperty) prop;
String inner = getSwaggerType(ap.getAdditionalProperties());
return inner;
return getSwaggerType(ap.getAdditionalProperties());
} else if (prop instanceof ArrayProperty) {
ArrayProperty ap = (ArrayProperty) prop;
String inner = getSwaggerType(ap.getItems());
Expand Down Expand Up @@ -555,7 +560,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent,
parentModel,
allDefinitions);
codegenModel = Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);
Swift5Codegen.reconcileProperties(codegenModel, parentCodegenModel);

// get the next parent
parentSchema = parentCodegenModel.parentSchema;
Expand Down Expand Up @@ -631,7 +636,7 @@ public String toEnumVarName(String name, String datatype) {
}

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

char[] separators = {'-', '_', ' ', ':', '(', ')'};
return camelize(WordUtils.capitalizeFully(StringUtils.lowerCase(name), separators)
.replaceAll("[-_ :\\(\\)]", ""),
.replaceAll("[-_ :()]", ""),
true);
}

Expand Down Expand Up @@ -761,8 +766,8 @@ public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}

private static CodegenModel reconcileProperties(CodegenModel codegenModel,
CodegenModel parentCodegenModel) {
private static void reconcileProperties(CodegenModel codegenModel,
CodegenModel parentCodegenModel) {
// To support inheritance in this generator, we will analyze
// the parent and child models, look for properties that match, and remove
// them from the child models and leave them in the parent.
Expand All @@ -772,7 +777,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
// Get the properties for the parent and child models
final List<CodegenProperty> parentModelCodegenProperties = parentCodegenModel.vars;
List<CodegenProperty> codegenProperties = codegenModel.vars;
codegenModel.allVars = new ArrayList<CodegenProperty>(codegenProperties);
codegenModel.allVars = new ArrayList<>(codegenProperties);
codegenModel.parentVars = parentCodegenModel.allVars;

// Iterate over all of the parent model properties
Expand All @@ -784,7 +789,7 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
Iterator<CodegenProperty> iterator = codegenProperties.iterator();
while (iterator.hasNext()) {
CodegenProperty codegenProperty = iterator.next();
if (codegenProperty.baseName == parentModelCodegenProperty.baseName) {
if (codegenProperty.baseName.equals(parentModelCodegenProperty.baseName)) {
// We found a property in the child class that is
// a duplicate of the one in the parent, so remove it.
iterator.remove();
Expand All @@ -799,12 +804,9 @@ private static CodegenModel reconcileProperties(CodegenModel codegenModel,
int numVars = codegenProperties.size();
for (CodegenProperty codegenProperty : codegenProperties) {
count += 1;
codegenProperty.hasMore = (count < numVars) ? true : false;
codegenProperty.hasMore = count < numVars;
}
codegenModel.vars = codegenProperties;
}


return codegenModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,66 @@
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import org.junit.experimental.theories.suppliers.TestedOn;
import org.testng.Assert;
import org.testng.annotations.Test;

public class Swift5CodegenTest {

Swift5Codegen swiftCodegen = new Swift5Codegen();
private Swift5Codegen swiftCodegen = new Swift5Codegen();

@Test
public void testCapitalizedReservedWord() throws Exception {
public void testCapitalizedReservedWord() {
Assert.assertEquals(swiftCodegen.toEnumVarName("AS", null), "_as");
}

@Test
public void testReservedWord() throws Exception {
public void testReservedWord() {
Assert.assertEquals(swiftCodegen.toEnumVarName("Public", null), "_public");
}

@Test
public void shouldNotBreakNonReservedWord() throws Exception {
public void shouldNotBreakNonReservedWord() {
Assert.assertEquals(swiftCodegen.toEnumVarName("Error", null), "error");
}

@Test
public void shouldNotBreakCorrectName() throws Exception {
public void shouldNotBreakCorrectName() {
Assert.assertEquals(swiftCodegen.toEnumVarName("EntryName", null), "entryName");
}

@Test
public void testSingleWordAllCaps() throws Exception {
public void testSingleWordAllCaps() {
Assert.assertEquals(swiftCodegen.toEnumVarName("VALUE", null), "value");
}

@Test
public void testSingleWordLowercase() throws Exception {
public void testSingleWordLowercase() {
Assert.assertEquals(swiftCodegen.toEnumVarName("value", null), "value");
}

@Test
public void testCapitalsWithUnderscore() throws Exception {
public void testCapitalsWithUnderscore() {
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY_NAME", null), "entryName");
}

@Test
public void testCapitalsWithDash() throws Exception {
public void testCapitalsWithDash() {
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY-NAME", null), "entryName");
}

@Test
public void testCapitalsWithSpace() throws Exception {
public void testCapitalsWithSpace() {
Assert.assertEquals(swiftCodegen.toEnumVarName("ENTRY NAME", null), "entryName");
}

@Test
public void testLowercaseWithUnderscore() throws Exception {
public void testLowercaseWithUnderscore() {
Assert.assertEquals(swiftCodegen.toEnumVarName("entry_name", null), "entryName");
}

@Test
public void testStartingWithNumber() throws Exception {
public void testStartingWithNumber() {
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName", null), "_123entryName");
Assert.assertEquals(swiftCodegen.toEnumVarName("123Entry_name", null), "_123entryName");
Assert.assertEquals(swiftCodegen.toEnumVarName("123EntryName123", null), "_123entryName123");
Expand Down Expand Up @@ -98,7 +99,7 @@ public void dateTest() {
}

@Test
public void testDefaultPodAuthors() throws Exception {
public void testDefaultPodAuthors() {
// Given

// When
Expand All @@ -110,7 +111,7 @@ public void testDefaultPodAuthors() throws Exception {
}

@Test
public void testPodAuthors() throws Exception {
public void testPodAuthors() {
// Given
final String swaggerDevs = "Swagger Devs";
swiftCodegen.additionalProperties().put(Swift5Codegen.POD_AUTHORS, swaggerDevs);
Expand All @@ -129,7 +130,7 @@ public void testInitialConfigValues() throws Exception {
codegen.processOpts();

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.TRUE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), true);
Assert.assertTrue(codegen.isHideGenerationTimestamp());
}

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

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertFalse(codegen.isHideGenerationTimestamp());
}

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

Assert.assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
Assert.assertEquals(codegen.isHideGenerationTimestamp(), false);
Assert.assertFalse(codegen.isHideGenerationTimestamp());
}

}

0 comments on commit 6793597

Please sign in to comment.