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

OneOf validation always succeeds #23

Open
jwilmoth-nc opened this issue Jan 19, 2021 · 0 comments
Open

OneOf validation always succeeds #23

jwilmoth-nc opened this issue Jan 19, 2021 · 0 comments

Comments

@jwilmoth-nc
Copy link

The use of oneOf in a schema results in schema validation never failing despite json docs not matching schema. Here's a simplified example:

Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "OneOf Example",
  "type": "object",
  "properties": {
    "address": {
      "type": "string",
      "oneOf": [
        { "format": "email" },
        { "format": "hostname" }
      ]
    }
  },
  "additionalProperties": false
}

Junit 5 Test

The shouldDetectValidJsonWithOneOfEmail and shouldDetectValidJsonWithOneOfHost test pass, but the shouldDetectInvalidJson test doesn't fail.

class OneOfSchemaValidationTest {

  ObjectMapper objectMapper = new ObjectMapper();
  MedeiaJacksonApi api = new MedeiaJacksonApi();

  SchemaValidator SCHEMA_VALIDATOR = loadSchema();

  @Test
  void shouldDetectValidJsonWithOneOfEmail() throws IOException {
    String resource = "{ \"address\": \"test@test.com\" }";
    JsonParser unvalidatedParser = objectMapper.getFactory().createParser(resource);
    JsonParser validatedParser = api.decorateJsonParser(SCHEMA_VALIDATOR, unvalidatedParser);
    OneOfExample oneOfExample = objectMapper.readValue(validatedParser, OneOfExample.class);
    assertEquals("test@test.com", oneOfExample.getAddress());
  }

  @Test
  void shouldDetectValidJsonWithOneOfHost() throws IOException {
    String resource = "{ \"address\": \"127.0.0.1\" }";
    JsonParser unvalidatedParser = objectMapper.getFactory().createParser(resource);
    JsonParser validatedParser = api.decorateJsonParser(SCHEMA_VALIDATOR, unvalidatedParser);
    OneOfExample oneOfExample = objectMapper.readValue(validatedParser, OneOfExample.class);
    assertEquals("127.0.0.1", oneOfExample.getAddress());
  }

  @Test
  void shouldDetectInvalidJson() throws IOException {
    String resource = "{ \"address\": \"*******\" }";
    JsonParser unvalidatedParser = objectMapper.getFactory().createParser(resource);
    JsonParser validatedParser = api.decorateJsonParser(SCHEMA_VALIDATOR, unvalidatedParser);
    assertThrows(ValidationFailedException.class,
                 () -> objectMapper.readValue(validatedParser, OneOfExample.class));
  }

  private SchemaValidator loadSchema() {
    SchemaSource source = new UrlSchemaSource(
        getClass().getResource("/patient/SimpleOneOfExample.schema.json"));
    return api.loadSchema(source);
  }

}
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