mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 14:10:56 +00:00
[Java] fix bigDecimalAsString config option (#14370)
This commit is contained in:
parent
a6eb96ea34
commit
38fdbe0c2c
@ -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)");
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user