Ask Your Question

What is the process for setting up Swagger Codegen (Maven plugin) to generate an error response for an enum field that does not match?

asked 2021-06-20 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-10-05 04:00:00 +0000

woof gravatar image

To set up Swagger Codegen to generate an error response for an enum field that does not match, you can follow these steps:

  1. Specify the valid enum values in the OpenAPI/Swagger specification using the enum keyword in the schema definition for the field.


    type: string
    enum: [draft, published, archived]
  1. Add the swagger-codegen-maven-plugin to your project's pom.xml file.


            <!-- plugin configuration options -->
  1. Configure the plugin to use a custom template for the error response.


  1. Create a custom template for the error response using the errorModel.mustache file.


  "message": "Invalid value for {{fieldName}}. Allowed values: {{allowedValues}}",
  "code": 400
  1. Run the generate-sources goal of the swagger-codegen-maven-plugin to generate the Java code and the custom error response template.


mvn swagger-codegen:generate
  1. Use the generated Java code to implement your API endpoint and handle errors for fields that do not match the enum.


public Response exampleApiMethod(RequestBody requestBody) {
  try {
    // Validate the request body

    // Process the request

    // Return a successful response
    return Response.ok().build();
  } catch (ValidationException e) {
    // Return a custom error response for the invalid enum value
    return Response
      .entity(renderErrorResponse(e.getFieldName(), e.getAllowedValues()))

private void validateRequestBody(RequestBody requestBody) throws ValidationException {
  // Validate the enum field values
  validateEnumField("status", requestBody.getStatus(), "draft", "published", "archived");

private void validateEnumField(String fieldName, String fieldValue, String... allowedValues) throws ValidationException {
  if (!Arrays.asList(allowedValues).contains(fieldValue)) {
    throw new ValidationException(fieldName, Arrays.asList(allowedValues));

private String renderErrorResponse(String fieldName, List<String> allowedValues) {
  Map<String, Object> errorModel = new HashMap<>();
  errorModel.put("fieldName", fieldName);
  errorModel.put("allowedValues", String.join(",", allowedValues));
  return MustacheFactory

Note: This example assumes that you have already implemented a RequestBody class to represent the request body of your API endpoint. The code for the ValidationException class and the processRequest() method is omitted for brevity.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2021-06-20 11:00:00 +0000

Seen: 8 times

Last updated: Oct 05 '22