From 826bb6c070ce9becc2a7f1ac6bf0ebeaaa3a3a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Thu, 2 Apr 2020 08:04:33 +0200 Subject: [PATCH] fix: @Column supports super class inheritance for write measurements (#99) --- CHANGELOG.md | 3 ++ .../client/internal/MeasurementMapper.java | 19 +++++---- .../com/influxdb/client/WriteApiTest.java | 42 +++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d18e0d4ad2..7862d13172d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ 1. [#93](https://github.com/influxdata/influxdb-client-java/issues/93): Add addTags and addFields helper functions to Point 1. [#97](https://github.com/influxdata/influxdb-client-java/pull/97): Add the ability to specify the org and the bucket when creating the client +### Bugs +1. [#98](https://github.com/influxdata/influxdb-client-java/issues/98): @Column supports super class inheritance for write measurements + ## 1.6.0 [2020-03-13] ### Features diff --git a/client/src/main/java/com/influxdb/client/internal/MeasurementMapper.java b/client/src/main/java/com/influxdb/client/internal/MeasurementMapper.java index 40e39492169..c43279c2588 100644 --- a/client/src/main/java/com/influxdb/client/internal/MeasurementMapper.java +++ b/client/src/main/java/com/influxdb/client/internal/MeasurementMapper.java @@ -129,15 +129,20 @@ private void cacheMeasurementClass(@Nonnull final Class... measurementTypes) influxColumnAndFieldMap = initialMap; } - for (Field field : measurementType.getDeclaredFields()) { - Column colAnnotation = field.getAnnotation(Column.class); - if (colAnnotation != null) { - String name = colAnnotation.name(); - if (name.isEmpty()) { - name = field.getName(); + Class currentMeasurementType = measurementType; + while (currentMeasurementType != null) { + for (Field field : currentMeasurementType.getDeclaredFields()) { + Column colAnnotation = field.getAnnotation(Column.class); + if (colAnnotation != null) { + String name = colAnnotation.name(); + if (name.isEmpty()) { + name = field.getName(); + } + influxColumnAndFieldMap.put(name, field); } - influxColumnAndFieldMap.put(name, field); } + + currentMeasurementType = currentMeasurementType.getSuperclass(); } } } diff --git a/client/src/test/java/com/influxdb/client/WriteApiTest.java b/client/src/test/java/com/influxdb/client/WriteApiTest.java index c183824b6eb..7ec38e7bf1a 100644 --- a/client/src/test/java/com/influxdb/client/WriteApiTest.java +++ b/client/src/test/java/com/influxdb/client/WriteApiTest.java @@ -28,6 +28,8 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; +import com.influxdb.annotations.Column; +import com.influxdb.annotations.Measurement; import com.influxdb.client.domain.WritePrecision; import com.influxdb.client.internal.AbstractInfluxDBClientTest; import com.influxdb.client.write.Point; @@ -163,6 +165,34 @@ void writeMeasurement() throws InterruptedException { Assertions.assertThat(request.getRequestUrl().queryParameter("precision")).isEqualTo("ns"); } + @Test + void writeMeasurementInheritance() throws InterruptedException { + + mockServer.enqueue(new MockResponse()); + + writeApi = influxDBClient.getWriteApi(); + + Metric measurement = new Visitor(); + //noinspection CastCanBeRemovedNarrowingVariableType + ((Visitor) measurement).count = 99; + measurement.source = "metric-source"; + + // response + writeApi.writeMeasurement("b1", "org1", WritePrecision.S, measurement); + + RecordedRequest request = mockServer.takeRequest(10L, TimeUnit.SECONDS); + + // value + Assertions.assertThat(request.getBody().readUtf8()).isEqualTo("visitor,source=metric-source count=99i"); + + // organization + Assertions.assertThat(request.getRequestUrl().queryParameter("org")).isEqualTo("org1"); + // bucket + Assertions.assertThat(request.getRequestUrl().queryParameter("bucket")).isEqualTo("b1"); + // precision + Assertions.assertThat(request.getRequestUrl().queryParameter("precision")).isEqualTo("s"); + } + @Test void writeMeasurementNull() { @@ -843,4 +873,16 @@ private String getRequestBody(@Nonnull final MockWebServer server) { return recordedRequest.getBody().readUtf8(); } + public abstract class Metric { + @Column(name = "source", tag = true) + private String source; + } + + + @Measurement(name = "visitor") + public class Visitor extends Metric { + @Column(name = "count") + private long count; + } + } \ No newline at end of file