Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3138: Reflect data get fields support not order by property name #2581

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

horizonzy
Copy link
Member

In some cases, we hope that the reflect fields order maintain the original order.

class Pojo {
String f2;
String f1;
}

At client, it create Pojo and serialize it using JSON format.
The json:

{"f2":"a", "f1":"b"}

And then, create the schema using refelct data, the fields will be ordered.

The schema:

[{"name":"f1", "type":"string"},
{"name":"f2", "type":"string"}]

Then push the payload and schema to server, the server store the json payload as k,v format, and store the schema to the schema registry.

At another client, it want to read the data. Then the server read the k,v format data, then decode it according the schema from schema registry. But the fields order is not match the origin. So the decode json will be like

{"f1":"b", "f2":"a"}

I want to make the json fields order maintain the original order, when creating the schema using ReflectData, not order the fields.

@github-actions github-actions bot added the Java Pull Requests for Java binding label Nov 9, 2023
@KalleOlaviNiemitalo
Copy link
Contributor

I wonder if this should be an annotation on the class, instead. A Java code generator could then automatically add the annotation if needed.

@horizonzy
Copy link
Member Author

@nielsbasjes cc

@RyanSkraba RyanSkraba changed the title Reflect data get fields support not order by property name. AVRO-3138: Reflect data get fields support not order by property name Nov 9, 2023
@RyanSkraba
Copy link
Contributor

Hello! I've added AVRO-3138 as the relevant JIRA for this feature. There's a bit of discussion about why we sort the field names. It looks like you're implementing "A JVM system property ... triggers the old behaviour", but there are other possibilities for specifying this.

In this case, it's possible for your test to fail depending on the JVM being used to run it.

@horizonzy
Copy link
Member Author

Hello! I've added AVRO-3138 as the relevant JIRA for this feature. There's a bit of discussion about why we sort the field names. It looks like you're implementing "A JVM system property ... triggers the old behaviour", but there are other possibilities for specifying this.

In this case, it's possible for your test to fail depending on the JVM being used to run it.

I have gone through the context. In most projects, they will use Java Reflect to get the fields without order, which means if the user uses Avro and other projects together, and the data is related with the avro and other projects, it may cause the problem that I meet.

@horizonzy
Copy link
Member Author

I would like to supply a config to control it instead of jvm properties.

@horizonzy
Copy link
Member Author

@RyanSkraba Use a final property to control the behavior.

@cb-kapilmehta
Copy link

@opwvhk , @horizonzy , Any tentative date to be merged this PR. This sorting part completely blocked multiple projects to upgrade the avro version. Any help will be appreciated. TIA!

Comment on lines +98 to +101
Field[] declaredFields = Meta.class.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
assertEquals(fields.get(i).name(), declaredFields[i].getName());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Pull Requests for Java binding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants