joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.22k stars 1.66k forks source link

feat: support declared type as array for jsonSchema #1590

Open yzaouzaou opened 6 months ago

yzaouzaou commented 6 months ago

When using Jsr303 annotation, to get our objects fileds checked for constraints, we have to annotate them with @Valid, this annotation is added on fileds that aren't premetives (or objects représenting premitives like : Integer) to recursively check constraints. Until now, to annotate a field with this annotation, we check for it's type weither it's "object" or "array". Since the specification of jsonSchema seems to accept a string or an array of strings, when we use an array of strings the annotation @Valid is not added to the field, thus, the field is not checked for constraints.

for example, in the current version, if we use this jsonShema (which is valid according to specification of jsonschema)

{
  "type": "object",
  "properties": {
    "myFiled": {
      "type": ["object", "null"],
      "properties": {
        "childprimitivefield": {
          "type": "string",
          "maxLength": 5
        }
      }
    }
  }
}

The filed myField will not have the annotation @Valid and it's objects won't be validated.

Some links :

PS :

For tests, i have just changed existing tests, please let me know if it's better to duplicate existing tests to cover this new feature even if it's the same as existing one 😃.