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: query response parser should use UTF-8 encoding #164

Merged
merged 2 commits into from
Oct 16, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

### Bug Fixes
1. [#161](https://github.com/influxdata/influxdb-client-java/pull/161): Offset param could be 0 - FluxDSL
1. [#164](https://github.com/influxdata/influxdb-client-java/pull/164): Query response parser uses UTF-8 encoding

## 1.12.0 [2020-10-02]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void parseFluxResponse(@Nonnull final BufferedSource bufferedSource,

Arguments.checkNotNull(bufferedSource, "bufferedSource");

Reader reader = new InputStreamReader(bufferedSource.inputStream());
Reader reader = new InputStreamReader(bufferedSource.inputStream(), StandardCharsets.UTF_8);

ParsingState parsingState = ParsingState.NORMAL;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
package com.influxdb.query.internal;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.Instant;
import java.util.Base64;
Expand Down Expand Up @@ -468,7 +470,7 @@ void parsingWithoutTableDefinition() {
@Test
void multipleQueries() throws IOException {

String data ="#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
String data = "#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t1,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
Expand Down Expand Up @@ -521,7 +523,7 @@ void multipleQueries() throws IOException {
@Test
void tableIndexNotStartAtZero() throws IOException {

String data ="#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
String data = "#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t1,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
Expand All @@ -548,6 +550,28 @@ void tableIndexNotStartAtZero() throws IOException {
Assertions.assertThat(tables.get(1).getRecords().get(0).getTable()).isEqualTo(1);
}

@Test
public void responseIsInUTF8() throws IOException, IllegalAccessException, NoSuchFieldException {

String oldCharset = Charset.defaultCharset().toString();
setDefaultCharset("GBK");

try {
String data = "#datatype,string,long,string,string,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,double,string\n"
+ "#group,false,false,true,true,true,true,false,false,true\n"
+ "#default,t1,,,,,,,,\n"
+ ",result,table,_field,_measurement,_start,_stop,_time,_value,tag\n"
+ ",,1,value,python_client_test,2010-02-27T04:48:32.752600083Z,2020-02-27T16:48:32.752600083Z,2020-02-27T16:20:00Z,2,ěščřĚŠČŘ Přerov 🍺\n";

List<FluxTable> tables = parseFluxResponse(data);
Assertions.assertThat(tables).hasSize(1);
Assertions.assertThat(tables.get(0).getRecords()).hasSize(1);
Assertions.assertThat(tables.get(0).getRecords().get(0).getValueByKey("tag")).isEqualTo("ěščřĚŠČŘ Přerov 🍺");
} finally {
setDefaultCharset(oldCharset);
}
}

@Nonnull
private List<FluxTable> parseFluxResponse(@Nonnull final String data) throws IOException {

Expand All @@ -561,8 +585,15 @@ private List<FluxTable> parseFluxResponse(@Nonnull final String data) throws IOE
return consumer.getTables();
}

private void setDefaultCharset(final String name) throws NoSuchFieldException, IllegalAccessException {

Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null, Charset.forName(name));
}
private static class DefaultCancellable implements Cancellable {


private boolean cancelled = false;

@Override
Expand Down