Skip to content

Commit 3b0b431

Browse files
committed
Do not normalize push command with changes for JSON5 upsert
1 parent e0f6fbb commit 3b0b431

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

server/src/main/java/com/linecorp/centraldogma/server/command/NormalizingPushCommand.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.linecorp.centraldogma.server.command;
1717

18+
import static com.linecorp.centraldogma.internal.Util.isJson5;
1819
import static java.util.Objects.requireNonNull;
1920

2021
import javax.annotation.Nullable;
@@ -24,6 +25,7 @@
2425

2526
import com.linecorp.centraldogma.common.Author;
2627
import com.linecorp.centraldogma.common.Change;
28+
import com.linecorp.centraldogma.common.ChangeType;
2729
import com.linecorp.centraldogma.common.Markup;
2830
import com.linecorp.centraldogma.common.Revision;
2931
import com.linecorp.centraldogma.server.storage.repository.Repository;
@@ -57,8 +59,13 @@ public final class NormalizingPushCommand extends AbstractPushCommand<CommitResu
5759
*/
5860
public PushAsIsCommand asIs(CommitResult commitResult) {
5961
requireNonNull(commitResult, "commitResult");
62+
63+
// JSON5 changes should not be normalized to preserve its content in replication log.
64+
final boolean useOriginalChanges = changes().stream().anyMatch(
65+
change -> change.type() == ChangeType.UPSERT_JSON && isJson5(change.path()));
66+
6067
return new PushAsIsCommand(timestamp(), author(), projectName(), repositoryName(),
61-
commitResult.revision().backward(1), summary(), detail(),
62-
markup(), commitResult.changes());
68+
commitResult.revision().backward(1), summary(), detail(), markup(),
69+
useOriginalChanges ? changes() : commitResult.changes());
6370
}
6471
}

server/src/test/java/com/linecorp/centraldogma/server/internal/replication/ZooKeeperCommandExecutorTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ void testRace() throws Exception {
223223
}
224224
}
225225

226+
@Test
227+
void testJson5UpsertReplicationLog() throws Exception {
228+
try (Cluster cluster = Cluster.of(ZooKeeperCommandExecutorTest::newMockDelegate)) {
229+
final Replica replica1 = cluster.get(0);
230+
231+
final List<Change<?>> changes =
232+
ImmutableList.of(Change.ofJsonUpsert("/foo.json5", "{a: 'b',}"));
233+
234+
final Command<CommitResult> command = Command.push(Author.DEFAULT, "project", "repo",
235+
new Revision(1), "summary", "detail",
236+
Markup.PLAINTEXT, changes);
237+
238+
replica1.commandExecutor().execute(command).join();
239+
final Optional<ReplicationLog<?>> commandResult = replica1.commandExecutor().loadLog(0, false);
240+
241+
assertThat(commandResult).isPresent();
242+
assertThat(commandResult.get().command()).isInstanceOf(PushAsIsCommand.class);
243+
assertThat(((PushAsIsCommand) commandResult.get().command()).changes()).isEqualTo(changes);
244+
}
245+
}
246+
226247
/**
227248
* Makes sure that we can stop a replica that's waiting for the initial quorum.
228249
*/

0 commit comments

Comments
 (0)