Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Java generator to generate valid @RequestMappings where the produce field takes list of string parameters instead of a single comma-separated string #18092

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
String contentType = consumes.isEmpty() ? defaultContentType : consumes.get(0);
operation.addExtension("x-content-type", contentType);
}
String accepts = getAccepts(openAPI, operation);
String[] accepts = getAccepts(openAPI, operation);
operation.addExtension("x-accepts", accepts);
}
}
Expand Down Expand Up @@ -1835,19 +1835,12 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}
}

private static String getAccepts(OpenAPI openAPIArg, Operation operation) {
private static String[] getAccepts(OpenAPI openAPIArg, Operation operation) {
final Set<String> producesInfo = getProducesInfo(openAPIArg, operation);
if (producesInfo != null && !producesInfo.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (String produce : producesInfo) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(produce);
}
return sb.toString();
return producesInfo.toArray(new String[] {});
}
return "application/json"; // default media type
return new String[] { "application/json" }; // default media type
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface {{classname}} extends ApiClient.Api {
@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}}",{{#headerParams}}
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -73,7 +73,7 @@ public interface {{classname}} extends ApiClient.Api {
@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}}",{{#headerParams}}
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -118,7 +118,7 @@ public interface {{classname}} extends ApiClient.Api {
@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}}",{{#headerParams}}
{{/vendorExtensions.x-content-type}} "Accept: {{#vendorExtensions.x-accepts}}{{.}}{{^-last}},{{/-last}}{{/vendorExtensions.x-accepts}}",{{#headerParams}}
"{{baseName}}: {{=<% %>=}}{<%paramName%>}<%={{ }}=%>"{{^-last}},
{{/-last}}{{/headerParams}}
})
Expand Down Expand Up @@ -158,7 +158,7 @@ public interface {{classname}} extends ApiClient.Api {
@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}}",{{#headerParams}}
{{/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 @@ -157,9 +157,7 @@ public class {{classname}} {
{{#x-content-type}}
reqSpec.setContentType("{{x-content-type}}");
{{/x-content-type}}
{{#x-accepts}}
reqSpec.setAccept("{{x-accepts}}");
{{/x-accepts}}
reqSpec.setAccept("{{#x-accepts}}{{.}}{{^-last}},{{/-last}}{{/x-accepts}}");
{{/vendorExtensions}}
this.respSpec = new ResponseSpecBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public interface {{classname}} {
@RequestMapping(
method = RequestMethod.{{httpMethod}},
value = "{{{path}}}"{{#singleContentTypes}}{{#hasProduces}},
produces = "{{{vendorExtensions.x-accepts}}}"{{/hasProduces}}{{#hasConsumes}},
produces = { {{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{/hasProduces}}{{#hasConsumes}},
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}}{{#hasVersionHeaders}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public interface {{classname}} {
{{/isDeprecated}}
@HttpExchange(
method = "{{{httpMethod}}}",
value = "{{{path}}}"{{#vendorExtensions.x-accepts}},
accept = "{{{vendorExtensions.x-accepts}}}"{{/vendorExtensions.x-accepts}}{{#vendorExtensions.x-content-type}},
value = "{{{path}}}",
accept = { {{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} }{{#vendorExtensions.x-content-type}},
contentType = "{{{vendorExtensions.x-content-type}}}"{{/vendorExtensions.x-content-type}}
)
{{>responseType}} {{operationId}}(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface {{classname}} {
@RequestMapping(
method = RequestMethod.{{httpMethod}},
value = "{{{path}}}"{{#singleContentTypes}},
produces = "{{{vendorExtensions.x-accepts}}}",
produces = { {{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}} },
consumes = "{{{vendorExtensions.x-content-type}}}"{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
produces = { {{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}} }{{/hasProduces}}{{#hasConsumes}},
consumes = { {{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}} }{{/hasConsumes}}{{/singleContentTypes}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class {{classname}}Controller({{#serviceInterface}}@Autowired(required = true) v
@RequestMapping(
method = [RequestMethod.{{httpMethod}}],
value = ["{{#lambdaEscapeInNormalString}}{{path}}{{/lambdaEscapeInNormalString}}"]{{#singleContentTypes}}{{#hasProduces}},
produces = "{{{vendorExtensions.x-accepts}}}"{{/hasProduces}}{{#hasConsumes}},
produces = [{{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}}]{{/hasProduces}}{{#hasConsumes}},
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ interface {{classname}} {
@RequestMapping(
method = [RequestMethod.{{httpMethod}}],
value = ["{{#lambdaEscapeInNormalString}}{{path}}{{/lambdaEscapeInNormalString}}"]{{#singleContentTypes}}{{#hasProduces}},
produces = "{{{vendorExtensions.x-accepts}}}"{{/hasProduces}}{{#hasConsumes}},
produces = [{{#vendorExtensions.x-accepts}}"{{.}}"{{^-last}}, {{/-last}}{{/vendorExtensions.x-accepts}}]{{/hasProduces}}{{#hasConsumes}},
consumes = "{{{vendorExtensions.x-content-type}}}"{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}},
produces = [{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}]{{/hasProduces}}{{#hasConsumes}},
consumes = [{{#consumes}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/consumes}}]{{/hasConsumes}}{{/singleContentTypes}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public void testPreprocessOpenApiIncludeAllMediaTypesInAcceptHeader() throws Exc
codegen.preprocessOpenAPI(openAPI);

Assert.assertEquals(codegen.getArtifactVersion(), openAPI.getInfo().getVersion());
Assert.assertEquals(openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts"), "application/json,application/xml");

Object xAccepts = openAPI.getPaths().get("/pet").getPost().getExtensions().get("x-accepts");
Assert.assertTrue(xAccepts instanceof String[]);
Assert.assertTrue(List.of((String[]) xAccepts).containsAll(List.of("application/json", "application/xml")));
}

@Test
Expand Down
78 changes: 52 additions & 26 deletions samples/client/echo_api/java/apache-httpclient/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ paths:
summary: Test path parameter(s)
tags:
- path
x-accepts: text/plain
x-accepts:
- text/plain
/form/integer/boolean/string:
post:
description: Test form parameter(s)
Expand All @@ -79,7 +80,8 @@ paths:
tags:
- form
x-content-type: application/x-www-form-urlencoded
x-accepts: text/plain
x-accepts:
- text/plain
/form/oneof:
post:
description: Test form parameter(s) for oneOf schema
Expand All @@ -100,7 +102,8 @@ paths:
tags:
- form
x-content-type: application/x-www-form-urlencoded
x-accepts: text/plain
x-accepts:
- text/plain
/header/integer/boolean/string/enums:
get:
description: Test header parameter(s)
Expand Down Expand Up @@ -155,7 +158,8 @@ paths:
summary: Test header parameter(s)
tags:
- header
x-accepts: text/plain
x-accepts:
- text/plain
/query/enum_ref_string:
get:
description: Test query parameter(s)
Expand Down Expand Up @@ -189,7 +193,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/datetime/date/string:
get:
description: Test query parameter(s)
Expand Down Expand Up @@ -228,7 +233,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/integer/boolean/string:
get:
description: Test query parameter(s)
Expand Down Expand Up @@ -265,7 +271,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_form/explode_true/array_string:
get:
description: Test query parameter(s)
Expand All @@ -288,7 +295,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_form/explode_false/array_integer:
get:
description: Test query parameter(s)
Expand All @@ -313,7 +321,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_form/explode_false/array_string:
get:
description: Test query parameter(s)
Expand All @@ -338,7 +347,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_form/explode_true/object:
get:
description: Test query parameter(s)
Expand All @@ -361,7 +371,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_form/explode_true/object/allOf:
get:
description: Test query parameter(s)
Expand All @@ -384,7 +395,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_deepObject/explode_true/object:
get:
description: Test query parameter(s)
Expand All @@ -407,7 +419,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/query/style_deepObject/explode_true/object/allOf:
get:
description: Test query parameter(s)
Expand All @@ -430,7 +443,8 @@ paths:
summary: Test query parameter(s)
tags:
- query
x-accepts: text/plain
x-accepts:
- text/plain
/body/application/octetstream/binary:
post:
description: Test body parameter(s)
Expand All @@ -452,7 +466,8 @@ paths:
tags:
- body
x-content-type: application/octet-stream
x-accepts: text/plain
x-accepts:
- text/plain
/echo/body/Pet:
post:
description: Test body parameter(s)
Expand All @@ -470,7 +485,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: application/json
x-accepts:
- application/json
/echo/body/allOf/Pet:
post:
description: Test body parameter(s)
Expand All @@ -488,7 +504,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: application/json
x-accepts:
- application/json
/echo/body/Pet/response_string:
post:
description: Test empty response body
Expand All @@ -506,7 +523,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: text/plain
x-accepts:
- text/plain
/echo/body/Tag/response_string:
post:
description: Test empty json (request body)
Expand All @@ -524,7 +542,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: text/plain
x-accepts:
- text/plain
/echo/body/FreeFormObject/response_string:
post:
description: Test free form object
Expand All @@ -546,7 +565,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: text/plain
x-accepts:
- text/plain
/echo/body/string_enum:
post:
description: Test string enum response body
Expand All @@ -568,7 +588,8 @@ paths:
tags:
- body
x-content-type: application/json
x-accepts: application/json
x-accepts:
- application/json
/binary/gif:
post:
description: Test binary (gif) response body
Expand All @@ -584,7 +605,8 @@ paths:
summary: Test binary (gif) response body
tags:
- body
x-accepts: image/gif
x-accepts:
- image/gif
/body/application/octetstream/single_binary:
post:
description: Test single binary in multipart mime
Expand All @@ -605,7 +627,8 @@ paths:
tags:
- body
x-content-type: multipart/form-data
x-accepts: text/plain
x-accepts:
- text/plain
/body/application/octetstream/array_of_binary:
post:
description: Test array of binary in multipart mime
Expand All @@ -626,7 +649,8 @@ paths:
tags:
- body
x-content-type: multipart/form-data
x-accepts: text/plain
x-accepts:
- text/plain
/auth/http/basic:
post:
description: To test HTTP basic authentication
Expand All @@ -643,7 +667,8 @@ paths:
summary: To test HTTP basic authentication
tags:
- auth
x-accepts: text/plain
x-accepts:
- text/plain
/auth/http/bearer:
post:
description: To test HTTP bearer authentication
Expand All @@ -660,7 +685,8 @@ paths:
summary: To test HTTP bearer authentication
tags:
- auth
x-accepts: text/plain
x-accepts:
- text/plain
components:
requestBodies:
Pet:
Expand Down
Loading
Loading