I need to generate an AVRO schema from an existing Java Model. I then want to use that schema to generate Java objects so I can replace my existing model with the generated one, making the schema the source of truth.
I am having a problem which I will try to illustrate with this example.
I have a basic Java class:
public class Car {
private String price;
private String engine;
public Car(String price, String engine) {
this.price = price;
this.engine = engine;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
}
I want to generate an avro schema which represents this class.
public static void main(String[] args) throws JsonMappingException {
System.out.println(generateAvroSchema());
}
public static String generateAvroSchema() throws JsonMappingException {
ObjectMapper mapper = new ObjectMapper(new AvroFactory());
AvroSchemaGenerator gen = new AvroSchemaGenerator();
mapper.acceptJsonFormatVisitor(Car.class, gen);
Schema schema = gen.getGeneratedSchema().getAvroSchema();
return schema.toString(true);
}
The result of this is:
{
"type" : "record",
"name" : "Car",
"namespace" : "app",
"fields" : [ {
"name" : "engine",
"type" : [ "null", "string" ]
}, {
"name" : "price",
"type" : [ "null", "string" ]
} ]
}
When the schema is generated the object attributes are ordered alphabetically. If I was to use this schema to generate a new java model, the constructor argument ordering would be different from the source.
My question is, is there any configuration I can use when generating the schema to preserve attribute ordering and not default to alphabetical?
Thanks