Iain
Iain

Reputation: 29

DropWizard @ValidationMethod adding a json property

I have a DropWizard representation which has a ValidationMethod. When I try to write unit tests for it though when I use the jackson ObjectMapper is is inserting an additional property to the json string which is valid{name_of_other_property}:boolean.

Which is breaking the system of having fixtures and comparing the ObjectMapped output to some fixed json files to compare. Has anyone else come across this and worked out the best way to get around it?

EDIT to add example

public class Cat {

  @NotEmpty
  private String name;

  @JsonProperty
  public String getName() {
      return name;
  }   

  @JsonProperty
  public void setName(String name) {
      this.name=name;
  }

  @ValidationMethod(message = "Not a cat name!")
  public boolean isValidCatName() {
      return (!"Fido".equals(this.name));
  }

}

I'm following the DropWizard testing documentation for fixtures: https://dropwizard.github.io/dropwizard/manual/testing.html

But I'm getting back Json from the object mapper going mapper.writeValueAsString(object) gives me json with an extra property:

{
  name:"Felix"
  validCatName:true
}

Which means the unit tests are failing. I don't notice this extra field existing when using the representations as arguments for my Resources though.

Thanks, Iain

EDIT: Solution is to add the @JsonIgnore annotation to the validation methods.

Upvotes: 1

Views: 1554

Answers (2)

Vikash Tiwari
Vikash Tiwari

Reputation: 113

There are two ways to solve it.

  • Explicitly specifying mapper to Ignore such properties

    @ValidationMethod(message = "Not a cat name!") @JsonIgnore public boolean isValidCatName() { return (!"Fido".equals(this.name)); }

This will return the desired result.

  • There are some default properties of Mapper which can help autodetecting, you can disable such properties

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig()
        .getDefaultVisibilityChecker()
        .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
        .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
        .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
        .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
        .withFieldVisibility(JsonAutoDetect.Visibility.NONE));
    

Both will yield the result.

Cheers!

Upvotes: 1

user3280180
user3280180

Reputation: 1403

I had some similar problems. I dont know if I am right, but for me it seems that there is a naming problem for the validation method. The only way for me to get it working was to rename validation method. In your case "isName" instead "isValidVatName". Perhaps you can try it and give answer, if this fixed your problem. Just name your validation methods this way: "is" + {name of your json property; in your case 'name'}.

For me it was not important, how the json property was written (upper case or not). So even the property 'catName' can have this validation method names:

  • isCatName or
  • isCatname or
  • iscatName or
  • iscatname or ...

It is just important to use the format: "is" + propertyName. But I'm not sure if this is always true. Perhaps there is a difference between camelCase or snakecase (= not tested it)

Fixed code example:

public class Cat {

  @NotEmpty
  private String name;

  @JsonProperty
  public String getName() {
      return name;
  }   

  @JsonProperty
  public void setName(String name) {
      this.name=name;
  }

  @ValidationMethod(message = "Not a cat name!")
  public boolean isName() {
      return (!"Fido".equals(this.name));
  }

}

Upvotes: 1

Related Questions