forked from loafle/openapi-generator-original
[ci] Sonar bug fixes (#7497)
* Fixing java:S3599 - Avoid double brace initializer Because Double Brace Initialization (DBI) creates an anonymous class with a reference to the instance of the owning object, its use can lead to memory leaks if the anonymous inner class is returned and held by other objects. Even when there's no leak, DBI is so obscure that it's bound to confuse most maintainers. * Fix incorrect List.contains typed check * Avoid potential NPE in DefaultCodegen * [fsharp] Remove unused boolean and log * Fix potential NPE in Haskell http client * Fix potential bugs in JavaCXFExtServerCodegen, found by static analysis
This commit is contained in:
parent
94ed8187f9
commit
874c2a19d8
@ -394,19 +394,17 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
if (this.useOneOfInterfaces) {
|
||||
// First, add newly created oneOf interfaces
|
||||
for (CodegenModel cm : addOneOfInterfaces) {
|
||||
Map<String, Object> modelValue = new HashMap<String, Object>() {{
|
||||
putAll(additionalProperties());
|
||||
put("model", cm);
|
||||
}};
|
||||
Map<String, Object> modelValue = new HashMap<>(additionalProperties());
|
||||
modelValue.put("model", cm);
|
||||
|
||||
List<Object> modelsValue = Arrays.asList(modelValue);
|
||||
List<Map<String, String>> importsValue = new ArrayList<Map<String, String>>();
|
||||
Map<String, Object> objsValue = new HashMap<String, Object>() {{
|
||||
put("models", modelsValue);
|
||||
put("package", modelPackage());
|
||||
put("imports", importsValue);
|
||||
put("classname", cm.classname);
|
||||
putAll(additionalProperties);
|
||||
}};
|
||||
Map<String, Object> objsValue = new HashMap<>();
|
||||
objsValue.put("models", modelsValue);
|
||||
objsValue.put("package", modelPackage());
|
||||
objsValue.put("imports", importsValue);
|
||||
objsValue.put("classname", cm.classname);
|
||||
objsValue.putAll(additionalProperties);
|
||||
objs.put(cm.name, objsValue);
|
||||
}
|
||||
|
||||
@ -2704,7 +2702,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
if (foundDisc != null) {
|
||||
return foundDisc;
|
||||
}
|
||||
if (!!this.getLegacyDiscriminatorBehavior()) {
|
||||
|
||||
if (this.getLegacyDiscriminatorBehavior()) {
|
||||
return null;
|
||||
}
|
||||
Discriminator disc = new Discriminator();
|
||||
@ -2742,7 +2741,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " +
|
||||
"oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
|
||||
}
|
||||
if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
|
||||
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) {
|
||||
disc.setPropertyName(foundDisc.getPropertyName());
|
||||
disc.setMapping(foundDisc.getMapping());
|
||||
return disc;
|
||||
@ -2771,7 +2770,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
throw new RuntimeException("The anyOf schemas have conflicting discriminator property names. " +
|
||||
"anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames));
|
||||
}
|
||||
if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) {
|
||||
if (foundDisc != null && (hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) {
|
||||
disc.setPropertyName(foundDisc.getPropertyName());
|
||||
disc.setMapping(foundDisc.getMapping());
|
||||
return disc;
|
||||
@ -2860,8 +2859,12 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
List<MappedModel> descendentSchemas = new ArrayList();
|
||||
Map<String, Schema> schemas = ModelUtils.getSchemas(openAPI);
|
||||
String currentSchemaName = thisSchemaName;
|
||||
while (true) {
|
||||
for (String childName : schemas.keySet()) {
|
||||
Set<String> keys = schemas.keySet();
|
||||
|
||||
int count = 0;
|
||||
// hack: avoid infinite loop on potential self-references in event our checks fail.
|
||||
while (100000 > count++) {
|
||||
for (String childName : keys) {
|
||||
if (childName.equals(thisSchemaName)) {
|
||||
continue;
|
||||
}
|
||||
@ -2879,8 +2882,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
continue;
|
||||
}
|
||||
String parentName = ModelUtils.getSimpleRef(ref);
|
||||
if (parentName.equals(currentSchemaName)) {
|
||||
if (queue.contains(childName) || descendentSchemas.contains(childName)) {
|
||||
if (parentName != null && parentName.equals(currentSchemaName)) {
|
||||
if (queue.contains(childName) || descendentSchemas.stream().anyMatch(i -> childName.equals(i.getMappingName()))) {
|
||||
throw new RuntimeException("Stack overflow hit when looking for " + thisSchemaName + " an infinite loop starting and ending at " + childName + " was seen");
|
||||
}
|
||||
queue.add(childName);
|
||||
@ -3600,7 +3603,10 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);
|
||||
|
||||
// lookup discriminator
|
||||
Schema schema = schemas.get(op.returnBaseType);
|
||||
Schema schema = null;
|
||||
if (schemas != null) {
|
||||
schema = schemas.get(op.returnBaseType);
|
||||
}
|
||||
if (schema != null) {
|
||||
CodegenModel cmod = fromModel(op.returnBaseType, schema);
|
||||
op.discriminator = cmod.discriminator;
|
||||
|
@ -176,7 +176,6 @@ public class FsharpGiraffeServerCodegen extends AbstractFSharpCodegen {
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
boolean isLibrary = false;
|
||||
|
||||
if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) {
|
||||
setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID));
|
||||
@ -191,10 +190,6 @@ public class FsharpGiraffeServerCodegen extends AbstractFSharpCodegen {
|
||||
|
||||
additionalProperties.put(PROJECT_SDK, projectSdk);
|
||||
|
||||
// TODO - should we be supporting a Giraffe class library?
|
||||
if (isLibrary)
|
||||
LOGGER.warn("Library flag not currently supported.");
|
||||
|
||||
String authFolder = sourceFolder + File.separator + "auth";
|
||||
String implFolder = sourceFolder + File.separator + "impl";
|
||||
String helperFolder = sourceFolder + File.separator + "helpers";
|
||||
|
@ -1274,9 +1274,11 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC
|
||||
if (dataType == null && cm.isArrayModel) { // isAlias + arrayModelType missing "datatype"
|
||||
dataType = "[" + cm.arrayModelType + "]";
|
||||
}
|
||||
cm.vendorExtensions.put(VENDOR_EXTENSION_X_DATA_TYPE, dataType);
|
||||
if (dataType.equals("Maybe A.Value")) {
|
||||
cm.vendorExtensions.put(VENDOR_EXTENSION_X_IS_MAYBE_VALUE, true);
|
||||
if (dataType != null) {
|
||||
cm.vendorExtensions.put(VENDOR_EXTENSION_X_DATA_TYPE, dataType);
|
||||
if (dataType.equals("Maybe A.Value")) {
|
||||
cm.vendorExtensions.put(VENDOR_EXTENSION_X_IS_MAYBE_VALUE, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (CodegenProperty var : cm.vars) {
|
||||
|
@ -282,7 +282,7 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
return f;
|
||||
});
|
||||
|
||||
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
|
||||
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
|
||||
|
||||
private static final long MIN_DATE;
|
||||
|
||||
@ -551,10 +551,13 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
short inclusiveMax = (short) (var == null || !var.exclusiveMaximum ? 1 : 0);
|
||||
byte randomByte = (byte) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomByte);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomByte);
|
||||
}
|
||||
} else {
|
||||
buffer.append(String.format(Locale.getDefault(), "(byte)%0#2x", randomByte));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -568,10 +571,13 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
char inclusiveMax = (char) (var == null || !var.exclusiveMaximum ? 1 : 0);
|
||||
char randomChar = (char) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomChar);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomChar);
|
||||
}
|
||||
} else {
|
||||
buffer.append(String.format(Locale.getDefault(), "'%c'", randomChar));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -607,10 +613,10 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
BigDecimal exclusiveMinLong = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
|
||||
BigDecimal inclusiveMaxLong = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
|
||||
long randomDateLong = minLong.add(exclusiveMinLong).add(maxLong.add(inclusiveMaxLong).subtract(minLong)
|
||||
.subtract(exclusiveMinLong).multiply(new BigDecimal(Math.random()))).longValue();
|
||||
.subtract(exclusiveMinLong).multiply(BigDecimal.valueOf(Math.random()))).longValue();
|
||||
|
||||
// If it's just a date without a time, round downwards to the nearest day.
|
||||
if ("date".equals(var.dataFormat))
|
||||
if (var != null && "date".equals(var.dataFormat))
|
||||
randomDateLong = (randomDateLong % MILLIS_PER_DAY) * MILLIS_PER_DAY;
|
||||
|
||||
// NOTE: By default Jackson serializes Date as long milliseconds since epoch date, but that conflicts with
|
||||
@ -635,19 +641,20 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
private void appendRandomDouble(StringBuilder buffer, CodegenOperation op, CodegenVariable var) {
|
||||
if (!appendRandomEnum(buffer, op, var)) {
|
||||
// NOTE: use BigDecimal to hold double values, to avoid numeric overflow.
|
||||
BigDecimal min = new BigDecimal(
|
||||
var == null || var.minimum == null ? Long.MIN_VALUE : Double.parseDouble(var.minimum));
|
||||
BigDecimal max = new BigDecimal(
|
||||
var == null || var.maximum == null ? Long.MAX_VALUE : Double.parseDouble(var.maximum));
|
||||
BigDecimal min = BigDecimal.valueOf(var == null || var.minimum == null ? Long.MIN_VALUE : Double.parseDouble(var.minimum));
|
||||
BigDecimal max = BigDecimal.valueOf(var == null || var.maximum == null ? Long.MAX_VALUE : Double.parseDouble(var.maximum));
|
||||
BigDecimal exclusiveMin = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
|
||||
BigDecimal inclusiveMax = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
|
||||
BigDecimal randomBigDecimal = min.add(exclusiveMin).add(max.add(inclusiveMax).subtract(min)
|
||||
.subtract(exclusiveMin).multiply(new BigDecimal(String.valueOf(Math.random()))));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomBigDecimal);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomBigDecimal);
|
||||
}
|
||||
} else {
|
||||
buffer.append(randomBigDecimal.toString()).append('D');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -704,10 +711,13 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
float randomFloat = (float) (min + exclusiveMin
|
||||
+ ((max + inclusiveMax - min - exclusiveMin) * Math.random()));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomFloat);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomFloat);
|
||||
}
|
||||
} else {
|
||||
buffer.append(String.format(Locale.getDefault(), "%g", randomFloat)).append('F');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -720,10 +730,13 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
long inclusiveMax = var == null || !var.exclusiveMaximum ? 1 : 0;
|
||||
int randomInt = (int) (min + exclusiveMin + ((max + inclusiveMax - min - exclusiveMin) * Math.random()));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomInt);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomInt);
|
||||
}
|
||||
} else {
|
||||
buffer.append(randomInt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -737,13 +750,16 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
BigDecimal exclusiveMin = new BigDecimal(var != null && var.exclusiveMinimum ? 1 : 0);
|
||||
BigDecimal inclusiveMax = new BigDecimal(var == null || !var.exclusiveMaximum ? 1 : 0);
|
||||
long randomLong = min.add(exclusiveMin).add(
|
||||
max.add(inclusiveMax).subtract(min).subtract(exclusiveMin).multiply(new BigDecimal(Math.random())))
|
||||
max.add(inclusiveMax).subtract(min).subtract(exclusiveMin).multiply(BigDecimal.valueOf(Math.random())))
|
||||
.longValue();
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomLong);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomLong);
|
||||
}
|
||||
} else {
|
||||
buffer.append(randomLong).append('L');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -757,10 +773,13 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
short randomShort = (short) (min + exclusiveMin
|
||||
+ ((max + inclusiveMax - min - exclusiveMin) * Math.random()));
|
||||
|
||||
if (loadTestDataFromFile)
|
||||
var.addTestData(randomShort);
|
||||
else
|
||||
if (loadTestDataFromFile) {
|
||||
if (var != null) {
|
||||
var.addTestData(randomShort);
|
||||
}
|
||||
} else {
|
||||
buffer.append(String.format(Locale.getDefault(), "(short)%d", randomShort));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -769,7 +788,9 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
String randomString = generateRandomString(var);
|
||||
|
||||
if (loadTestDataFromFile) {
|
||||
var.addTestData(randomString);
|
||||
if (var != null) {
|
||||
var.addTestData(randomString);
|
||||
}
|
||||
} else {
|
||||
buffer.append('"').append(randomString).append('"');
|
||||
}
|
||||
@ -1375,13 +1396,15 @@ public class JavaCXFExtServerCodegen extends JavaCXFServerCodegen implements CXF
|
||||
break;
|
||||
}
|
||||
}
|
||||
supportingFiles.remove(supportingFile);
|
||||
SupportingFile updated = new SupportingFile(
|
||||
supportingFile.getTemplateFile(),
|
||||
supportingFile.getFolder(),
|
||||
"ApplicationContext-" + invokerPackage + ".xml"
|
||||
);
|
||||
supportingFiles.add(updated);
|
||||
if (supportingFile != null) {
|
||||
supportingFiles.remove(supportingFile);
|
||||
SupportingFile updated = new SupportingFile(
|
||||
supportingFile.getTemplateFile(),
|
||||
supportingFile.getFolder(),
|
||||
"ApplicationContext-" + invokerPackage + ".xml"
|
||||
);
|
||||
supportingFiles.add(updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -818,11 +818,11 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
}
|
||||
|
||||
if (addImports) {
|
||||
Map<String, String> imports2Classnames = new HashMap<String, String>() {{
|
||||
put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
|
||||
put("NoSuchElementException", "java.util.NoSuchElementException");
|
||||
put("JsonIgnore", "com.fasterxml.jackson.annotation.JsonIgnore");
|
||||
}};
|
||||
Map<String, String> imports2Classnames = new HashMap<>();
|
||||
imports2Classnames.put("JsonNullable", "org.openapitools.jackson.nullable.JsonNullable");
|
||||
imports2Classnames.put("NoSuchElementException", "java.util.NoSuchElementException");
|
||||
imports2Classnames.put("JsonIgnore", "com.fasterxml.jackson.annotation.JsonIgnore");
|
||||
|
||||
for (Map.Entry<String, String> entry : imports2Classnames.entrySet()) {
|
||||
cm.imports.add(entry.getKey());
|
||||
Map<String, String> importsItem = new HashMap<String, String>();
|
||||
@ -976,9 +976,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
@Override
|
||||
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
|
||||
for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {
|
||||
Map<String, String> oneImport = new HashMap<String, String>() {{
|
||||
put("import", importMapping.get(i));
|
||||
}};
|
||||
Map<String, String> oneImport = new HashMap<>();
|
||||
oneImport.put("import", importMapping.get(i));
|
||||
|
||||
if (!imports.contains(oneImport)) {
|
||||
imports.add(oneImport);
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.openapitools.codegen.languages;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import io.swagger.v3.oas.models.Operation;
|
||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
@ -269,25 +271,23 @@ public class ScalaAkkaHttpServerCodegen extends AbstractScalaCodegen implements
|
||||
return baseObjs;
|
||||
}
|
||||
|
||||
private static Set<String> primitiveParamTypes = new HashSet<String>() {{
|
||||
addAll(Arrays.asList(
|
||||
"Int",
|
||||
"Long",
|
||||
"Float",
|
||||
"Double",
|
||||
"Boolean",
|
||||
"String"
|
||||
));
|
||||
}};
|
||||
private static final Set<String> primitiveParamTypes = ImmutableSet.of(
|
||||
"Int",
|
||||
"Long",
|
||||
"Float",
|
||||
"Double",
|
||||
"Boolean",
|
||||
"String"
|
||||
);
|
||||
|
||||
private static Map<String, String> pathTypeToMatcher = new HashMap<String, String>() {{
|
||||
put("Int", "IntNumber");
|
||||
put("Long", "LongNumber");
|
||||
put("Float", "FloatNumber");
|
||||
put("Double", "DoubleNumber");
|
||||
put("Boolean", "Boolean");
|
||||
put("String", "Segment");
|
||||
}};
|
||||
private static final Map<String, String> pathTypeToMatcher = ImmutableMap.<String,String>builder()
|
||||
.put("Int", "IntNumber")
|
||||
.put("Long", "LongNumber")
|
||||
.put("Float", "FloatNumber")
|
||||
.put("Double", "DoubleNumber")
|
||||
.put("Boolean", "Boolean")
|
||||
.put("String", "Segment")
|
||||
.build();
|
||||
|
||||
protected static void addPathMatcher(CodegenOperation codegenOperation) {
|
||||
LinkedList<String> allPaths = new LinkedList<>(Arrays.asList(codegenOperation.path.split("/")));
|
||||
|
Loading…
x
Reference in New Issue
Block a user