Skip to content

Commit 78499b6

Browse files
committed
add unit tests
1 parent 59bd0eb commit 78499b6

3 files changed

Lines changed: 271 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ public PropertyAssert withType(final String expectedType) {
2626
return this;
2727
}
2828

29+
public PropertyAssert isArray() {
30+
Assertions.assertThat(actual.getCommonType().isArrayType())
31+
.withFailMessage("Expected property %s to be array, but it was NOT", actual.getVariable(0).getNameAsString())
32+
.isEqualTo(true);
33+
return this;
34+
}
35+
36+
public PropertyAssert isNotArray() {
37+
Assertions.assertThat(actual.getCommonType().isArrayType())
38+
.withFailMessage("Expected property %s NOT to be array, but it was", actual.getVariable(0).getNameAsString())
39+
.isEqualTo(false);
40+
return this;
41+
}
42+
2943
public PropertyAnnotationsAssert assertPropertyAnnotations() {
3044
return new PropertyAnnotationsAssert(this, actual.getAnnotations());
3145
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,92 @@ public void testSchemaImplements() throws IOException {
864864
.implementsInterfaces(fooInterface, fooAnotherInterface);
865865
}
866866

867+
868+
@Test
869+
public void shouldHandleFormatByteCorrectlyForAllApiParametersAndProperties() throws IOException {
870+
final SpringCodegen codegen = new SpringCodegen();
871+
872+
final Map<String, File> files = generateFiles(codegen, "src/test/resources/3_0/spring/byte-format-edge-cases.yaml");
873+
// Query parameters: both plain text and Base64-encoded fields are mapped to String
874+
JavaFileAssert.assertThat(files.get("QueryApi.java"))
875+
.assertMethod("queryParams")
876+
.assertParameter("plain")
877+
.hasType("String"); // plain query param → always String
878+
JavaFileAssert.assertThat(files.get("QueryApi.java"))
879+
.assertMethod("queryParams")
880+
.assertParameter("_byte")
881+
.hasType("String"); // Base64 query param → String (manual decoding needed)
882+
883+
// Path parameters: same behavior as query params
884+
JavaFileAssert.assertThat(files.get("PathApi.java"))
885+
.assertMethod("pathParams")
886+
.assertParameter("plain")
887+
.hasType("String"); // path param → String
888+
JavaFileAssert.assertThat(files.get("PathApi.java"))
889+
.assertMethod("pathParams")
890+
.assertParameter("_byte")
891+
.hasType("String"); // Base64 path param → String
892+
893+
// Header parameters: always String
894+
JavaFileAssert.assertThat(files.get("HeaderApi.java"))
895+
.assertMethod("headerParams")
896+
.assertParameter("xPlain")
897+
.hasType("String"); // header → String
898+
JavaFileAssert.assertThat(files.get("HeaderApi.java"))
899+
.assertMethod("headerParams")
900+
.assertParameter("xByte")
901+
.hasType("String"); // Base64 header → String
902+
903+
// Cookie parameters: always String
904+
JavaFileAssert.assertThat(files.get("CookieApi.java"))
905+
.assertMethod("cookieParams")
906+
.assertParameter("plain")
907+
.hasType("String"); // cookie → String
908+
JavaFileAssert.assertThat(files.get("CookieApi.java"))
909+
.assertMethod("cookieParams")
910+
.assertParameter("_byte")
911+
.hasType("String"); // Base64 cookie → String
912+
913+
// Form fields: text fields → String
914+
JavaFileAssert.assertThat(files.get("FormApi.java"))
915+
.assertMethod("formParams")
916+
.assertParameter("plain")
917+
.hasType("String"); // form field → String
918+
JavaFileAssert.assertThat(files.get("FormApi.java"))
919+
.assertMethod("formParams")
920+
.assertParameter("_byte")
921+
.hasType("String"); // Base64 form field → String
922+
923+
// Multipart fields: text fields → String, files → MultipartFile
924+
JavaFileAssert.assertThat(files.get("MultipartApi.java"))
925+
.assertMethod("multipartParams")
926+
.assertParameter("plain")
927+
.hasType("String"); // multipart text field → String
928+
JavaFileAssert.assertThat(files.get("MultipartApi.java"))
929+
.assertMethod("multipartParams")
930+
.assertParameter("_byte")
931+
.hasType("String"); // Base64 multipart text → String
932+
JavaFileAssert.assertThat(files.get("MultipartApi.java"))
933+
.assertMethod("multipartParams")
934+
.assertParameter("file")
935+
.hasType("MultipartFile"); // binary file upload → MultipartFile
936+
937+
// Form request DTO: JSON or form object mapping
938+
JavaFileAssert.assertThat(files.get("FormParamsRequest.java"))
939+
.assertProperty("plain")
940+
.withType("String"); // text property → String
941+
JavaFileAssert.assertThat(files.get("FormParamsRequest.java"))
942+
.assertProperty("_byte")
943+
.isArray()
944+
.withType("byte"); // Base64 property in DTO → auto-decoded to byte[]
945+
946+
// Binary request body: bound as Resource for streaming
947+
JavaFileAssert.assertThat(files.get("BinaryBodyApi.java"))
948+
.assertMethod("binaryBody")
949+
.assertParameter("body")
950+
.hasType("org.springframework.core.io.Resource"); // raw binary body → Resource (streamable)
951+
}
952+
867953
@Test
868954
public void shouldAddParameterWithInHeaderWhenImplicitHeadersIsTrue_issue14418() throws IOException {
869955
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Byte Format Edge Cases
4+
version: 1.0.0
5+
6+
paths:
7+
/queryfdsfsd:
8+
get:
9+
operationId: queryParamsfdsfsfdsfs
10+
summary: Query parametersfsdaf
11+
parameters:
12+
- name: plain
13+
in: query
14+
schema:
15+
type: string
16+
17+
responses:
18+
'204':
19+
description: No content
20+
21+
/query:
22+
get:
23+
operationId: queryParams
24+
summary: Query parameters
25+
parameters:
26+
- name: plain
27+
in: query
28+
schema:
29+
type: string
30+
- name: byte
31+
in: query
32+
schema:
33+
type: string
34+
format: byte
35+
responses:
36+
'204':
37+
description: No content
38+
39+
/path/{plain}/{byte}:
40+
get:
41+
operationId: pathParams
42+
summary: Path parameters
43+
parameters:
44+
- name: plain
45+
in: path
46+
required: true
47+
schema:
48+
type: string
49+
- name: byte
50+
in: path
51+
required: true
52+
schema:
53+
type: string
54+
format: byte
55+
responses:
56+
'204':
57+
description: No content
58+
59+
/header:
60+
get:
61+
operationId: headerParams
62+
summary: Header parameters
63+
parameters:
64+
- name: X-Plain
65+
in: header
66+
schema:
67+
type: string
68+
- name: X-Byte
69+
in: header
70+
schema:
71+
type: string
72+
format: byte
73+
responses:
74+
'204':
75+
description: No content
76+
77+
/cookie:
78+
get:
79+
operationId: cookieParams
80+
summary: Cookie parameters
81+
parameters:
82+
- name: plain
83+
in: cookie
84+
schema:
85+
type: string
86+
- name: byte
87+
in: cookie
88+
schema:
89+
type: string
90+
format: byte
91+
responses:
92+
'204':
93+
description: No content
94+
95+
/form:
96+
post:
97+
operationId: formParams
98+
summary: application/x-www-form-urlencoded
99+
requestBody:
100+
required: true
101+
content:
102+
application/x-www-form-urlencoded:
103+
schema:
104+
type: object
105+
properties:
106+
plain:
107+
type: string
108+
byte:
109+
type: string
110+
format: byte
111+
responses:
112+
'204':
113+
description: No content
114+
115+
/multipart:
116+
post:
117+
operationId: multipartParams
118+
summary: multipart/form-data
119+
requestBody:
120+
required: true
121+
content:
122+
multipart/form-data:
123+
schema:
124+
type: object
125+
properties:
126+
plain:
127+
type: string
128+
byte:
129+
type: string
130+
format: byte
131+
file:
132+
type: string
133+
format: binary
134+
responses:
135+
'204':
136+
description: No content
137+
138+
/json-body:
139+
post:
140+
operationId: jsonBody
141+
summary: JSON request body
142+
requestBody:
143+
required: true
144+
content:
145+
application/json:
146+
schema:
147+
type: object
148+
properties:
149+
plain:
150+
type: string
151+
byte:
152+
type: string
153+
format: byte
154+
responses:
155+
'204':
156+
description: No content
157+
158+
/binary-body:
159+
post:
160+
operationId: binaryBody
161+
summary: Raw binary body
162+
requestBody:
163+
required: true
164+
content:
165+
application/octet-stream:
166+
schema:
167+
type: string
168+
format: binary
169+
responses:
170+
'204':
171+
description: No content

0 commit comments

Comments
 (0)