Swagger-codegen: [Java][Spring] Add support for custom validation messages in Beanvalidation annotations

Created on 15 May 2017  路  10Comments  路  Source: swagger-api/swagger-codegen

The PR #4600 added support for beanvalidation annotations, which work fine, but generated annotations also support a message property to specify a custom error message if the validation for that annotation fails.
This can look like this:

@ApiModelProperty(example = "VGVzdA==", value = "")
@Pattern(regexp="^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$", message = "The provided String is not Base64 encoded")
public String getFoo() {
    return foo;
}

Are there plans to add support for such a message? It might be possible to add it to the declaration file like:

foo:
    type: string
    pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$"
        message: "The provided String is not Base64 encoded"
    example: "VGVzdA=="

I guess this feature must be added in the beanValidationCore.mustache template (in swagger-codegen/src/main/java/resources/), but I'm not proficient enough with that template language to do that myself.

(I've tested the release version of 2.2.0 and the current master version, but both don't support these message properties)

All 10 comments

+1

+2

+1

+1 very important, our clients are getting a ton of regex instead of a readable error message

Hello @TomSoflow321 @cartick @shexbeer @gmgk93 @unphon

i can implement this using vendor extensions.

foo:
    type: string
    pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2} ..."
    x-message: "The provided String is not Base64 encoded"
    example: "VGVzdA=="

I would update templates to read the x-message field. Please let me know what you think about that.

@HugoMario,

While I do think that the use of vendor extensions might work for this, your suggestion might be problematic when multiple constraints are required (e.g. minLength + maxLength). It might also be hard to relate this to 'required' constraints, since these are separated from the field declaration.

One option that I see is to still use vendor extensions, but have one per constraint:

foo:
    type: string
    pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2} ..."
    x-pattern-message: "The provided String is not Base64 encoded"
    x-required-message: "The provided String cannot be null"
    example: "VGVzdA=="
required:
    - foo

What are your thoughts on this?

i like that, i can do it that way. thanks

+1

You might want to support localisation and might want to consider message keys as well.

hello guys, i'm really really sorry for delay. i just merged a PR making possible the custom message.
Here is an example about how it works:

swagger: '2.0'
info:
  version: '1.0.0'
  title: 'simple'
  description: 'it is simple'
paths:
  /foo:
    post:
      parameters:
        - in: body
          name: body
          schema:
            $ref: '#/definitions/BodyParam'
      responses:
        200:
          description: OK
          schema:
            $ref: '#/definitions/OKResponse'
        400:
          description: Error
          schema:
            $ref: '#/definitions/ErrorResponse'

definitions:
  BodyParam:
    type: object
    properties:
      foo:
        type: string
        pattern: "^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$"
        x-pattern-message: "extension pattern message."
        message: "The provided String is not Base64 encoded"
  OKResponse:
    type: object
    properties:
      bar:
        type: string
  ErrorResponse:
    type: object
    properties:
      error:
        type: integer
      message:
        type: string
    required:
      - error
      - message

The custom message for patter will have this output on BodyParam model:

@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-01-25T11:05:38.641-05:00")

public class BodyParam   {
  @JsonProperty("foo")
  private String foo = null;

  public BodyParam foo(String foo) {
    this.foo = foo;
    return this;
  }

  /**
   * Get foo
   * @return foo
  **/
  @ApiModelProperty(value = "")

@Pattern(regexp="^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$", message="extension pattern message.") 
  public String getFoo() {
    return foo;
  }

  public void setFoo(String foo) {
    this.foo = foo;
  }
...
Was this page helpful?
0 / 5 - 0 ratings