diff --git a/README.md b/README.md index cbd3acbd611..ef0c25ec110 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/ZipUtil.java b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/ZipUtil.java index ec7f6f3d77e..2719cecd1dd 100644 --- a/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/ZipUtil.java +++ b/modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/service/ZipUtil.java @@ -51,18 +51,18 @@ public class ZipUtil { public void compressFiles(List 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()) { - addFolderToZip(file, file.getName(), zos); - } else { - addFileToZip(file, zos); + for (File file : listFiles) { + if (file.isDirectory()) { + addFolderToZip(file, file.getName(), zos); + } else { + addFileToZip(file, zos); + } } - } - zos.flush(); - zos.close(); + zos.flush(); + } } /** @@ -84,15 +84,12 @@ public class ZipUtil { zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName())); - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); - - long bytesRead = 0; - byte[] bytesIn = new byte[BUFFER_SIZE]; - int read = 0; - - while ((read = bis.read(bytesIn)) != -1) { - zos.write(bytesIn, 0, read); - bytesRead += read; + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read; + while ((read = bis.read(bytesIn)) != -1) { + zos.write(bytesIn, 0, read); + } } zos.closeEntry(); @@ -112,13 +109,12 @@ public class ZipUtil { IOException { zos.putNextEntry(new ZipEntry(file.getName())); - BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); - - byte[] bytesIn = new byte[BUFFER_SIZE]; - int read = 0; - - while ((read = bis.read(bytesIn)) != -1) { - zos.write(bytesIn, 0, read); + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) { + byte[] bytesIn = new byte[BUFFER_SIZE]; + int read; + while ((read = bis.read(bytesIn)) != -1) { + zos.write(bytesIn, 0, read); + } } zos.closeEntry(); 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 8a3c8b09e50..1a0109b9f4d 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 @@ -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")); - out.write(contents); - out.close(); + try (Writer out = new BufferedWriter(new OutputStreamWriter( + new FileOutputStream(output), "UTF-8"))) { + out.write(contents); + } return output; } 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 b4142f360fd..1528a064da2 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 @@ -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 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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/SpecValidationException.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/SpecValidationException.java index f09ba918cbc..2d8e65ba993 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/SpecValidationException.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/SpecValidationException.java @@ -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()) { + 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()); - warnings.forEach(msg -> - sb.append("\t-").append(msg).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(); } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java index 7a56847a07b..30c7f20ba07 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java @@ -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; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java index f135af4af94..54581cc2464 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaLagomServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaLagomServerCodegen.java index fc0c35d7b87..9b4651e601f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaLagomServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaLagomServerCodegen.java @@ -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(); + 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"); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java index b91b1d65e4f..fb8d581ce48 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAngularClientCodegen.java @@ -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.-]*$"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java index c65ef2fb9d4..0bb64748d78 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java index bdec9ffeffd..e26e7a25f16 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptFetchClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptInversifyClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptInversifyClientCodegen.java index c078a29382f..61bb8a52129 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptInversifyClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptInversifyClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptJqueryClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptJqueryClientCodegen.java index 7f6c5eb0632..217f6ded902 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptJqueryClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptJqueryClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java index 672a16b0b26..5735b09e47a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java index d16b26f7c48..69332ff6bbf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptRxjsClientCodegen.java @@ -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"; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 7df528a0436..f95125e9ce1 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -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"); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/SpecValidationExceptionTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/SpecValidationExceptionTest.java new file mode 100644 index 00000000000..5acf2d1a136 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/SpecValidationExceptionTest.java @@ -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); + } +} \ No newline at end of file