Skip to content

Commit

Permalink
Merge #656
Browse files Browse the repository at this point in the history
656: Mark internal fields as transient r=curquiza a=axelrindle

# Pull Request

## Related issue
Fixes #655

## What does this PR do?
Gson has issues working with the class `com.meilisearch.sdk.Index` in Java 9 and higher. Specifically all the fields which hold a `com.meilisearch.sdk.HttpClient`. Marking those fields as `transient` excludes them from serialization and thus preventing the reflective access on JDK-internal types.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Axel Rindle <mail@axelrindle.de>
  • Loading branch information
meili-bors[bot] and axelrindle committed Sep 26, 2023
2 parents 521b0b9 + 316c3ec commit 6d4fc2f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class Index implements Serializable {
@Getter protected Pagination pagination;
@Getter protected Faceting faceting;
@Getter @ToString.Exclude protected String updatedAt;
@Getter @ToString.Exclude protected Config config;
@ToString.Exclude protected Documents documents;
@ToString.Exclude protected TasksHandler tasksHandler;
@ToString.Exclude protected Search search;
@ToString.Exclude protected SettingsHandler settingsHandler;
@ToString.Exclude protected InstanceHandler instanceHandler;
@Getter @ToString.Exclude protected transient Config config;
@ToString.Exclude protected transient Documents documents;
@ToString.Exclude protected transient TasksHandler tasksHandler;
@ToString.Exclude protected transient Search search;
@ToString.Exclude protected transient SettingsHandler settingsHandler;
@ToString.Exclude protected transient InstanceHandler instanceHandler;

/**
* Sets the Meilisearch configuration for the index
Expand Down
35 changes: 22 additions & 13 deletions src/test/java/com/meilisearch/integration/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.meilisearch.integration;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;
import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.utils.Movie;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Modifier;
import org.junit.jupiter.api.*;

@Tag("integration")
public class ClientTest extends AbstractIT {
Expand Down Expand Up @@ -304,4 +296,21 @@ public void testCreateDump() throws Exception {
assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
assertThat(dump.getType(), is(equalTo("dumpCreation")));
}

/**
* Test the exclusion of transient fields.
*
* @see <a href="https://github.com/meilisearch/meilisearch-java/issues/655">Issue #655</a>
*/
@Test
public void testTransientFieldExclusion() throws MeilisearchException {
Index test = client.index("Transient");

Gson gsonWithTransient =
new GsonBuilder().excludeFieldsWithModifiers(Modifier.STATIC).create();

// TODO: Throws StackOverflowError on JDK 1.8, but InaccessibleObjectException on JDK9+
Assertions.assertThrows(StackOverflowError.class, () -> gsonWithTransient.toJson(test));
Assertions.assertDoesNotThrow(() -> gson.toJson(test));
}
}

0 comments on commit 6d4fc2f

Please sign in to comment.