Skip to content

Commit fdbb563

Browse files
authored
fix: Remove stranded commas when generating spec with cookies with Retrofit2 (OpenAPITools#24178)
1 parent 63ce47f commit fdbb563

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,13 @@ public int compare(CodegenParameter one, CodegenParameter another) {
955955
return 0;
956956
}
957957
});
958+
959+
// Retrofit2 has no annotation for cookie parameters, so they cannot be
960+
// expressed in the interface method signature. Drop them so the generated
961+
// interface (and its Javadoc) only references parameters that are actually
962+
// rendered; otherwise the parameter separator logic would emit a dangling
963+
// comma for the omitted parameter.
964+
operation.allParams.removeIf(param -> param.isCookieParam);
958965
}
959966
}
960967
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,44 @@ public void testImportMappingResult() {
337337
.content().contains("import java.time.Instant;");
338338
}
339339

340+
@Test
341+
public void testRetrofit2CookieParamsOmittedFromSignature() {
342+
final Path output = newTempFolder();
343+
final CodegenConfigurator configurator = new CodegenConfigurator()
344+
.setGeneratorName(JAVA_GENERATOR)
345+
.setLibrary(JavaClientCodegen.RETROFIT_2)
346+
.setInputSpec("src/test/resources/3_0/java/retrofit2-cookie-params.yaml")
347+
.setOutputDir(output.toString().replace("\\", "/"));
348+
349+
List<File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate();
350+
351+
// Retrofit2 has no annotation for cookie parameters, so they are omitted from the
352+
// interface method signature. Previously the cookie param was dropped while its
353+
// separator comma was kept, producing an uncompilable signature such as
354+
// "cookieLast(@Header(...) String xApiVersion, );". validateJavaSourceFiles parses
355+
// every generated file, so a stray leading/trailing comma fails the parse here.
356+
validateJavaSourceFiles(files);
357+
358+
Map<String, File> fileMap = files.stream()
359+
.collect(Collectors.toMap(File::getName, Function.identity()));
360+
361+
JavaFileAssert.assertThat(fileMap.get("DefaultApi.java"))
362+
// cookie param is last -> no trailing comma, cookie param omitted
363+
.assertMethod("cookieLast", "String")
364+
.assertParameter("xApiVersion")
365+
.toMethod()
366+
.toFileAssert()
367+
// cookie param is first -> no leading comma, remaining params kept
368+
.assertMethod("cookieFirst", "String", "String")
369+
.assertParameter("xApiVersion")
370+
.toMethod()
371+
.assertParameter("filter")
372+
.toMethod()
373+
.toFileAssert()
374+
// only a cookie param -> empty parameter list
375+
.assertMethod("cookieOnly");
376+
}
377+
340378
@Test
341379
public void testSupportedSecuritySchemesJersey() {
342380
final JavaClientCodegen codegen = new JavaClientCodegen();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Retrofit2 cookie params
4+
description: >
5+
Retrofit2 has no annotation for cookie parameters, so they are omitted from
6+
the generated interface method signature. This spec exercises cookie params
7+
in various positions to ensure the parameter separator logic never produces
8+
an uncompilable signature (e.g. a trailing or leading comma).
9+
version: 1.0.0
10+
paths:
11+
/cookie-last:
12+
get:
13+
operationId: cookieLast
14+
parameters:
15+
- name: x-api-version
16+
in: header
17+
required: true
18+
schema:
19+
type: string
20+
- name: JSESSIONID
21+
in: cookie
22+
required: true
23+
schema:
24+
type: string
25+
responses:
26+
'200':
27+
description: OK
28+
/cookie-first:
29+
get:
30+
operationId: cookieFirst
31+
parameters:
32+
- name: JSESSIONID
33+
in: cookie
34+
required: true
35+
schema:
36+
type: string
37+
- name: x-api-version
38+
in: header
39+
required: true
40+
schema:
41+
type: string
42+
- name: filter
43+
in: query
44+
required: true
45+
schema:
46+
type: string
47+
responses:
48+
'200':
49+
description: OK
50+
/cookie-only:
51+
get:
52+
operationId: cookieOnly
53+
parameters:
54+
- name: JSESSIONID
55+
in: cookie
56+
required: true
57+
schema:
58+
type: string
59+
responses:
60+
'200':
61+
description: OK

0 commit comments

Comments
 (0)