second part of fixing Sonar issues (#2295)

* fix sonar issues

* fix csharp model issue

* refactor code
This commit is contained in:
Ramzi Maalej 2019-03-20 03:12:00 -04:00 committed by William Cheng
parent 546a230c73
commit 3100afce26
15 changed files with 46 additions and 38 deletions

View File

@ -86,10 +86,9 @@ public class ConfigHelp implements Runnable {
//noinspection ResultOfMethodCallIgnored
out.getParentFile().mkdirs();
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8));
writer.write(sb.toString());
writer.close();
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8))) {
writer.write(sb.toString());
}
} else {
System.out.print(sb.toString());
}

View File

@ -192,7 +192,7 @@ public class Generator {
return outputFolder;
} catch (Exception e) {
e.printStackTrace();
return null;
throw new RuntimeException("Cannot access tmp folder");
}
}
}

View File

@ -62,17 +62,20 @@ public abstract class AbstractGenerator {
throw new RuntimeException("can't load template " + name);
}
@SuppressWarnings("squid:S2095")
// ignored rule as used in the CLI and it's required to return a reader
public Reader getTemplateReader(String name) {
InputStream is = null;
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(name));
is = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(name));
if (is == null) {
is = new FileInputStream(new File(name)); // May throw but never return a null value
}
return new InputStreamReader(is, "UTF-8");
} catch (Exception e) {
} catch (FileNotFoundException | UnsupportedEncodingException e) {
LOGGER.error(e.getMessage());
throw new RuntimeException("can't load template " + name);
}
throw new RuntimeException("can't load template " + name);
}
private String buildLibraryFilePath(String dir, String library, String file) {

View File

@ -1270,12 +1270,15 @@ public class DefaultCodegen implements CodegenConfig {
}
/**
* Return property value depending on property type
* Return property value depending on property type.
* @param schema property type
* @return property value
*/
@SuppressWarnings("squid:S3923")
private String getPropertyDefaultValue(Schema schema) {
//NOSONAR
/**
* Although all branches return null, this is left intentionally as examples for new contributors
*/
if (ModelUtils.isBooleanSchema(schema)) {
return "null";
} else if (ModelUtils.isDateSchema(schema)) {
@ -4793,6 +4796,9 @@ public class DefaultCodegen implements CodegenConfig {
}
private void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
if(parameter == null || property == null) {
return;
}
parameter.isNullable = property.isNullable;
}

View File

@ -424,14 +424,14 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
private Map<String, List<String>> getAuthScopes(List<SecurityRequirement> securities, Map<String, SecurityScheme> securitySchemes) {
final Map<String, List<String>> scopes = new HashMap<>();
for (SecurityRequirement requirement : securities) {
for (String key : requirement.keySet()) {
SecurityScheme securityScheme = securitySchemes.get(key);
if (securityScheme != null) {
scopes.put(key, requirement.get(key));
Optional.ofNullable(securitySchemes).ifPresent(_securitySchemes -> {
for (SecurityRequirement requirement : securities) {
for (String key : requirement.keySet()) {
Optional.ofNullable(securitySchemes.get(key))
.ifPresent(securityScheme -> scopes.put(key, requirement.get(key)));
}
}
}
});
return scopes;
}
@ -618,7 +618,7 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg
* Collect the scopes to generate a unique identifier for each of them.
*
* @param authMethods the auth methods with their scopes.
* @param scopes the optional auth methods and scopes required by an operation
* @param scopes the optional auth methods and scopes required by an operation
* @return the authMethods to be used by the operation with its required scopes.
*/
private List<CodegenSecurity> postProcessAuthMethod(List<CodegenSecurity> authMethods, Map<String, List<String>> scopes) {

View File

@ -75,10 +75,10 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
public String sanitizeName(String name) {
name = super.sanitizeName(name);
if (name.contains("__")) { // Preventing namespacing
name.replaceAll("__", "_");
name = name.replaceAll("__", "_");
}
if (name.matches("^\\d.*")) { // Prevent named credentials with leading number
name.replaceAll("^\\d.*", "");
name = name.replaceAll("^\\d.*", "");
}
return name;
}
@ -293,7 +293,7 @@ public abstract class AbstractApexCodegen extends DefaultCodegen implements Code
}
} else if (Boolean.TRUE.equals(p.isString)) {
p.example = "'" + p.example + "'";
} else if ("".equals(p.example) || p.example == null && p.dataType != "Object") {
} else if ("".equals(p.example) || p.example == null && "Object".equals(p.dataType)) {
// Get an example object from the generated model
if (!isReservedWord(p.dataType.toLowerCase(Locale.ROOT))) {
p.example = p.dataType + ".getExample()";

View File

@ -540,7 +540,7 @@ public class CSharpClientCodegen extends AbstractCSharpCodegen {
}
for (final CodegenProperty property : codegenModel.readWriteVars) {
if (property.defaultValue == null && property.baseName.equals(parentCodegenModel.discriminator)) {
if (property.defaultValue == null && property.baseName.equals(parentCodegenModel.discriminator.getPropertyName())) {
property.defaultValue = "\"" + name + "\"";
}
}

View File

@ -243,7 +243,7 @@ public class CSharpNetCoreClientCodegen extends AbstractCSharpCodegen {
}
for (final CodegenProperty property : codegenModel.readWriteVars) {
if (property.defaultValue == null && property.baseName.equals(parentCodegenModel.discriminator)) {
if (property.defaultValue == null && property.baseName.equals(parentCodegenModel.discriminator.getPropertyName())) {
property.defaultValue = "\"" + name + "\"";
}
}

View File

@ -584,7 +584,7 @@ public class ElmClientCodegen extends DefaultCodegen implements CodegenConfig {
}
String mapResult = "";
if (maybeMapResult != null) {
if (mapFn == "") {
if ("".equals(mapFn)) {
mapResult = maybeMapResult;
} else {
mapResult = maybeMapResult + (param.required ? " <|" : " <<");

View File

@ -848,17 +848,17 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
}
if (ModelUtils.isArraySchema(model)) {
ArraySchema am = (ArraySchema) model;
if (am.getItems() != null) {
if (codegenModel != null && am.getItems() != null) {
codegenModel.getVendorExtensions().put("x-isArray", true);
codegenModel.getVendorExtensions().put("x-itemType", getSchemaType(am.getItems()));
}
} else if (ModelUtils.isMapSchema(model)) {
if (ModelUtils.getAdditionalProperties(model) != null) {
if (codegenModel != null && ModelUtils.getAdditionalProperties(model) != null) {
codegenModel.getVendorExtensions().put("x-isMap", true);
codegenModel.getVendorExtensions().put("x-itemType", getSchemaType(ModelUtils.getAdditionalProperties(model)));
} else {
String type = model.getType();
if (isPrimitiveType(type)) {
if (codegenModel != null && isPrimitiveType(type)) {
codegenModel.vendorExtensions.put("x-isPrimitive", true);
}
}
@ -1054,11 +1054,13 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
}
}
for (CodegenProperty var : cm.vars) {
if (var == lastRequired) {
var.vendorExtensions.put("x-codegen-hasMoreRequired", false);
} else if (var.required) {
var.vendorExtensions.put("x-codegen-hasMoreRequired", true);
}
Optional.ofNullable(lastRequired).ifPresent(_lastRequired -> {
if (var == _lastRequired) {
var.vendorExtensions.put("x-codegen-hasMoreRequired", false);
} else if (var.required) {
var.vendorExtensions.put("x-codegen-hasMoreRequired", true);
}
});
}
}
return objs;

View File

@ -32,7 +32,7 @@ import java.util.*;
import static org.openapitools.codegen.utils.StringUtils.dashize;
public class JavascriptFlowtypedClientCodegen extends AbstractTypeScriptClientCodegen {
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
private final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
public static final String NPM_NAME = "npmName";
public static final String NPM_VERSION = "npmVersion";

View File

@ -327,7 +327,7 @@ public class PythonAbstractConnexionServerCodegen extends DefaultCodegen impleme
for (String token: pathname.substring(1).split("/")) {
if (token.startsWith("{")) {
String snake_case_token = "{" + this.toParamName(token.substring(1, token.length()-1)) + "}";
if(token != snake_case_token) {
if(!token.equals(snake_case_token)) {
token = snake_case_token;
}
}

View File

@ -36,7 +36,6 @@ import static org.openapitools.codegen.utils.StringUtils.underscore;
public class RubyOnRailsServerCodegen extends AbstractRubyCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(RubyOnRailsServerCodegen.class);
private static final SimpleDateFormat MIGRATE_FILE_NAME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ROOT);
protected String gemName;
protected String moduleName;

View File

@ -1012,10 +1012,10 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
if (bound < 0) {
throw new RuntimeException("Unsigned bound is negative: " + bound);
}
return 65 - Long.numberOfLeadingZeros(bound >> 1);
return 65L - Long.numberOfLeadingZeros(bound >> 1);
}
return 65 - Long.numberOfLeadingZeros(
return 65L - Long.numberOfLeadingZeros(
// signed bounds go from (-n) to (n - 1), i.e. i8 goes from -128 to 127
bound < 0 ? Math.abs(bound) - 1 : bound);
}

View File

@ -102,7 +102,7 @@ public class ScalaAkkaClientCodegen extends AbstractScalaCodegen implements Code
importMapping.put("DateTime", "org.joda.time.DateTime");
typeMapping = new HashMap<String, String>();
typeMapping = new HashMap<>();
typeMapping.put("array", "Seq");
typeMapping.put("set", "Set");
typeMapping.put("boolean", "Boolean");
@ -114,7 +114,6 @@ public class ScalaAkkaClientCodegen extends AbstractScalaCodegen implements Code
typeMapping.put("byte", "Byte");
typeMapping.put("short", "Short");
typeMapping.put("char", "Char");
typeMapping.put("long", "Long");
typeMapping.put("double", "Double");
typeMapping.put("object", "Any");
typeMapping.put("file", "File");