diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ConfigHelp.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ConfigHelp.java index 669882e1dc7b..1e54ce83b436 100644 --- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ConfigHelp.java +++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ConfigHelp.java @@ -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()); } diff --git a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/Generator.java b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/Generator.java index 106edeb432a9..557d74a8197c 100644 --- a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/Generator.java +++ b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/Generator.java @@ -192,7 +192,7 @@ private static File getTmpFolder() { return outputFolder; } catch (Exception e) { e.printStackTrace(); - return null; + throw new RuntimeException("Cannot access tmp folder"); } } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java index 1a0109b9f4d6..eccbcc827a6d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java @@ -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) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index e973ce6329e0..d17598228efe 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -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") 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 List fromServerVariables(Map> getAuthScopes(List securities, Map securitySchemes) { final Map> 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 @@ public Map postProcessSupportingFileData(Map 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 postProcessAuthMethod(List authMethods, Map> scopes) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java index 33362b8bdb13..4f4e60730d67 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractApexCodegen.java @@ -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; } @@ -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()"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java index d25f8aec6e3c..2bc1b3a84d38 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java @@ -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 + "\""; } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java index 9f6f94161892..031913fb0be8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java @@ -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 + "\""; } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java index a3ecd30d7c7a..32739004f526 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ElmClientCodegen.java @@ -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 ? " <|" : " <<"); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java index d5cc63ed28b8..2cb75b81a59c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java @@ -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); } } @@ -1064,11 +1064,13 @@ public Map postProcessModels(Map 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; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptFlowtypedClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptFlowtypedClientCodegen.java index 8128529f8d25..878fb31d47b6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptFlowtypedClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptFlowtypedClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java index 749091c02b13..003d08560372 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonAbstractConnexionServerCodegen.java @@ -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; } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java index bbad693cf20a..25a9eed5da1f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java @@ -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; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java index 27af253ed2a1..b8a14c5218c2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java @@ -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); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaAkkaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaAkkaClientCodegen.java index 7bffd96158cf..7db20b6c8578 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaAkkaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaAkkaClientCodegen.java @@ -102,7 +102,7 @@ public ScalaAkkaClientCodegen() { importMapping.put("DateTime", "org.joda.time.DateTime"); - typeMapping = new HashMap(); + typeMapping = new HashMap<>(); typeMapping.put("array", "Seq"); typeMapping.put("set", "Set"); typeMapping.put("boolean", "Boolean"); @@ -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");