swagger-api / swagger-api/swagger-codegen
[SPRING] implicitHeaders crashes if all parameters on an operation are header parameters
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
If implicitHeaders=true is set when generating an API for a Spring server and an operation contains only header parameters, the code crashes due to an out of bounds exception.
Exception in thread "Thread-0" java.lang.RuntimeException: Could not generate api file for 'div'
at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:569)
at io.swagger.codegen.v3.DefaultGenerator.generate(DefaultGenerator.java:776)
at io.swagger.codegen.v3.cli.cmd.Generate.run(Generate.java:342)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at io.swagger.codegen.v3.generators.java.SpringCodegen.removeHeadersFromAllParams(SpringCodegen.java:570)
at io.swagger.codegen.v3.generators.java.SpringCodegen.postProcessOperations(SpringCodegen.java:509)
at io.swagger.codegen.v3.DefaultGenerator.processOperations(DefaultGenerator.java:984)
at io.swagger.codegen.v3.DefaultGenerator.generateApis(DefaultGenerator.java:486)
... 3 more
Swagger-codegen version
3.0.10.
Swagger declaration file content or url
openapi: 3.0.0
info:
title: Failed Header Demo
version: 1.0.0
description: Demonstrate a crash in Swagger Codegen 3.0.10
paths:
/div:
get:
tags:
- div
operationId: getV1Div
parameters:
- in: header
name: My-Request-Id
description: Unique Correlation Id For Each Request
required: true
schema:
type: string
responses:
200:
description: Successful Response
content:
application/json:
schema:
$ref: '#/components/schemas/DivResponse'
components:
schemas:
DivResponse:
type: object
properties:
divs:
type: array
items:
type: string
required:
- divs
Command line used for generation
swagger-codegen generate -i bug.yml
-l spring
--api-package com.foo.api
-Dapis=div
--additional-properties implicitHeaders=true
Steps to reproduce
- Execute the command above with the provided file.
- Witness the crash in all its glory.
Related issues/PRs
None
Suggest a fix/enhancement
The offending line of code is here. The bug in the algorithm is obvious from a simple code review. The allParams parameter is duplicated and then cleared, so it has a size of 0. Non-header parameters from the copy of the parameters are added back into allParams. Because of this filtering, it is possible for allParams to still have a size of 0. And then, the last item in the list is modified by using the size of the list minus 1 to identify the index. This results in a call to List<>.get(-1), which is clearly invalid.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with SpringCodegen.java, specifically removeHeadersFromAllParams, and reproduce the failure using the supplied OpenAPI document and generation command. The work is done when Spring server generation succeeds for an operation whose only parameter is a header and the generated API is produced without the IndexOutOfBoundsException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring
- Domain
- api, backend, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100