Skip to content

Commit eb90ab5

Browse files
authored
fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name (#129)
* fix: serialization of `\n`, `\r` and `\t` to Line Protocol * fix: `=` is valid sign for measurement name
1 parent 73de99d commit eb90ab5

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.10.0 [unreleased]
22

3+
### Bug Fixes
4+
1. [#129](https://github.com/influxdata/influxdb-client-java/pull/129): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol, `=` is valid sign for measurement name
5+
36
### Dependencies
47

58
1. [#124](https://github.com/influxdata/influxdb-client-java/pull/124): Update dependencies: akka: 2.6.6, commons-io: 2.7, spring: 5.2.7.RELEASE, retrofit: 2.9.0, okhttp3: 4.7.2

client/src/main/java/com/influxdb/client/write/Point.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public String toLineProtocol(@Nullable final PointSettings pointSettings) {
303303

304304
StringBuilder sb = new StringBuilder();
305305

306-
escapeKey(sb, name);
306+
escapeKey(sb, name, false);
307307
appendTags(sb, pointSettings);
308308
boolean appendedFields = appendFields(sb);
309309
if (!appendedFields) {
@@ -421,15 +421,34 @@ private void appendTime(@Nonnull final StringBuilder sb) {
421421
}
422422

423423
private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key) {
424+
escapeKey(sb, key, true);
425+
}
426+
427+
private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key, final boolean escapeEqual) {
424428
for (int i = 0; i < key.length(); i++) {
425429
switch (key.charAt(i)) {
430+
case '\n':
431+
sb.append("\\n");
432+
continue;
433+
case '\r':
434+
sb.append("\\r");
435+
continue;
436+
case '\t':
437+
sb.append("\\t");
438+
continue;
426439
case ' ':
427440
case ',':
428-
case '=':
429441
sb.append('\\');
442+
break;
443+
case '=':
444+
if (escapeEqual) {
445+
sb.append('\\');
446+
}
447+
break;
430448
default:
431-
sb.append(key.charAt(i));
432449
}
450+
451+
sb.append(key.charAt(i));
433452
}
434453
}
435454

client/src/test/java/com/influxdb/client/internal/MeasurementMapperTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ void instantOver2226() {
101101
Assertions.assertThat(mapper.toPoint(pojo, WritePrecision.NS).toLineProtocol()).isEqualTo("pojo,tag=value value=\"15\" 43658216763800123456");
102102
}
103103

104+
@Test
105+
public void escapingTags() {
106+
107+
Pojo pojo = new Pojo();
108+
pojo.tag = "mad\nrid";
109+
pojo.value = 5;
110+
111+
String lineProtocol = mapper.toPoint(pojo, WritePrecision.S).toLineProtocol();
112+
Assertions.assertThat(lineProtocol).isEqualTo("pojo,tag=mad\\nrid value=\"5\"");
113+
}
114+
104115
@Measurement(name = "pojo")
105116
private static class Pojo {
106117

client/src/test/java/com/influxdb/client/write/PointTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void measurementEscape() {
5454
.addTag("", "warn")
5555
.addField("level", 2);
5656

57-
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2\\=o,location=europe level=2i");
57+
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2=o,location=europe level=2i");
5858

5959
point = Point.measurement("h2,o")
6060
.addTag("location", "europe")
@@ -86,6 +86,30 @@ void tagEmptyValue() {
8686
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
8787
}
8888

89+
@Test
90+
public void tagEscapingKeyAndValue() {
91+
92+
Point point = Point.measurement("h\n2\ro\t_data")
93+
.addTag("new\nline", "new\nline")
94+
.addTag("carriage\rreturn", "carriage\rreturn")
95+
.addTag("t\tab", "t\tab")
96+
.addField("level", 2);
97+
98+
Assertions.assertThat(point.toLineProtocol())
99+
.isEqualTo("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\rreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i");
100+
}
101+
102+
@Test
103+
public void equalSignEscaping() {
104+
105+
Point point = Point.measurement("h=2o")
106+
.addTag("l=ocation", "e=urope")
107+
.addField("l=evel", 2);
108+
109+
Assertions.assertThat(point.toLineProtocol())
110+
.isEqualTo("h=2o,l\\=ocation=e\\=urope l\\=evel=2i");
111+
}
112+
89113
@Test
90114
void fieldTypes() {
91115

0 commit comments

Comments
 (0)