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

Text/plain request body to non-existing enum value returns 500 internal server error instead of 400 bad request #11607

Open
jarandia-hc opened this issue Feb 20, 2025 · 1 comment
Labels
type: bug Something isn't working

Comments

@jarandia-hc
Copy link

Expected Behavior

When a value that does not exist in an enum is sent in the request body of a request with Content-Type=text/plain, it should return 400 Bad request.

Actual Behaviour

When a value that does not exist in an enum is sent in the request body of a request with Content-Type=text/plain, it returns 500 Internal server error.

Steps To Reproduce

  1. Create an enum. For example:
public enum Language {

  EN_CA("en-CA"),
  FR_CA("fr-CA");

  @JsonValue
  private final String locale;

  public static Language fromLocale(String locale) {
    return (Language) Arrays.stream(values()).filter((lang) -> {
      return Objects.equals(lang.getLocale(), locale);
    }).findFirst().orElse(null);
  }

  public String toString() {
    return this.locale;
  }

  private Language(String locale) {
    this.locale = locale;
  }

  public String getLocale() {
    return this.locale;
  }
}
  1. Create a controller that receives the created enum as request body. For example:
@Controller
public class LanguageController {

  @Put(value = "/language", processes = TEXT_PLAIN)
  public HttpResponse<Void> putLanguage(@Body Language language) {
    return HttpResponse.noContent();
  }
}
  1. Run the application and make a request to the endpoint with a non-existing enum value. For example:
curl --location --request PUT 'http://localhost:8080/language' \
--header 'Content-Type: text/plain' \
--data 'en-EN'

Notice that en-EN does not exist in the Language enum.

  1. Verify that the returned HTTP status is 400 Bad request, not 500 Internal server error

Environment Information

JDK version: 17

Example Application

No response

Version

4.7.6

@jarandia-hc
Copy link
Author

This issue happens because the byteBuffer is released twice. First it is released in io.micronaut.http.body.TextPlainObjectBodyReader.read0, and then it is released again in io.micronaut.http.server.netty.binders.NettyBodyAnnotationBinder.read in the finally clause.

@graemerocher graemerocher added the type: bug Something isn't working label Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants