Skip to content

Commit b725960

Browse files
committed
Implement regression test for bigDecimalAsString annotations
1 parent c08afa3 commit b725960

2 files changed

Lines changed: 155 additions & 1 deletion

File tree

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
package org.openapitools.codegen.java;
1919

2020
import org.junit.jupiter.api.Assertions;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.InstanceOfAssertFactories.FILE;
24+
import static org.assertj.core.api.InstanceOfAssertFactories.type;
2125
import static org.openapitools.codegen.TestUtils.assertFileContains;
2226
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
2327
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
24-
import static org.openapitools.codegen.languages.JavaClientCodegen.USE_ENUM_CASE_INSENSITIVE;
28+
import static org.openapitools.codegen.languages.JavaClientCodegen.*;
2529
import static org.testng.Assert.assertEquals;
2630
import static org.testng.Assert.assertTrue;
2731
import static org.testng.Assert.fail;
@@ -3472,4 +3476,66 @@ void testBuilderJavaClient() throws IOException {
34723476
".name(getName())",
34733477
"hasLegs(getHasLegs())");
34743478
}
3479+
3480+
/**
3481+
* Regression test for <a href="https://github.com/OpenAPITools/openapi-generator/issues/6496">#6496</a>
3482+
*/
3483+
@Test void doesNotGenerateJacksonToStringSerializerAnnotation_whenLibraryIsGson_andSerializeBigDecimalAsStringIsTrue() throws IOException {
3484+
File output = Files.createTempDirectory("test").toFile();
3485+
output.deleteOnExit();
3486+
3487+
final CodegenConfigurator configurator = new CodegenConfigurator()
3488+
.setGeneratorName("java")
3489+
.setLibrary(JavaClientCodegen.OKHTTP_GSON)
3490+
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_GSON)
3491+
.addAdditionalProperty(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, true)
3492+
.addGlobalProperty(CodegenConstants.MODELS, "FormatTest")
3493+
.addGlobalProperty(CodegenConstants.MODEL_DOCS, "false")
3494+
.addGlobalProperty(CodegenConstants.MODEL_TESTS, "false")
3495+
.setInputSpec("src/test/resources/2_0/java/issue-6496.yaml")
3496+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3497+
3498+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3499+
3500+
assertThat(files).hasSize(1).first(FILE).content()
3501+
.doesNotContain(
3502+
"@JsonDeserialize(as = LinkedHashSet.class)",
3503+
"@JsonSerialize(using = ToStringSerializer.class)",
3504+
"com.fasterxml.jackson.databind.ser.std.ToStringSerializer",
3505+
"com.fasterxml.jackson.databind.annotation.JsonDeserialize",
3506+
"com.fasterxml.jackson.databind.annotation.JsonSerialize"
3507+
);
3508+
}
3509+
3510+
/**
3511+
* Test that fix for <a href="https://github.com/OpenAPITools/openapi-generator/issues/6496">#6496</a> has
3512+
* no unwanted side effects on the existing feature (Jackson + bigDecimalAsString)
3513+
*/
3514+
@Test void generatesJacksonToStringSerializerAnnotation_whenLibraryIsJackson_andSerializeBigDecimalAsStringIsTrue() throws IOException {
3515+
File output = Files.createTempDirectory("test").toFile();
3516+
output.deleteOnExit();
3517+
3518+
final CodegenConfigurator configurator = new CodegenConfigurator()
3519+
.setGeneratorName("java")
3520+
.setLibrary(JavaClientCodegen.NATIVE)
3521+
.addAdditionalProperty(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_JACKSON)
3522+
.addAdditionalProperty(CodegenConstants.SERIALIZE_BIG_DECIMAL_AS_STRING, true)
3523+
.addAdditionalProperty(OPENAPI_NULLABLE, false)
3524+
.addGlobalProperty(CodegenConstants.MODELS, "FormatTest")
3525+
.addGlobalProperty(CodegenConstants.MODEL_DOCS, "false")
3526+
.addGlobalProperty(CodegenConstants.MODEL_TESTS, "false")
3527+
.setInputSpec("src/test/resources/2_0/java/issue-6496.yaml")
3528+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
3529+
3530+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
3531+
3532+
assertThat(files).hasSize(1).first(FILE).content()
3533+
.contains(
3534+
"@JsonDeserialize(as = LinkedHashSet.class)",
3535+
"@JsonSerialize(using = ToStringSerializer.class)",
3536+
"com.fasterxml.jackson.databind.ser.std.ToStringSerializer",
3537+
"com.fasterxml.jackson.databind.annotation.JsonDeserialize",
3538+
"com.fasterxml.jackson.databind.annotation.JsonSerialize"
3539+
);
3540+
}
34753541
}
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)