fix(eventbridge): keep persisted rules from disappearing on reload (#878)

Rule.getRegion() is a derived getter (computed from the rule's ARN) but
has no matching setter. When EventBridgeService runs in any non-memory
storage mode, PersistentStorage's ObjectMapper writes "region" into
eventbridge-rules.json on save, then throws UnrecognizedPropertyException
on the next startup because Rule cannot deserialize it. The exception is
swallowed by PersistentStorage.load(), so the rule store comes up empty
and every PutRule made before the restart silently disappears.

Two changes on Rule:

- @JsonIgnore on getRegion() so the derived field is no longer written
  to disk.
- @JsonIgnoreProperties(ignoreUnknown = true) on the class to match the
  convention used by Target, InputTransformer, SqsParameters and most
  other persisted models, and to let a file that includes "region"
  (written before this change) still load cleanly after upgrade.

Adds RulePersistenceTest covering the PersistentStorage round-trip and
unknown-property tolerance.
This commit is contained in:
Yoshiyuki Ohta authored and GitHub committed 2026-05-17 11:24:30 -05:00
1 parent 70e61dcbda
commit ff80dac780
2 files changed
+88

No files matched your search

@@ -0,0 +1,84 @@
package io.github.hectorvent.floci.services.eventbridge;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.services.eventbridge.model.Rule;
import io.github.hectorvent.floci.services.eventbridge.model.RuleState;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
class RulePersistenceTest {
@TempDir
Path tempDir;
@Test
void rulePersistsAcrossInstances() {
Path filePath = tempDir.resolve("eventbridge-rules.json");
String key = "000000000000/rule:us-east-1:default/my-rule";
Rule rule = new Rule();
rule.setName("my-rule");
rule.setArn("arn:aws:events:us-east-1:000000000000:rule/my-rule");
rule.setAccountId("000000000000");
rule.setEventBusName("default");
rule.setScheduleExpression("rate(5 minutes)");
rule.setState(RuleState.ENABLED);
var writer = new PersistentStorage<String, Rule>(
filePath, new TypeReference<Map<String, Rule>>() {});
writer.put(key, rule);
var reader = new PersistentStorage<String, Rule>(
filePath, new TypeReference<Map<String, Rule>>() {});
reader.load();
Optional<Rule> loaded = reader.get(key);
assertTrue(loaded.isPresent(),
"Rule should survive a fresh PersistentStorage instance + load()");
assertEquals("my-rule", loaded.get().getName());
assertEquals("arn:aws:events:us-east-1:000000000000:rule/my-rule", loaded.get().getArn());
assertEquals("rate(5 minutes)", loaded.get().getScheduleExpression());
assertEquals(RuleState.ENABLED, loaded.get().getState());
}
@Test
void ignoresUnknownPropertiesOnLoad() throws Exception {
// A persisted file may contain fields the current Rule class does not
// recognize: either a derived field written by an older Floci version
// (e.g. "region") or a brand new field from a future version. Loading
// must not fail in either case.
Path filePath = tempDir.resolve("eventbridge-rules.json");
String key = "000000000000/rule:us-east-1:default/legacy-rule";
String legacyJson = """
{
"%s" : {
"name" : "legacy-rule",
"arn" : "arn:aws:events:us-east-1:000000000000:rule/legacy-rule",
"accountId" : "000000000000",
"eventBusName" : "default",
"scheduleExpression" : "rate(5 minutes)",
"state" : "ENABLED",
"fieldFromFutureVersion" : "whatever"
}
}
""".formatted(key);
Files.writeString(filePath, legacyJson);
var store = new PersistentStorage<String, Rule>(
filePath, new TypeReference<Map<String, Rule>>() {});
store.load();
Optional<Rule> loaded = store.get(key);
assertTrue(loaded.isPresent(),
"Rules with unknown extra fields should still load");
assertEquals("legacy-rule", loaded.get().getName());
}
}