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

@JsonTypeInfo Jacson annotation disables validation? #11

Open
stanislawosinski opened this issue May 10, 2021 · 0 comments
Open

@JsonTypeInfo Jacson annotation disables validation? #11

stanislawosinski opened this issue May 10, 2021 · 0 comments

Comments

@stanislawosinski
Copy link

Excellent library! I was hoping to use it to validate polymorphic JSONs implemented using Jackson's @JsonTypeInfo annotation. Unfortunately, the use of this annotation appears to turn off the validation on the polymorphic object.

I include a quick reproduction test below. If you remove the @JsonTypeInfo annotation, validation will be performed as expected.

Is there any workaround I could use to make jackson-bean-validation work with @JsonTypeInfo?

public class ValidatorTest {
  @JsonValidated
  public static class Impl implements Interface {
    @JsonProperty @NotEmpty public String s;
  }

  @JsonValidated
  @JsonTypeInfo(use = Id.NAME, property = "type")
  @JsonSubTypes({@JsonSubTypes.Type(name = "default", value = Impl.class)})
  public interface Interface {}

  @Test
  public void validation() throws JsonProcessingException {
    final ObjectMapper om = new ObjectMapper();
    final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    final BeanValidationModule module = new BeanValidationModule(factory);
    om.registerModule(module);

    final Interface invalid = new Impl();
    final String invalidJson = om.writeValueAsString(invalid);

    try {
      om.readValue(invalidJson, Impl.class);
    } catch (ConstraintViolationException e) {
      return;
    }
    Assertions.fail("Expecting ConstraintViolationException");
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant