[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)

This commit is contained in:
Tomasz Letachowicz 2024-10-19 09:12:45 +02:00 committed by GitHub
parent fc161821a7
commit c8f2d2be1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 90 additions and 8 deletions

View File

@ -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}}
})
@ -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}}
})
@ -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}}
})
@ -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}}
})

View File

@ -1273,6 +1273,34 @@ public class JavaClientCodegenTest {
);
}
/**
* 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>

View File

@ -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