Skip to content

Commit

Permalink
fix: fixing double quote escape
Browse files Browse the repository at this point in the history
  • Loading branch information
rhajek committed Jun 9, 2022
1 parent 7a12bff commit 772c36f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public final class FunctionsParameters {

private Map<String, Property> properties = new LinkedHashMap<>();

public static String escapeDoubleQuotes(final String val) {
return val.replace("\"", "\\\"");
}

private FunctionsParameters() {
}

Expand Down Expand Up @@ -86,7 +90,7 @@ public static String serializeValue(@Nonnull final Object value) {
}

serializedValue = collection.stream()
.map(host -> "\"" + host + "\"")
.map(host -> "\"" + escapeDoubleQuotes(host.toString()) + "\"")
.collect(Collectors.joining(", ", "[", "]"));
}

Expand All @@ -97,7 +101,7 @@ public static String serializeValue(@Nonnull final Object value) {
Map map = (Map) serializedValue;
//noinspection unchecked
map.keySet().forEach(key -> {
joiner.add(String.format("%s: \"%s\"", key, map.get(key)));
joiner.add(String.format("%s: \"%s\"", key, escapeDoubleQuotes(map.get(key).toString())));
});

serializedValue = joiner;
Expand Down Expand Up @@ -337,7 +341,7 @@ public String value(@Nonnull final Map<String, Object> namedProperties) {
return null;
}

return "\"" + value + "\"";
return "\"" + escapeDoubleQuotes(value) + "\"";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.influxdb.query.dsl.functions.properties.FunctionsParameters;
import com.influxdb.utils.Arguments;

import static com.influxdb.query.dsl.functions.properties.FunctionsParameters.escapeDoubleQuotes;

/**
* The column restrictions.
*
Expand Down Expand Up @@ -166,8 +168,9 @@ public ContainsRestrictions(@Nonnull final String fieldName, @Nonnull final Stri

@Override
public String toString() {
return "contains(value: r[\"" + fieldName + "\"], set:["
+ Arrays.stream(set).collect(Collectors.joining("\", \"", "\"", "\"")) + "])";
return "contains(value: r[\"" + escapeDoubleQuotes(fieldName) + "\"], set:["
+ Arrays.stream(set).map(FunctionsParameters::escapeDoubleQuotes)
.collect(Collectors.joining("\", \"", "\"", "\"")) + "])";
}
}

Expand Down Expand Up @@ -197,7 +200,4 @@ public String toString() {
return "r[\"" + escapeDoubleQuotes(fieldName) + "\"] " + operator + " " + value;
}
}
private static String escapeDoubleQuotes(final String val) {
return val.replace("\"", "\\\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
*/
package com.influxdb.query.dsl.functions.restriction;

import com.influxdb.query.dsl.Flux;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import java.util.HashMap;
import java.util.Map;

/**
* @author Jakub Bednar (bednar@github) (09/10/2018 10:33)
*/
Expand Down Expand Up @@ -97,12 +101,35 @@ void emptyLogical() {
}

@Test
void escapeStringValues() {
void escaping() {

Restrictions restrictions = Restrictions.tag("my-tag").equal("escaped\"tag");
Assertions.assertThat(restrictions.toString()).isEqualTo("r[\"my-tag\"] == \"escaped\\\"tag\"");

restrictions = Restrictions.column("my\"column").exists();
Assertions.assertThat(restrictions.toString()).isEqualTo("exists r[\"my\\\"column\"]");

restrictions = Restrictions.tag("_value").contains(new String[]{"val\"ue1", "value2"});

Assertions.assertThat(restrictions.toString()).isEqualTo("contains(value: r[\"_value\"], set:[\"val\\\"ue1\", \"value2\"])");

Flux flux = Flux
.from("telegraf")
.drop(new String[]{"host", "ta\"g"});

Assertions.assertThat(flux.toString())
.isEqualToIgnoringWhitespace("from(bucket:\"telegraf\") |> drop(columns:[\"host\", \"ta\\\"g\"])");

Map<String, String> map = new HashMap<>();
map.put("host", "ser\"ver");
map.put("_value", "va\"l");

flux = Flux
.from("tel\"egraf")
.rename(map);

Assertions.assertThat(flux.toString()).isEqualToIgnoringWhitespace("from(bucket:\"tel\\\"egraf\") |> rename(columns: {host: \"ser\\\"ver\", _value: \"va\\\"l\"})");

}

}

0 comments on commit 772c36f

Please sign in to comment.