How can I generate java record objects for java schemas with the anyOf
field?
I have the following schemas:
animals.json
{
"type": "object",
"properties": {
"animals": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "mammale.json"},
{"$ref": "bird.json"}
],
"minContains": 1,
"uniqueItems": true
}
}
}
}
mammal.json
{
"type": "object",
"properties": {
"mammalType": {"type": "string"},
"furColor": {"type": "string"}
}
}
bird.json
{
"type": "string"
}
I want to output a json that looks like the following:
{
"animals": [
{
"mammalType": "dog",
"furColour": "brown"
},
"someBirdsName"
]
}
Is there a recommended way to represent the 'anyOf' structure in the java records (preferably with Jackson annotation)?
I found a solution when using POJOs, where they use an interface: Json schema with anyOf fields to POJO
This looks like a good solution but how do I us an arbitrary string that doesnt implements the interface?