Skip to content

Commit c8f2d2b

Browse files
authored
[java][feign] Fix the character escaping in feign generated Content-type and Accept headers in order to generate proper values for these fields (#19895) (#19912)
1 parent fc16182 commit c8f2d2b

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/feign/api.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public interface {{classname}} extends ApiClient.Api {
4747
{{/isDeprecated}}
4848
@RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
4949
@Headers({
50-
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
51-
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
50+
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
51+
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
5252
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
5353
{{/-last}}{{/headerParams}}
5454
})
@@ -77,8 +77,8 @@ public interface {{classname}} extends ApiClient.Api {
7777
{{/isDeprecated}}
7878
@RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
7979
@Headers({
80-
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
81-
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
80+
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
81+
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
8282
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
8383
{{/-last}}{{/headerParams}}
8484
})
@@ -122,8 +122,8 @@ public interface {{classname}} extends ApiClient.Api {
122122
{{/isDeprecated}}
123123
@RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
124124
@Headers({
125-
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
126-
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
125+
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
126+
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
127127
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
128128
{{/-last}}{{/headerParams}}
129129
})
@@ -162,8 +162,8 @@ public interface {{classname}} extends ApiClient.Api {
162162
{{/isDeprecated}}
163163
@RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
164164
@Headers({
165-
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
166-
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
165+
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
166+
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
167167
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
168168
{{/-last}}{{/headerParams}}
169169
})

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,34 @@ private Optional<CodegenOperation> getByCriteria(List<CodegenOperation> codegenO
12731273
);
12741274
}
12751275

1276+
/**
1277+
* see https://github.com/OpenAPITools/openapi-generator/issues/19895
1278+
*/
1279+
@Test public void testCharsetInContentTypeCorrectlyEncodedForFeignApi_issue19895() {
1280+
final Path output = newTempFolder();
1281+
final CodegenConfigurator configurator = new CodegenConfigurator()
1282+
.setGeneratorName("java")
1283+
.setLibrary(FEIGN)
1284+
.setInputSpec("src/test/resources/3_0/issue_19895.yaml")
1285+
.setOutputDir(output.toString().replace("\\", "/"));
1286+
1287+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
1288+
DefaultGenerator generator = new DefaultGenerator();
1289+
List<File> files = generator.opts(clientOptInput).generate();
1290+
1291+
validateJavaSourceFiles(files);
1292+
var defaultApiFile = output.resolve("src/main/java/org/openapitools/client/api/DefaultApi.java");
1293+
assertThat(files).contains(defaultApiFile.toFile());
1294+
assertThat(defaultApiFile).content()
1295+
.doesNotContain(
1296+
"Content-Type: application/json;charset&#x3D;utf-8",
1297+
"Accept: application/json;charset&#x3D;utf-8")
1298+
.contains(
1299+
"Content-Type: application/json;charset=utf-8",
1300+
"Accept: application/json;charset=utf-8"
1301+
);
1302+
}
1303+
12761304
/**
12771305
* See https://github.com/OpenAPITools/openapi-generator/issues/6715
12781306
* <p>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
openapi: 3.0.1
2+
info:
3+
title: sample spec
4+
description: "Sample spec"
5+
version: 0.0.1
6+
7+
paths:
8+
/v1/sample:
9+
post:
10+
operationId: samplePost
11+
requestBody:
12+
content:
13+
application/json;charset=utf-8:
14+
schema:
15+
properties:
16+
name:
17+
type: string
18+
type: object
19+
responses:
20+
200:
21+
description: success
22+
content:
23+
application/json;charset=utf-8:
24+
schema:
25+
properties:
26+
response:
27+
type: object
28+
type: object
29+
put:
30+
operationId: samplePut
31+
parameters:
32+
- in: query
33+
name: limit
34+
schema:
35+
type: string
36+
requestBody:
37+
content:
38+
application/json;charset=utf-8:
39+
schema:
40+
properties:
41+
name:
42+
type: string
43+
type: object
44+
responses:
45+
200:
46+
description: success
47+
content:
48+
application/json;charset=utf-8:
49+
schema:
50+
properties:
51+
response:
52+
type: object
53+
type: object
54+

0 commit comments

Comments
 (0)