Here is my avro schema.

{
    "namespace": "example.avro",
    "type": "record",
    "name": "User",
    "fields": [
        {
            "name": "name",
            "type": "string"
        },
        {
            "name": "favorite_number",
            "type": ["int", "null"]
        },
        {
            "name": "favorite_color",
            "type": ["string", "null"]
        }
    ]
}

And my sample Java program using Avro v1.11.3 to generate a random JSON message.

package org.example;

import org.apache.avro.Schema;
import org.apache.avro.util.RandomData;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

public class MyTest {
    public static void main(String [] args) throws IOException {
        MyTest me = new MyTest();
        ClassLoader classLoader = me.getClass().getClassLoader();
        InputStream is = classLoader.getResourceAsStream("person.avsc");
        Schema schema = new Schema.Parser().parse(is);
        Iterator<Object> it = new RandomData(schema, 1).iterator();
        System.out.println(it.next());
    }
}

And the generated JSON message is below. It is clearly an invalid JSON when validating against the schema.

{
    "name": "rymhxcnbcyohbtjmouegufvchxh",
    "favorite_number": 4211,
    "favorite_color": "red"
}

The correct and expected JSON message should contain UNION types like this

{
    "name": "rymhxcnbcyohbtjmouegufvchxh",
    "favorite_number": {
        "int": 4211
    },
    "favorite_color": {
        "string": "red"
    }
}

So, the question is how to generate a JSON sample including UNION types as the correct output above.

1

There are 1 answers

0
Emptyless On

I've faced the same issue, notice that it is the UNION case of the generate method. If adapted, to:

case UNION:
    List<Schema> types = schema.getTypes();
    map = (Map<Object, Object>) genericData.newMap(null, 1);
    Schema type = types.get(random.nextInt(types.size()));
    map.put(type.getName(), generate(type, random, d));
    return map;

It will return the correct JSON payload. I'll open a PR to see if it is accepted.