Skip to content

Commit

Permalink
[#3165] fix(client-java): Fix the http client toJson encode without…
Browse files Browse the repository at this point in the history
… UTF-8 (#3179)

### What changes were proposed in this pull request?

Currently in the java client, the json result is not encoded with
`UTF-8` when requesting the server, which will cause some Chinese
characters to be garbled.It will use `ISO_8859_1` as default. This PR
fixed this.

![image](https://github.com/datastrato/gravitino/assets/26177232/9342dd32-1ded-4670-a3c7-37d9a5673955)

### Why are the changes needed?

Fix: #3165 

### How was this patch tested?

Add some ITs.

---------

Co-authored-by: xiaojiebao <xiaojiebao@xiaomi.com>
  • Loading branch information
xloya and xiaojiebao authored Apr 30, 2024
1 parent b841e58 commit d6fe135
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ public void testCreateFileset() throws IOException {
Fileset.Type.MANAGED, fileset3.type(), "fileset type should be MANAGED by default");
}

@Test
public void testCreateFilesetWithChinese() throws IOException {
// create fileset
String filesetName = "test_create_fileset_with_chinese";
String storageLocation = storageLocation(filesetName) + "/中文目录test";
Assertions.assertFalse(
hdfs.exists(new Path(storageLocation)), "storage location should not exists");
Fileset fileset =
createFileset(
filesetName,
"这是中文comment",
Fileset.Type.MANAGED,
storageLocation,
ImmutableMap.of("k1", "v1", "test", "中文测试test", "中文key", "test1"));

// verify fileset is created
assertFilesetExists(filesetName);
Assertions.assertNotNull(fileset, "fileset should be created");
Assertions.assertEquals("这是中文comment", fileset.comment());
Assertions.assertEquals(Fileset.Type.MANAGED, fileset.type());
Assertions.assertEquals(storageLocation, fileset.storageLocation());
Assertions.assertEquals(3, fileset.properties().size());
Assertions.assertEquals("v1", fileset.properties().get("k1"));
Assertions.assertEquals("中文测试test", fileset.properties().get("test"));
Assertions.assertEquals("test1", fileset.properties().get("中文key"));
}

@Test
public void testExternalFileset() throws IOException {
// create fileset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ public HTTPClient build() {

private StringEntity toJson(Object requestBody) {
try {
return new StringEntity(mapper.writeValueAsString(requestBody));
return new StringEntity(mapper.writeValueAsString(requestBody), StandardCharsets.UTF_8);
} catch (JsonProcessingException e) {
throw new RESTException(e, "Failed to write request body: %s", requestBody);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ public void testCreateCatalogWithoutProperties() {
metalake.dropCatalog(catalogIdent);
}

@Test
public void testCreateCatalogWithChinese() {
String catalogName = GravitinoITUtils.genRandomName("catalogz");
NameIdentifier catalogIdent = NameIdentifier.of(metalakeName, catalogName);
Assertions.assertFalse(metalake.catalogExists(catalogIdent));

Map<String, String> properties = Maps.newHashMap();
properties.put("metastore.uris", hmsUri);
metalake.createCatalog(
catalogIdent, Catalog.Type.RELATIONAL, "hive", "这是中文comment", properties);
Assertions.assertTrue(metalake.catalogExists(catalogIdent));
Catalog catalog = metalake.loadCatalog(catalogIdent);
Assertions.assertEquals(catalogName, catalog.name());
Assertions.assertEquals(Catalog.Type.RELATIONAL, catalog.type());
Assertions.assertEquals("hive", catalog.provider());
Assertions.assertEquals("这是中文comment", catalog.comment());
Assertions.assertTrue(catalog.properties().containsKey("metastore.uris"));

metalake.dropCatalog(catalogIdent);
}

@Test
public void testListCatalogsInfo() {
String relCatalogName = GravitinoITUtils.genRandomName("rel_catalog_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ public void testCreateMetalake() {
});
}

@Test
public void testCreateMetalakeWithChinese() {
client.createMetalake(
NameIdentifier.parse(metalakeNameA), "这是中文comment", Collections.emptyMap());
GravitinoMetalake metalake = client.loadMetalake(NameIdentifier.of(metalakeNameA));
assertEquals(metalakeNameA, metalake.name());
assertEquals("这是中文comment", metalake.comment());
assertEquals(AuthConstants.ANONYMOUS_USER, metalake.auditInfo().creator());

// Test metalake name already exists
Map<String, String> emptyMap = Collections.emptyMap();
NameIdentifier exists = NameIdentifier.parse(metalakeNameA);
assertThrows(
MetalakeAlreadyExistsException.class,
() -> {
client.createMetalake(exists, "metalake A comment", emptyMap);
});
}

@Test
public void testDropMetalakes() {
GravitinoMetalake metalakeA =
Expand Down

0 comments on commit d6fe135

Please sign in to comment.