Skip to content

Commit

Permalink
[java][feign] Fix the character escaping in feign generated Content-t…
Browse files Browse the repository at this point in the history
…ype and Accept headers in order to generate proper values for these fields (#19895) (#19912)
  • Loading branch information
CaptainAye authored Oct 19, 2024
1 parent fc16182 commit c8f2d2b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public interface {{classname}} extends ApiClient.Api {
{{/isDeprecated}}
@RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
@Headers({
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -77,8 +77,8 @@ public interface {{classname}} extends ApiClient.Api {
{{/isDeprecated}}
@RequestLine("{{httpMethod}} {{{path}}}{{#hasQueryParams}}?{{/hasQueryParams}}{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
@Headers({
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -122,8 +122,8 @@ public interface {{classname}} extends ApiClient.Api {
{{/isDeprecated}}
@RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
@Headers({
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -162,8 +162,8 @@ public interface {{classname}} extends ApiClient.Api {
{{/isDeprecated}}
@RequestLine("{{httpMethod}} {{{path}}}?{{#queryParams}}{{baseName}}={{=<% %>=}}{<%paramName%>}<%={{ }}=%>{{^-last}}&{{/-last}}{{/queryParams}}")
@Headers({
{{#vendorExtensions.x-content-type}} "Content-Type: {{vendorExtensions.x-content-type}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
{{#vendorExtensions.x-content-type}} "Content-Type: {{{vendorExtensions.x-content-type}}}",
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{{.}}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,34 @@ private Optional<CodegenOperation> getByCriteria(List<CodegenOperation> codegenO
);
}

/**
* see https://github.com/OpenAPITools/openapi-generator/issues/19895
*/
@Test public void testCharsetInContentTypeCorrectlyEncodedForFeignApi_issue19895() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(FEIGN)
.setInputSpec("src/test/resources/3_0/issue_19895.yaml")
.setOutputDir(output.toString().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

validateJavaSourceFiles(files);
var defaultApiFile = output.resolve("src/main/java/org/openapitools/client/api/DefaultApi.java");
assertThat(files).contains(defaultApiFile.toFile());
assertThat(defaultApiFile).content()
.doesNotContain(
"Content-Type: application/json;charset&#x3D;utf-8",
"Accept: application/json;charset&#x3D;utf-8")
.contains(
"Content-Type: application/json;charset=utf-8",
"Accept: application/json;charset=utf-8"
);
}

/**
* See https://github.com/OpenAPITools/openapi-generator/issues/6715
* <p>
Expand Down
54 changes: 54 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/issue_19895.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
openapi: 3.0.1
info:
title: sample spec
description: "Sample spec"
version: 0.0.1

paths:
/v1/sample:
post:
operationId: samplePost
requestBody:
content:
application/json;charset=utf-8:
schema:
properties:
name:
type: string
type: object
responses:
200:
description: success
content:
application/json;charset=utf-8:
schema:
properties:
response:
type: object
type: object
put:
operationId: samplePut
parameters:
- in: query
name: limit
schema:
type: string
requestBody:
content:
application/json;charset=utf-8:
schema:
properties:
name:
type: string
type: object
responses:
200:
description: success
content:
application/json;charset=utf-8:
schema:
properties:
response:
type: object
type: object

0 comments on commit c8f2d2b

Please sign in to comment.