Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serialization of \n, \r and \t to Line Protocol, = is valid sign for measurement name #129

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, `=` is valid sign for measurement name

### 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
25 changes: 22 additions & 3 deletions client/src/main/java/com/influxdb/client/write/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public String toLineProtocol(@Nullable final PointSettings pointSettings) {

StringBuilder sb = new StringBuilder();

escapeKey(sb, name);
escapeKey(sb, name, false);
appendTags(sb, pointSettings);
boolean appendedFields = appendFields(sb);
if (!appendedFields) {
Expand Down Expand Up @@ -421,15 +421,34 @@ private void appendTime(@Nonnull final StringBuilder sb) {
}

private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key) {
escapeKey(sb, key, true);
}

private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key, final boolean escapeEqual) {
for (int i = 0; i < key.length(); i++) {
switch (key.charAt(i)) {
case '\n':
sb.append("\\n");
continue;
case '\r':
sb.append("\\r");
continue;
case '\t':
sb.append("\\t");
continue;
case ' ':
case ',':
case '=':
sb.append('\\');
break;
case '=':
if (escapeEqual) {
sb.append('\\');
}
break;
default:
sb.append(key.charAt(i));
}

sb.append(key.charAt(i));
}
}

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
26 changes: 25 additions & 1 deletion client/src/test/java/com/influxdb/client/write/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void measurementEscape() {
.addTag("", "warn")
.addField("level", 2);

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

point = Point.measurement("h2,o")
.addTag("location", "europe")
Expand Down Expand Up @@ -86,6 +86,30 @@ 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\rreturn")
.addTag("t\tab", "t\tab")
.addField("level", 2);

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

@Test
public void equalSignEscaping() {

Point point = Point.measurement("h=2o")
.addTag("l=ocation", "e=urope")
.addField("l=evel", 2);

Assertions.assertThat(point.toLineProtocol())
.isEqualTo("h=2o,l\\=ocation=e\\=urope l\\=evel=2i");
}

@Test
void fieldTypes() {

Expand Down