mardi 10 janvier 2023

Avro Schema is being generated in wrong field order (alphabetical)

I have a class the I need to converto to an avro schema:

public class MonitorStateSchema {
    private MetaResponse c;
    private Header a;
    private MonitorStateEnvelope b;

    public MonitorStateSchema(MetaResponse c, Header a, MonitorStateEnvelope b) {
        this.c = c;
        this.a = a;
        this.b = b;
    }
}

I use a generic method to get the schema from it

public static <D> void getFromAvro(Class<D> schemaType) {
    Schema schema = ReflectData.get().getSchema(schemaType);

   // other stuff
}

After doing it, I got a different order in the result than the expected:

EXPECTED:

{
  "type": "record",
  "name": "MonitorSchema",
  "namespace": "mypackage.monitor",
  "fields": [
    {
      "name": "c",
      "type": {
        "type": "record",
        "name": "MetaResponse",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "uuid",
            "type": "string"
          },
          {
            "name": "code",
            "type": "int"
          },
          {
            "name": "message",
            "type": "string"
          }
        ]
      }
    },
    {
      "name": "a",
      "type": {
        "type": "record",
        "name": "Header",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "apiKey",
            "type": "string"
          },
          {
            "name": "signature",
            "type": "string"
          },
          {
            "name": "nonce",
            "type": "int"
          }
        ]
      }
    },
    {
      "name": "b",
      "type": {
        "type": "record",
        "name": "MonitorEnvelope",
        "fields": [
          {
            "name": "fields",
            "type": {
              "type": "array",
              "items": {
                "type": "record",
                "name": "Field",
                "fields": [
                  {
                    "name": "name",
                    "type": "string"
                  },
                  {
                    "name": "value",
                    "type": "string"
                  }
                ]
              },
              "java-class": "[Lmypackage.monitor.Field;"
            }
          }
        ]
      }
    }
  ]
}

ACTUAL RESULT:

{
  "type": "record",
  "name": "MonitorStateSchema",
  "namespace": "mypackage.monitor",
  "fields": [
    {
      "name": "a",
      "type": {
        "type": "record",
        "name": "Header",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "apiKey",
            "type": "string"
          },
          {
            "name": "nonce",
            "type": "int"
          },
          {
            "name": "signature",
            "type": "string"
          }
        ]
      }
    },
    {
      "name": "b",
      "type": {
        "type": "record",
        "name": "MonitorStateEnvelope",
        "fields": [
          {
            "name": "fields",
            "type": {
              "type": "array",
              "items": {
                "type": "record",
                "name": "Field",
                "namespace": "mypackage.monitor",
                "fields": [
                  {
                    "name": "name",
                    "type": "string"
                  },
                  {
                    "name": "value",
                    "type": "string"
                  }
                ]
              },
              "java-class": "[Lmypackage.monitor.Field;"
            }
          }
        ]
      }
    },
    {
      "name": "c",
      "type": {
        "type": "record",
        "name": "MetaResponse",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "code",
            "type": "int"
          },
          {
            "name": "message",
            "type": "string"
          },
          {
            "name": "uuid",
            "type": "string"
          }
        ]
      }
    }
  ]
}

Seems that it is ordering by alphabetical order on the name of the field and it is breaking the application when deserializing the byte array. Is there any reason for this to happen?





Aucun commentaire:

Enregistrer un commentaire