Decathlon / tzatziki

Decathlon library to ease and promote Test Driven Development of Java microservices!
Apache License 2.0
58 stars 27 forks source link

Impossible de parse date type from avro schema #325

Open Z21GELHA opened 2 days ago

Z21GELHA commented 2 days ago

Hello,

Currently in the KafkaSteps class, when we try to map a date like 2024-09-16, we encounter some troubles because of the avro type.

In fact, when we have a date, we have this kind of Avro schema: { "default": null, "name": "my_date", "type": [ "null", { "logicalType": "date", "type": "int" } ] }

But because, the type is considered as an UNION, the parseAvro method doesn't transform the value in an Integer.

What I suggest is to modify the method like this to cover this situation:

@Nullable
    private Object parseAvro(String value, Schema valueSchema) {
        return switch (valueSchema.getType()) {
            case INT -> {
                if("date".equals(valueSchema.getProp("logicalType"))) {
                    yield (int) LocalDate.parse(value, DateTimeFormatter.ISO_LOCAL_DATE).toEpochDay();
                } else {
                    yield Integer.parseInt(value);
                }
            }
            case LONG -> Long.parseLong(value);
            case FLOAT -> Float.parseFloat(value);
            case DOUBLE -> Double.parseDouble(value);
            case BOOLEAN -> Boolean.parseBoolean(value);
            default -> value;
        };
    }
laujacq commented 1 day ago

Hello,

Thank you for bringing this up.

The behavior you’re describing with Avro’s logicalType for dates makes sense for it to be managed

However, in this case we also need to manage the other subtypes in order to be more inclusive, such as : • time-millis • time-micros • timestamp-millis • timestamp-micros • decimal

However, given our current priorities and other ongoing tasks, we will need to schedule this for future work. We will keep you updated in this issue.

In the meantime, feel free to fork the project and contribute 👍 !