[Java][Spring] Server codegen gradle task fails when operation only has header parameters, and generator has implicitHeaders=true
Created by: jcohen-stingray
Description
Gradle codegen task fails with IndexOutOfBoundException. This is caused when the generator task has the additional parameter of implicitHeaders = true and the operation in the api yaml specification file only has header parameters.
openapi-generator version
3.0.3
OpenAPI gradle task specification (partial)
swagger: '2.0'
paths:
/account-info:
get:
tags:
- Account
summary: Get the account information
operationId: getAccountInfo
parameters:
# HEADERS
- $ref: '#/parameters/userIdTokenHeaderParam'
- $ref: '#/parameters/clientIdHeaderParam'
- $ref: '#/parameters/languageHeaderParam'
responses:
200:
description: Account Info
schema:
$ref: '#/definitions/Account'
headers:
cache-control:
type: string
description: Caching rules
400:
$ref: '#/responses/BadRequest'
401:
$ref: '#/responses/Unauthorized'
403:
$ref: '#/responses/Forbidden'
500:
$ref: '#/responses/UnexpectedError'
Gradle task specification for generation
openApiGenerate {
...
generatorName = 'spring'
additionalProperties = [
...
'interfaceOnly' : 'true',
'java8' : 'true',
'delegatePattern' : 'true',
'implicitHeaders' : 'true',
'useTags' : 'true',
// 'useOptional' : 'true', // bug in codegen prevents using this with delegatePattern=true
// 'async' : 'true', // Only use if necessary (defaults to CompletableFuture for java8, Future for java<8)
// 'responseWrapper' : 'CompletableFuture',
]
}
Suggest a fix/enhancement
Add check for list size before setting last element value
Class: org.openapitools.codegen.languages.SpringCodegen Original method:
private void removeHeadersFromAllParams(List<CodegenParameter> allParams) {
if(allParams.isEmpty()){
return;
}
final ArrayList<CodegenParameter> copy = new ArrayList<>(allParams);
allParams.clear();
for(CodegenParameter p : copy){
if(!p.isHeaderParam){
allParams.add(p);
}
}
allParams.get(allParams.size()-1).hasMore =false;
}
Suggested fix:
private void removeHeadersFromAllParams(List<CodegenParameter> allParams) {
if(allParams.isEmpty()){
return;
}
final ArrayList<CodegenParameter> copy = new ArrayList<>(allParams);
allParams.clear();
for(CodegenParameter p : copy){
if(!p.isHeaderParam){
allParams.add(p);
}
}
if (!allParams.isEmpty()) {
allParams.get(allParams.size()-1).hasMore =false;
}
}