Skip to content

Commit

Permalink
fix: serialization of \n, \r and \t to Line Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Jun 25, 2020
1 parent 73de99d commit baf4008
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.10.0 [unreleased]

### Bug Fixes
1. [#129](https://github.com/influxdata/influxdb-client-java/pull/129): Fixed serialization of `\n`, `\r` and `\t` to Line Protocol

### Dependencies

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
Expand Down
9 changes: 9 additions & 0 deletions client/src/main/java/com/influxdb/client/write/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,15 @@ private void appendTime(@Nonnull final StringBuilder sb) {
private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key) {
for (int i = 0; i < key.length(); i++) {
switch (key.charAt(i)) {
case '\n':
sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '\t':
sb.append("\\t");
break;
case ' ':
case ',':
case '=':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ void instantOver2226() {
Assertions.assertThat(mapper.toPoint(pojo, WritePrecision.NS).toLineProtocol()).isEqualTo("pojo,tag=value value=\"15\" 43658216763800123456");
}

@Test
public void escapingTags() {

Pojo pojo = new Pojo();
pojo.tag = "mad\nrid";
pojo.value = 5;

String lineProtocol = mapper.toPoint(pojo, WritePrecision.S).toLineProtocol();
Assertions.assertThat(lineProtocol).isEqualTo("pojo,tag=mad\\nrid value=\"5\"");
}

@Measurement(name = "pojo")
private static class Pojo {

Expand Down
14 changes: 14 additions & 0 deletions client/src/test/java/com/influxdb/client/write/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ void tagEmptyValue() {
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}

@Test
public void tagEscapingKeyAndValue() {

Point point = Point.measurement("h\n2\ro\t_data")
.addTag("new\nline", "new\nline")
.addTag("carriage\rreturn", "carriage\nreturn")
.addTag("carriage\rreturn", "carriage\nreturn")
.addTag("t\tab", "t\tab")
.addField("level", 2);

Assertions.assertThat(point.toLineProtocol())
.isEqualTo("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\nreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i");
}

@Test
void fieldTypes() {

Expand Down

0 comments on commit baf4008

Please sign in to comment.