javajsonschemajsonschema2pojo

Plugin jsonschema2pojo: Properties are shown as required but they should be optional


i have a personschema which has e.g. those two fields:

    "name": {
      "title": "Last name",
      "type": "string",
      "minLength": 1,
      "isRequired" : true
    }
    "birthday": {
      "title": "Date of birth",
      "$ref": "resource:schema/general/dateSchema.json",
      "isRequired" : true
    },
    "birthCountry": {
      "$ref": "resource:schema/general/countrySchema.json",
      "title": "Country of birth",
      "isRequired" : true
    }

Then I have another schema which extends the person schema. In this schema, I want those two properties (birthCountry and birthday) to be optional and only the name to be mandatory. I've tried it like that:

{
  "$schema": "http://json-schema.org/draft/2019-09/schema",
  "title": "Other Schema",
  "type": "object",
  "javaType": "..json_schema_model.dto.OtherSchema",
  "description": "Other Schema Description",
  "extendsJavaClass": "..json_schema_model.dto.PersonSchema",
  "allOf": [{
    "required": [
      "name"
    ],
    "$ref": "resource:schema/general/personSchema.json"
  }

But unfortunately, in the API-Docs they are still remarked as mandatory.


Solution

  • The issue was that properties can only be added in child classes and not modified, so I rearranged the class structure to modify the decision whether a value is mandatory will only be decided in the child classes themselves. Also please note, that sibling values alongsid $refs are ignored. To add properties to a $ref, you need to wrap it into an allOf.