Set default values for object array in json schema

171 views Asked by At

How can we set default values for an array of objects in JSON schema definition?

The following shows the specific section of schema. The default data provided is not helping.

 "children": {
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "fname": {
        "type": "string"
      },
      "lname": {
        "type": "string"
      }
    }
  },
  "default": [{
    "fname": "x",
    "lname": "ray"
  }]
}

I am later converting this schema to java object using jsonschema2pojo maven plugin. So I am expecting the same default data to be available in the generated classes. The above mentioned approach works perfectly for sting, number etc.. but not for object array. Any help is highly appreciated.

1

There are 1 answers

4
Jeremy Fiel On

the default keyword does not provide any validation assertion. It's merely an annotation. With that said, you can add default to the schema property definition. There's no guarantee it will be used in the pojo generation. that's dependent on the implementation used.

"children": {
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "fname": {
        "type": "string",
        "default": "x"
      },
      "lname": {
        "type": "string",
        "default": "ray",
        "examples": [ "one", "two"]
      }
    }
  }
}

The examples keyword is a place to provide an array of examples that validate against the schema. This isn’t used for validation, but may help with explaining the effect and purpose of the schema to a reader. Each entry should validate against the schema in which it resides, but that isn’t strictly required. There is no need to duplicate the default value in the examples array, since default will be treated as another example.