0

I have a requirement to autogerate Json schema with dependencies ( one attribute is required or not based on other attribute value ).

My exact situation has been asked and answered in jsonSchema attribute conditionally required.

However, my requirement doesn't allow manual changes to schema directly. I'm creating schema using POJO's with Jackson annotations. In a nutshell, I need Jackson annotation to achieve this.

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "if": {
     "properties": {
       "foo": { "const": "bar" }
     },
   "required": ["foo"]
  },
  "then": { "required": ["bar"] }
}

(or)

{
  "type": "object",
  "properties": {
    "foo": { "enum": ["bar", "baz"] },
    "bar": { "type": "string" },
    "baz": { "type": "string" }
  },
  "anyOf": [
    {
      "properties": {
        "foo": { "const": "bar" }
      },
      "required": ["bar"]
    },
    {
      "properties": {
        "foo": { "const": "baz" }
      },
      "required": ["baz"]
    }
  ]
}

I have experimented with Jackson annotations like @JsonProperty, @JsonFilter, @JsonSerialize etc. Please do nudge me in right direction.

Thanks.

iAmOren
  • 2,760
  • 2
  • 11
  • 23
  • If you can express this in two different classes, `@JsonSubTypes` and `@JsonTupeInfo` might be the annotations you’re looking for. – Carsten Jul 09 '20 at 04:28
  • @Carsten Apologies for delayed response. do you mean with @JsonTypeInfo and @JsonSubTypes? I tried this, this converses into anyOf() in schema. which is not I'm looking for. here is the code. `@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, property = "Type") @JsonSubTypes({ @JsonSubTypes.Type(name="Renewal", value = Renewal.class), @JsonSubTypes.Type(name="New", value = New.class), @JsonSubTypes.Type(name="Amendment", value = Amendment.class) })` – madhu venkatesh Jul 21 '20 at 09:33
  • It won’t be the `“anyOf“` as per your example above, true. But it should nonetheless allow you to describe the JSON structure you want. – Carsten Jul 21 '20 at 09:36
  • @Carsten I'm sorry. I meant to say it will convert to `oneOf`. here is the generated schema ( version-04). `"oneOf" : [ { "$ref" : "#/definitions/Renewal" }, { "$ref" : "#/definitions/New" }, { "$ref" : "#/definitions/Amendment" } ],` I'm using this schema for validation. based on value of 'Type', I want my Json to be validated against one of the references and throw error message if any. but here if validation fails for first, it moves on next one. what am I missing? – madhu venkatesh Jul 21 '20 at 09:52
  • Presumably each of these referenced schemas contains the „Type“ property with the corresponding fixed/`enum` value, i.e. there can only ever be one match. In that regard `oneOf` and `anyOf` should behave similarly. `anyOf` will just stop once it found a match while `oneOf` makes sure that no other match is present. If your concern is not performance, then that should make no difference. – Carsten Jul 21 '20 at 10:01

0 Answers0