swagger-api / swagger-api/swagger-codegen
[JAVA] NullPointerException from generated Java OAuth code when the OAuth server returns no body
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
When making a HTTP request using the Java code that Swagger generates, and the OAuth server returns a response with no body - e.g. a HTTP 401 Unauthorized response without a body, then the generated code throws a NullPointerException.
The NullPointerException hides the nature of the error (in my case, that the username and password I am trying to use are invalid) and therefore makes debugging harder.
Swagger-codegen version
3.0.13
Suggest a fix/enhancement
The problematic code is here: https://github.com/swagger-api/swagger-codegen/blob/4607a90d7b69463a0ae8fc94fac68fc95d80965e/modules/swagger-codegen/src/main/resources/Java/libraries/feign/auth/OAuth.mustache#L174
feignResponse.body() can be null if the HTTP response did not include a body.
The ideal fix for me would involve detecting HTTP 401 (or possibly, all non-200) responses and throwing an exception with a error message that describes what response code was returned.
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 at modules/swagger-codegen/src/main/resources/Java/libraries/feign/auth/OAuth.mustache around line 174, where feignResponse.body() can be null. Trace how empty-body OAuth responses are handled and verify the generated Java client no longer throws a NullPointerException. Done should preserve a useful error describing the returned HTTP response, especially a 401 response.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- api, authentication
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100