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 Jackson deserialization for 7.17 snapshot repo data #737

Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rfs.version_es_7_10;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
Expand All @@ -16,6 +17,7 @@
public class SnapshotRepoData_ES_7_10 {
public static SnapshotRepoData_ES_7_10 fromRepoFile(Path filePath) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
SnapshotRepoData_ES_7_10 data = mapper.readValue(new File(filePath.toString()), SnapshotRepoData_ES_7_10.class);
data.filePath = filePath;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.rfs.version_es_7_10;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;

public class SnapshotRepoData_ES_7_10Test {

private final String basicSnapshotJson = " {\n" + //
" \"snapshots\" : [ {\n" + //
" \"name\" : \"snapshot-1\",\n" + //
" \"uuid\" : \"DToXzBoXTha6u2KRsQZ0Yw\",\n" + //
" \"state\" : 1,\n" + //
" \"index_metadata_lookup\" : {\n" + //
" \"VUr1hXkwQOOeWNBnvZ7pmA\" : \"4RVV4jgXRNqALWe9TxkoYA-_na_-1-2-1\"\n" + //
" },\n" + //
" \"version\" : \"7.10.2\"\n" + //
" } ],\n" + //
" \"indices\" : {\n" + //
" \"my-index\" : {\n" + //
" \"id\" : \"VUr1hXkwQOOeWNBnvZ7pmA\",\n" + //
" \"snapshots\" : [ \"DToXzBoXTha6u2KRsQZ0Yw\" ],\n" + //
" \"shard_generations\" : [ \"VcuUZNqoR8SilY8H3VY-fQ\" ]\n" + //
" }\n" + //
" },\n" + //
" \"min_version\" : \"7.9.0\",\n" + //
" \"index_metadata_identifiers\" : {\n" + //
" \"4RVV4jgXRNqALWe9TxkoYA-_na_-1-2-1\" : \"-I-dMZABiRFG_R03ChN0\"\n" + //
" }\n" + //
" }";

@Test
void testFromRepoFile_default() {
// Setup
final var jsonInFile = createTempFile(basicSnapshotJson);

// Act
final var result = SnapshotRepoData_ES_7_10.fromRepoFile(jsonInFile);

// Verify
assertThat(result.minVersion, equalTo("7.9.0"));
assertThat(result.indices.size(), equalTo(1));
}

@Test
void testFromRepoFile_extraFields() {
// Setup
var jsonWithExtraFields = insertAtLine(basicSnapshotJson, "\"foo\":\"bar\",", 2);
final var jsonInFile = createTempFile(jsonWithExtraFields);

// Act
final var result = SnapshotRepoData_ES_7_10.fromRepoFile(jsonInFile);

// Verify
assertThat(result.minVersion, equalTo("7.9.0"));
assertThat(result.indices.size(), equalTo(1));
}

private String insertAtLine(final String source, final String toAdd, final int lineNumber) {
final var lines = source.split("\n");
final StringBuilder sb = new StringBuilder();
for (var i = 0; i < lines.length; i++) {
if (lineNumber == i) {
sb.append(toAdd);
}
sb.append(lines[i]);
}
return sb.toString();
}

private Path createTempFile(final String contents) {
try {
final var file = File.createTempFile("repoFile", ".txt");
try (final var fos = new PrintWriter(new FileOutputStream(file))) {
fos.append(contents);
}
file.deleteOnExit();
return Path.of(file.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading