[Java] fix bigDecimalAsString config option (#14370)

This commit is contained in:
Oleh Kurpiak 2023-01-03 17:49:08 +02:00 committed by GitHub
parent a6eb96ea34
commit 38fdbe0c2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 9 deletions

View File

@ -1375,7 +1375,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
}
if (serializeBigDecimalAsString) {
if ("decimal".equals(property.baseType)) {
if ("decimal".equals(property.baseType) || "bigdecimal".equalsIgnoreCase(property.baseType)) {
// we serialize BigDecimal as `string` to avoid precision loss
property.vendorExtensions.put("x-extra-annotation", "@JsonSerialize(using = ToStringSerializer.class)");

View File

@ -114,6 +114,12 @@ public class JavaFileAssert extends AbstractAssert<JavaFileAssert, CompilationUn
return this;
}
public JavaFileAssert hasNoImports(final String... imports) {
Assertions.assertThat(actual.getImports().stream().map(NodeWithName::getNameAsString))
.doesNotContainAnyElementsOf(Arrays.asList(imports));
return this;
}
public JavaFileAssert printFileContent() {
System.out.println(actual);
return this;

View File

@ -47,7 +47,6 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.assertj.core.api.Condition;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.ClientOptInput;
@ -1568,9 +1567,26 @@ public class SpringCodegenTest {
.printFileContent();
javaFileAssert.assertMethod("getName")
.assertMethodAnnotations().anyMatch(annotation -> !annotation.getNameAsString().equals("NotNull"));
javaFileAssert.isNot(new Condition<>(classfile ->
classfile.getImports().stream().map(NodeWithName::getNameAsString)
.anyMatch("javax.validation.constraints.NotNull"::equals), ""));
javaFileAssert.hasNoImports("javax.validation.constraints.NotNull");
}
@Test
public void requiredFieldShouldIncludeNotNullAnnotationWithBeanValidationTrue_issue14252() throws IOException {
SpringCodegen codegen = new SpringCodegen();
codegen.setLibrary(SPRING_BOOT);
codegen.additionalProperties().put(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, "true");
Map<String, File> files = generateFiles(codegen, "src/test/resources/bugs/issue_14252.yaml");
JavaFileAssert.assertThat(files.get("MyResponse.java"))
.printFileContent()
.hasImports("com.fasterxml.jackson.databind.annotation.JsonSerialize", "com.fasterxml.jackson.databind.ser.std.ToStringSerializer")
.assertMethod("getMyPropTypeNumber")
.assertMethodAnnotations()
.containsWithNameAndAttributes("JsonSerialize", ImmutableMap.of(
"using", "ToStringSerializer.class"
));
}
@Test
@ -1595,10 +1611,9 @@ public class SpringCodegenTest {
.printFileContent();
javaFileAssert.assertMethod("getName").assertMethodAnnotations()
.containsWithName("NotNull").containsWithName("Size").containsWithName("Email");
javaFileAssert.isNot(new Condition<>(classfile ->
classfile.getImports().stream().map(NodeWithName::getNameAsString)
.anyMatch("javax.validation.constraints.NotNull"::equals), ""));
javaFileAssert.hasImports("javax.validation.constraints");
javaFileAssert
.hasNoImports("javax.validation.constraints.NotNull")
.hasImports("javax.validation.constraints");
}
public void shouldUseEqualsNullableForArrayWhenSetInConfig_issue13385() throws IOException {

View File

@ -0,0 +1,28 @@
openapi: 3.0.2
servers:
- url: https://test.org
info:
version: 1.0.0
title: test bigdecimal as String
description: test bigdecimal as String
paths:
/test:
get:
operationId: get
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/MyResponse"
components:
schemas:
MyResponse:
properties:
myPropTypeNumber:
type: number