mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
232 stars 75 forks source link

anyOf not working with array and nullableJsonSchemaDraft4 #161

Closed leccyril closed 2 years ago

leccyril commented 2 years ago

i use your code to create jsonSchema to then validate another json but it doe'snt work any idea ?

` ObjectMapper objectMapper = new ObjectMapper(); //JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper); JsonSchemaConfig config = JsonSchemaConfig.nullableJsonSchemaDraft4().withJsonSchemaDraft(JsonSchemaDraft.DRAFT_04); JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(objectMapper, config);

        JsonNode jsonSchema = jsonSchemaGenerator.generateJsonSchema(SimpleTestWrapper.class);

        String jsonSchemaAsString = objectMapper.writeValueAsString(jsonSchema);

        System.out.println(jsonSchemaAsString);

        Schema schema = SchemaLoader.load(new JSONObject(jsonSchemaAsString));
        schema.validate(new JSONObject(json));

        System.out.println("ok");`

but i causes issues:

org.everit.json.schema.ValidationException: #/simpleTests: #: 0 subschemas matched instead of one org.everit.json.schema.InternalValidationException: #/simpleTests: expected: null, found: JSONArray org.everit.json.schema.InternalValidationException: #/simpleTests/0/attr1: expected type: String, found: Null

schema generated :

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Simple Test Wrapper","type":"object","additionalProperties":false,"properties":{"simpleTests":{"oneOf":[{"type":"null","title":"Not included"},{"type":"array","items":{"$ref":"#/definitions/SimpleTest"}}]}},"definitions":{"SimpleTest":{"type":"object","additionalProperties":false,"properties":{"attr1":{"type":"string"},"attr2":{"oneOf":[{"type":"null","title":"Not included"},{"type":"string"}]},"chiffre":{"oneOf":[{"type":"null","title":"Not included"},{"type":"integer"}]}},"required":["attr1"]}}}

what i want validate

{"simpleTests":[{"attr1":null,"attr2":null,"chiffre":12},{"attr1":"attr1","attr2":"test2"}]}