forked from loafle/openapi-generator-original
fix resources management (#2229)
* fix resources management * remove obselete if statement * throw exception when body is null * prevent potentional nullpointerexception * use valueOf instead of constructor * remove duplicated code * avoid unclosed resources * remove redundant key * fix broken tests * fix sonar issues * fix tests * add Veamly as a company using openAPI generator * revert back if statement to explicitly express the intention behind it
This commit is contained in:
parent
0c54286909
commit
cc1fe6eebf
@ -524,6 +524,7 @@ Here are some companies/projects (alphabetical order) using OpenAPI Generator in
|
||||
- [Telstra](https://dev.telstra.com)
|
||||
- [TUI InfoTec GmbH](http://www.tui-infotec.com/)
|
||||
- [unblu inc.](https://www.unblu.com/)
|
||||
- [Veamly](https://www.veamly.com/)
|
||||
- [Xero](https://www.xero.com/)
|
||||
- [Zalando](https://www.zalando.com)
|
||||
|
||||
|
@ -51,7 +51,7 @@ public class ZipUtil {
|
||||
public void compressFiles(List<File> listFiles, String destZipFile)
|
||||
throws IOException {
|
||||
|
||||
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile));
|
||||
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile))) {
|
||||
|
||||
for (File file : listFiles) {
|
||||
if (file.isDirectory()) {
|
||||
@ -62,7 +62,7 @@ public class ZipUtil {
|
||||
}
|
||||
|
||||
zos.flush();
|
||||
zos.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,15 +84,12 @@ public class ZipUtil {
|
||||
|
||||
zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
|
||||
long bytesRead = 0;
|
||||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
|
||||
int read;
|
||||
while ((read = bis.read(bytesIn)) != -1) {
|
||||
zos.write(bytesIn, 0, read);
|
||||
bytesRead += read;
|
||||
}
|
||||
}
|
||||
|
||||
zos.closeEntry();
|
||||
@ -112,14 +109,13 @@ public class ZipUtil {
|
||||
IOException {
|
||||
zos.putNextEntry(new ZipEntry(file.getName()));
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
|
||||
|
||||
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
|
||||
byte[] bytesIn = new byte[BUFFER_SIZE];
|
||||
int read = 0;
|
||||
|
||||
int read;
|
||||
while ((read = bis.read(bytesIn)) != -1) {
|
||||
zos.write(bytesIn, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
@ -40,11 +40,11 @@ public abstract class AbstractGenerator {
|
||||
File parent = new File(output.getParent());
|
||||
parent.mkdirs();
|
||||
}
|
||||
Writer out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(output), "UTF-8"));
|
||||
|
||||
try (Writer out = new BufferedWriter(new OutputStreamWriter(
|
||||
new FileOutputStream(output), "UTF-8"))) {
|
||||
out.write(contents);
|
||||
out.close();
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
@ -1251,23 +1251,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return schema.getExample().toString();
|
||||
}
|
||||
|
||||
if (ModelUtils.isBooleanSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isDateSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isDateTimeSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isNumberSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isIntegerSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isStringSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isObjectSchema(schema)) {
|
||||
return "null";
|
||||
} else {
|
||||
return "null";
|
||||
}
|
||||
return getPropertyDefaultValue(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1282,6 +1266,16 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return schema.getDefault().toString();
|
||||
}
|
||||
|
||||
return getPropertyDefaultValue(schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return property value depending on property type
|
||||
* @param schema property type
|
||||
* @return property value
|
||||
*/
|
||||
private String getPropertyDefaultValue(Schema schema) {
|
||||
//NOSONAR
|
||||
if (ModelUtils.isBooleanSchema(schema)) {
|
||||
return "null";
|
||||
} else if (ModelUtils.isDateSchema(schema)) {
|
||||
@ -4506,6 +4500,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, String bodyParameterName) {
|
||||
if (body == null) {
|
||||
LOGGER.error("body in fromRequestBody cannot be null!");
|
||||
throw new RuntimeException("body in fromRequestBody cannot be null!");
|
||||
}
|
||||
CodegenParameter codegenParameter = CodegenModelFactory.newInstance(CodegenModelType.PARAMETER);
|
||||
codegenParameter.baseName = "UNKNOWN_BASE_NAME";
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.openapitools.codegen;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class SpecValidationException extends RuntimeException {
|
||||
@ -105,28 +106,28 @@ public class SpecValidationException extends RuntimeException {
|
||||
@Override
|
||||
public String getMessage() {
|
||||
int errorCount = 0;
|
||||
if (errors != null) {
|
||||
errorCount = errors.size();
|
||||
}
|
||||
if (errors != null) errorCount = errors.size();
|
||||
int warningCount = 0;
|
||||
if (warnings != null) {
|
||||
warningCount = warnings.size();
|
||||
}
|
||||
if (warnings != null) warningCount = warnings.size();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(System.lineSeparator())
|
||||
.append("Errors: ")
|
||||
.append(System.lineSeparator());
|
||||
errors.forEach(msg ->
|
||||
sb.append("\t-").append(msg).append(System.lineSeparator())
|
||||
);
|
||||
|
||||
if (!warnings.isEmpty()) {
|
||||
sb.append("Warnings: ").append(System.lineSeparator());
|
||||
warnings.forEach(msg ->
|
||||
sb.append("\t-").append(msg).append(System.lineSeparator())
|
||||
);
|
||||
Optional.ofNullable(errors).ifPresent(_errors -> {
|
||||
for (String msg : errors) {
|
||||
sb.append("\t-").append(msg).append(System.lineSeparator());
|
||||
}
|
||||
});
|
||||
|
||||
Optional.ofNullable(warnings).filter(warnings -> !warnings.isEmpty()).ifPresent(_errors -> {
|
||||
sb.append("Warnings: ").append(System.lineSeparator());
|
||||
for (String msg : errors) {
|
||||
sb.append("\t-").append(msg).append(System.lineSeparator());
|
||||
}
|
||||
});
|
||||
|
||||
return super.getMessage() + " | " +
|
||||
"Error count: " + errorCount + ", Warning count: " + warningCount + sb.toString();
|
||||
}
|
||||
|
@ -254,12 +254,12 @@ public class ExampleGenerator {
|
||||
} else if (ModelUtils.isDateTimeSchema(property)) {
|
||||
return "2000-01-23T04:56:07.000+00:00";
|
||||
} else if (ModelUtils.isNumberSchema(property)) {
|
||||
Double min = property.getMinimum() == null ? null : property.getMinimum().doubleValue();
|
||||
Double max = property.getMaximum() == null ? null : property.getMaximum().doubleValue();
|
||||
Double min = getPropertyValue(property.getMinimum());
|
||||
Double max = getPropertyValue(property.getMaximum());
|
||||
if (ModelUtils.isFloatSchema(property)) { // float
|
||||
return (float) randomNumber(min, max);
|
||||
} else if (ModelUtils.isDoubleSchema(property)) { // decimal/double
|
||||
return new BigDecimal(randomNumber(min, max));
|
||||
return BigDecimal.valueOf(randomNumber(min, max));
|
||||
} else { // no format defined
|
||||
return randomNumber(min, max);
|
||||
}
|
||||
@ -267,8 +267,8 @@ public class ExampleGenerator {
|
||||
return ""; // TODO
|
||||
|
||||
} else if (ModelUtils.isIntegerSchema(property)) {
|
||||
Double min = property.getMinimum() == null ? null : property.getMinimum().doubleValue();
|
||||
Double max = property.getMaximum() == null ? null : property.getMaximum().doubleValue();
|
||||
Double min = getPropertyValue(property.getMinimum());
|
||||
Double max = getPropertyValue(property.getMaximum());
|
||||
if (ModelUtils.isLongSchema(property)) {
|
||||
return (long) randomNumber(min, max);
|
||||
}
|
||||
@ -319,6 +319,10 @@ public class ExampleGenerator {
|
||||
return "";
|
||||
}
|
||||
|
||||
private Double getPropertyValue(BigDecimal propertyValue) {
|
||||
return propertyValue == null ? null : propertyValue.doubleValue();
|
||||
}
|
||||
|
||||
private double randomNumber(Double min, Double max) {
|
||||
if (min != null && max != null) {
|
||||
double range = max - min;
|
||||
|
@ -28,6 +28,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@ -38,6 +39,8 @@ import static org.openapitools.codegen.utils.StringUtils.underscore;
|
||||
public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTypeScriptClientCodegen.class);
|
||||
|
||||
protected final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
|
||||
private static final String UNDEFINED_VALUE = "undefined";
|
||||
|
||||
|
@ -78,7 +78,7 @@ public class ScalaLagomServerCodegen extends AbstractScalaCodegen implements Cod
|
||||
importMapping.put("DateTime", "org.joda.time.DateTime");
|
||||
importMapping.put("ListBuffer", "scala.collection.mutable.ListBuffer");
|
||||
|
||||
typeMapping = new HashMap<String, String>();
|
||||
typeMapping = new HashMap<>();
|
||||
typeMapping.put("Integer", "Int");
|
||||
typeMapping.put("enum", "NSString");
|
||||
typeMapping.put("array", "Seq");
|
||||
@ -91,7 +91,6 @@ public class ScalaLagomServerCodegen extends AbstractScalaCodegen implements Cod
|
||||
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");
|
||||
|
@ -34,8 +34,6 @@ import static org.openapitools.codegen.utils.StringUtils.*;
|
||||
public class TypeScriptAngularClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptAngularClientCodegen.class);
|
||||
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
|
||||
private static String CLASS_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9]*$";
|
||||
private static String FILE_NAME_SUFFIX_PATTERN = "^[a-zA-Z0-9.-]*$";
|
||||
|
||||
|
@ -30,7 +30,6 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class TypeScriptAxiosClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -31,7 +31,6 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -31,8 +31,6 @@ import java.util.*;
|
||||
import static org.openapitools.codegen.utils.StringUtils.camelize;
|
||||
|
||||
public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
private static final String X_DISCRIMINATOR_TYPE = "x-discriminator-value";
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -33,7 +33,6 @@ import java.util.Locale;
|
||||
|
||||
public class TypeScriptJqueryClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptJqueryClientCodegen.class);
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -32,7 +32,6 @@ import static org.openapitools.codegen.utils.StringUtils.camelize;
|
||||
|
||||
public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptNodeClientCodegen.class);
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -32,7 +32,6 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
public class TypeScriptRxjsClientCodegen extends AbstractTypeScriptClientCodegen {
|
||||
private static final SimpleDateFormat SNAPSHOT_SUFFIX_FORMAT = new SimpleDateFormat("yyyyMMddHHmm", Locale.ROOT);
|
||||
|
||||
public static final String NPM_NAME = "npmName";
|
||||
public static final String NPM_VERSION = "npmVersion";
|
||||
|
@ -38,7 +38,7 @@ import java.util.stream.Collectors;
|
||||
public class DefaultCodegenTest {
|
||||
|
||||
@Test
|
||||
public void testHasBodyParameter() throws Exception {
|
||||
public void testHasBodyParameter() {
|
||||
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
|
||||
Operation pingOperation = new Operation()
|
||||
.responses(
|
||||
@ -61,6 +61,13 @@ public class DefaultCodegenTest {
|
||||
Assert.assertEquals(codegen.hasBodyParameter(openAPI, createOperation), true);
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = RuntimeException.class)
|
||||
public void testParameterEmptyDescription() {
|
||||
DefaultCodegen codegen = new DefaultCodegen();
|
||||
|
||||
codegen.fromRequestBody(null, new HashSet<>(), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetConsumesInfoAndGetProducesInfo() throws Exception {
|
||||
final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
|
||||
|
@ -0,0 +1,18 @@
|
||||
package org.openapitools.codegen;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class SpecValidationExceptionTest {
|
||||
|
||||
@Test
|
||||
public void shouldGetDefaultMessage() {
|
||||
SpecValidationException specValidationException = new SpecValidationException();
|
||||
|
||||
String expectedResult = new StringBuffer("null | Error count: 0, Warning count: 0")
|
||||
.append(System.lineSeparator()).append("Errors: ")
|
||||
.append(System.lineSeparator()).toString();
|
||||
|
||||
Assert.assertEquals(specValidationException.getMessage(), expectedResult);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user