Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing various issues reported by Sonar (part 2) #2295

Merged
merged 3 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ public void run() {
//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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static File getTmpFolder() {
return outputFolder;
} catch (Exception e) {
e.printStackTrace();
return null;
throw new RuntimeException("Cannot access tmp folder");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ public String readTemplate(String name) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,12 +1270,15 @@ public String toDefaultValue(Schema schema) {
}

/**
* Return property value depending on property type
* Return property value depending on property type.
* @param schema property type
* @return property value
*/
@SuppressWarnings("squid:S3923")
jimschubert marked this conversation as resolved.
Show resolved Hide resolved
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)) {
Expand Down Expand Up @@ -4793,6 +4796,9 @@ public List<CodegenServerVariable> fromServerVariables(Map<String, ServerVariabl
}

private void setParameterNullable(CodegenParameter parameter, CodegenProperty property) {
if(parameter == null || property == null) {
return;
}
parameter.isNullable = property.isNullable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,14 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation

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;
}

Expand Down Expand Up @@ -618,7 +618,7 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
* 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public String escapeReservedWord(String name) {
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;
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public void setParameterExampleValue(CodegenParameter p) {
}
} 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()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public CodegenModel fromModel(String name, Schema model) {
}

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 + "\"";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public CodegenModel fromModel(String name, Schema model) {
}

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 + "\"";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ private String paramToString(final String prefix, final CodegenParameter param,
}
String mapResult = "";
if (maybeMapResult != null) {
if (mapFn == "") {
if ("".equals(mapFn)) {
mapResult = maybeMapResult;
} else {
mapResult = maybeMapResult + (param.required ? " <|" : " <<");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,17 +858,17 @@ public CodegenModel fromModel(String name, Schema model) {
}
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);
}
}
Expand Down Expand Up @@ -1064,11 +1064,13 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
}
}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1012,10 +1012,10 @@ static long requiredBits(Long bound, boolean unsigned) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public ScalaAkkaClientCodegen() {

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");
Expand All @@ -114,7 +114,6 @@ public ScalaAkkaClientCodegen() {
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");
Expand Down