OpenAPITools / OpenAPITools/openapi-generator

[BUG] com.ly.doc.utils.HttpStatusUtil.getStatusCode is correct only in static import

Open
#20,208 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

For follow code can generate exception response correct:

@ExceptionHandler(value = DirectException.class)
@ResponseStatus(BAD_REQUEST)
public ResponseEntity<String> exceptionHandler(DirectException e) {
    return new ResponseEntity<>(e.getBody(), e.getHttpStatus());
}

But not correct when use:

@ExceptionHandler(value = DirectException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> exceptionHandler(DirectException e) {
    return new ResponseEntity<>(e.getBody(), e.getHttpStatus());
}

Because JavaAnnotation.getNamedParameter method return value "HttpStatus.BAD_REQUEST", but HttpStatusEnum.valueOf can't find it

Another bug is get status desc
in IRestDocTemplate

String statusCode = HttpStatusUtil.getStatusCode(adviceMethod.getStatus());
if (statusSet.contains(statusCode)) {
	continue;
}
statusSet.add(statusCode);
ApiExceptionStatus apiExceptionStatus = new ApiExceptionStatus();
apiExceptionStatus.setStatus(statusCode);
apiExceptionStatus.setDesc(HttpStatusUtil.getStatusDescription(statusCode));
apiExceptionStatus.setAuthor(docJavaMethod.getAuthor());
apiExceptionStatus.setDetail(docJavaMethod.getDetail());

the input for HttpStatusUtil.getStatusDescription is a String of the number of the Code, for example "400"

	public static String getStatusDescription(String statusCode) {
		try {
			HttpStatusEnum httpStatusEnum = HttpStatusEnum.valueOf(statusCode);
			return String.valueOf(httpStatusEnum.getReasonPhrase());
		}
		catch (IllegalArgumentException e) {
			return HttpStatusEnum.INTERNAL_SERVER_ERROR.getReasonPhrase();
		}
	}

so this method throw IllegalArgumentException forever, the correct usage is public static HttpStatusEnum valueOf(int statusCode). So need to convert code to int

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating HttpStatusUtil, JavaAnnotation.getNamedParameter, HttpStatusEnum, and the IRestDocTemplate code shown in the issue. Trace both qualified HttpStatus annotation values and numeric status descriptions, then verify that status codes and reason phrases are resolved correctly in the affected API documentation flow.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.