Skip to content

Commit a106437

Browse files
Philzenwelshm
authored andcommitted
[JAVA] Fix jackson (de)serializer annotation breaking libs using gson when bigDecimalAsString=true (OpenAPITools#18835)
* Implement regression test for bigDecimalAsString annotations * Ensure @JsonSerialize and @JsonDeserialize is only generated when Jackson enabled Fixes OpenAPITools#6496
1 parent 3c98216 commit a106437

3 files changed

Lines changed: 147 additions & 2 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ private void addAdditionalImports(CodegenModel model, CodegenComposedSchemas com
17791779

17801780
@Override
17811781
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
1782-
if (serializeBigDecimalAsString) {
1782+
if (serializeBigDecimalAsString && additionalProperties.containsKey(JACKSON)) {
17831783
if ("decimal".equals(property.baseType) || "bigdecimal".equalsIgnoreCase(property.baseType)) {
17841784
// we serialize BigDecimal as `string` to avoid precision loss
17851785
property.vendorExtensions.put("x-extra-annotation", "@JsonSerialize(using = ToStringSerializer.class)");
@@ -1804,7 +1804,7 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
18041804
model.imports.add("Arrays");
18051805
} else if ("set".equals(property.containerType)) {
18061806
model.imports.add("LinkedHashSet");
1807-
if (!openApiNullable || !property.isNullable) { // cannot be wrapped to nullable
1807+
if ((!openApiNullable || !property.isNullable) && additionalProperties.containsKey(JACKSON)) { // cannot be wrapped to nullable
18081808
model.imports.add("JsonDeserialize");
18091809
property.vendorExtensions.put("x-setter-extra-annotation", "@JsonDeserialize(as = LinkedHashSet.class)");
18101810
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import static org.assertj.core.api.Assertions.assertThat;
6060
import static org.assertj.core.api.Assertions.entry;
61+
import static org.assertj.core.api.InstanceOfAssertFactories.FILE;
6162
import static org.openapitools.codegen.CodegenConstants.SERIALIZATION_LIBRARY;
6263
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
6364
import static org.openapitools.codegen.languages.JavaClientCodegen.*;
@@ -2610,6 +2611,62 @@ private void testHandleURIEnum(String library, String[] expectedInnerEnumLines,
26102611
);
26112612
}
26122613

2614+
/**
2615+
* Regression test for <a href="https://github.com/OpenAPITools/openapi-generator/issues/6496">#6496</a>
2616+
*/
2617+
@Test void doesNotGenerateJacksonToStringSerializerAnnotation_whenLibraryIsGson_andSerializeBigDecimalAsStringIsTrue() {
2618+
final CodegenConfigurator configurator = new CodegenConfigurator()
2619+
.setGeneratorName("java")
2620+
.setLibrary(JavaClientCodegen.OKHTTP_GSON)
2621+
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_GSON)
2622+
.addAdditionalProperty(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, true)
2623+
.addGlobalProperty(CodegenConstants.MODELS, "FormatTest")
2624+
.addGlobalProperty(CodegenConstants.MODEL_DOCS, "false")
2625+
.addGlobalProperty(CodegenConstants.MODEL_TESTS, "false")
2626+
.setInputSpec("src/test/resources/2_0/java/issue-6496.yaml")
2627+
.setOutputDir(newTempFolder().toString().replace("\\", "/"));
2628+
2629+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
2630+
2631+
assertThat(files).hasSize(1).first(FILE).content()
2632+
.doesNotContain(
2633+
"@JsonDeserialize(as = LinkedHashSet.class)",
2634+
"@JsonSerialize(using = ToStringSerializer.class)",
2635+
"com.fasterxml.jackson.databind.ser.std.ToStringSerializer",
2636+
"com.fasterxml.jackson.databind.annotation.JsonDeserialize",
2637+
"com.fasterxml.jackson.databind.annotation.JsonSerialize"
2638+
);
2639+
}
2640+
2641+
/**
2642+
* Test that fix for <a href="https://github.com/OpenAPITools/openapi-generator/issues/6496">#6496</a> has
2643+
* no unwanted side effects on the existing feature (Jackson + bigDecimalAsString)
2644+
*/
2645+
@Test void generatesJacksonToStringSerializerAnnotation_whenLibraryIsJackson_andSerializeBigDecimalAsStringIsTrue() {
2646+
final CodegenConfigurator configurator = new CodegenConfigurator()
2647+
.setGeneratorName("java")
2648+
.setLibrary(JavaClientCodegen.NATIVE)
2649+
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_JACKSON)
2650+
.addAdditionalProperty(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, true)
2651+
.addAdditionalProperty(OPENAPI_NULLABLE, false)
2652+
.addGlobalProperty(CodegenConstants.MODELS, "FormatTest")
2653+
.addGlobalProperty(CodegenConstants.MODEL_DOCS, "false")
2654+
.addGlobalProperty(CodegenConstants.MODEL_TESTS, "false")
2655+
.setInputSpec("src/test/resources/2_0/java/issue-6496.yaml")
2656+
.setOutputDir(newTempFolder().toString().replace("\\", "/"));
2657+
2658+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
2659+
2660+
assertThat(files).hasSize(1).first(FILE).content()
2661+
.contains(
2662+
"@JsonDeserialize(as = LinkedHashSet.class)",
2663+
"@JsonSerialize(using = ToStringSerializer.class)",
2664+
"com.fasterxml.jackson.databind.ser.std.ToStringSerializer",
2665+
"com.fasterxml.jackson.databind.annotation.JsonDeserialize",
2666+
"com.fasterxml.jackson.databind.annotation.JsonSerialize"
2667+
);
2668+
}
2669+
26132670
static private Path newTempFolder() {
26142671
try {
26152672
var tempDir = Files.createTempDirectory("test");
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
swagger: '2.0'
2+
info:
3+
description: "This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\"
4+
version: 1.0.0
5+
title: OpenAPI Petstore
6+
license:
7+
name: Apache-2.0
8+
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
9+
basePath: /v2
10+
schemes:
11+
- http
12+
paths:
13+
/:
14+
get:
15+
operationId: addPet
16+
responses:
17+
'200':
18+
description: successful operation
19+
definitions:
20+
FormatTest:
21+
type: object
22+
required:
23+
- number
24+
- byte
25+
- date
26+
- password
27+
properties:
28+
integer:
29+
type: integer
30+
maximum: 100
31+
minimum: 10
32+
int32:
33+
type: integer
34+
format: int32
35+
maximum: 200
36+
minimum: 20
37+
int64:
38+
type: integer
39+
format: int64
40+
number:
41+
maximum: 543.2
42+
minimum: 32.1
43+
type: number
44+
float:
45+
type: number
46+
format: float
47+
maximum: 987.6
48+
minimum: 54.3
49+
double:
50+
type: number
51+
format: double
52+
maximum: 123.4
53+
minimum: 67.8
54+
string:
55+
type: string
56+
pattern: /[a-z]/i
57+
byte:
58+
type: string
59+
format: byte
60+
binary:
61+
type: string
62+
format: binary
63+
date:
64+
type: string
65+
format: date
66+
dateTime:
67+
type: string
68+
format: date-time
69+
uuid:
70+
type: string
71+
format: uuid
72+
example: 72f98069-206d-4f12-9f12-3d1e525a8e84
73+
password:
74+
type: string
75+
format: password
76+
maxLength: 64
77+
minLength: 10
78+
BigDecimal:
79+
type: string
80+
format: number
81+
enumContainer:
82+
type: array
83+
uniqueItems: true
84+
items:
85+
enum:
86+
- placed
87+
- approved
88+
- delivered

0 commit comments

Comments
 (0)