-
-
Notifications
You must be signed in to change notification settings - Fork 346
BE: Restore SchemaRegistry auto-selection for Avro topics (#1833) #1842
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
Open
joshuaNathaniel
wants to merge
1
commit into
kafbat:main
Choose a base branch
from
joshuaNathaniel:issues/1833
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
253 changes: 253 additions & 0 deletions
253
api/src/test/java/io/kafbat/ui/serdes/ClusterSerdesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| package io.kafbat.ui.serdes; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.kafbat.ui.serde.api.DeserializeResult; | ||
| import io.kafbat.ui.serde.api.PropertyResolver; | ||
| import io.kafbat.ui.serde.api.RecordHeaders; | ||
| import io.kafbat.ui.serde.api.SchemaDescription; | ||
| import io.kafbat.ui.serde.api.Serde; | ||
| import io.kafbat.ui.serdes.builtin.StringSerde; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
| import javax.annotation.Nullable; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ClusterSerdesTest { | ||
|
|
||
| private static final String TOPIC = "orders"; | ||
|
|
||
| /** | ||
| * Fake serde whose canSerialize / canDeserialize / couldBePreferable are knobbed per test. | ||
| * Keeps tests focused on selection logic in ClusterSerdes without standing up a real serde. | ||
| */ | ||
| static final class StubSerde implements Serde { | ||
| private final boolean canSerialize; | ||
| private final boolean canDeserialize; | ||
| private final boolean couldBePreferable; | ||
|
|
||
| StubSerde(boolean canSerialize, boolean canDeserialize, boolean couldBePreferable) { | ||
| this.canSerialize = canSerialize; | ||
| this.canDeserialize = canDeserialize; | ||
| this.couldBePreferable = couldBePreferable; | ||
| } | ||
|
|
||
| static StubSerde alwaysApplicable() { | ||
| return new StubSerde(true, true, false); | ||
| } | ||
|
|
||
| static StubSerde preferable() { | ||
| return new StubSerde(true, true, true); | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(PropertyResolver serdeProperties, | ||
|
Check failure on line 46 in api/src/test/java/io/kafbat/ui/serdes/ClusterSerdesTest.java
|
||
| PropertyResolver kafkaClusterProperties, | ||
| PropertyResolver globalProperties) { | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<String> getDescription() { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<SchemaDescription> getSchema(String topic, Target type) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canDeserialize(String topic, Target type) { | ||
| return canDeserialize; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canSerialize(String topic, Target type) { | ||
| return canSerialize; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean couldBePreferable(String topic, Target type) { | ||
| return couldBePreferable; | ||
| } | ||
|
|
||
| @Override | ||
| public Serializer serializer(String topic, Target type) { | ||
| return input -> input == null ? new byte[0] : input.getBytes(); | ||
| } | ||
|
|
||
| @Override | ||
| public Deserializer deserializer(String topic, Target type) { | ||
| return (RecordHeaders headers, byte[] data) -> | ||
| new DeserializeResult(new String(data), DeserializeResult.Type.STRING, Map.of()); | ||
| } | ||
| } | ||
|
|
||
| private static SerdeInstance instance(String name, Serde serde, | ||
| @Nullable Pattern keyPattern, | ||
| @Nullable Pattern valuePattern) { | ||
| return new SerdeInstance(name, serde, keyPattern, valuePattern, null); | ||
| } | ||
|
|
||
| private static SerdeInstance stringInstance() { | ||
| return instance(StringSerde.NAME, new StringSerde(), null, null); | ||
| } | ||
|
|
||
| private static ClusterSerdes cluster(LinkedHashMap<String, SerdeInstance> serdes, | ||
| @Nullable SerdeInstance defaultKey, | ||
| @Nullable SerdeInstance defaultValue) { | ||
| var fallback = new SerdeInstance("Fallback", new StringSerde(), null, null, null); | ||
| return new ClusterSerdes(serdes, defaultKey, defaultValue, fallback); | ||
| } | ||
|
|
||
| // --- Pass 3 (couldBePreferable) — the regression pin ----------------------------------------- | ||
|
|
||
| @Test | ||
| void preferableSerdeIsSelectedOverStringFallbackWhenNoPatternsOrDefaults() { | ||
| // Mirrors: auto-configured SchemaRegistry + Avro topic, no defaultValueSerde configured. | ||
| // The regression in #1833: this returned StringSerde instead of the preferable SR. | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Preferable", instance("Preferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("Preferable"); | ||
| } | ||
|
|
||
| @Test | ||
| void preferableSerdeIsSelectedForSerializeToo() { | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Preferable", instance("Preferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForSerialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("Preferable"); | ||
| } | ||
|
|
||
| // --- Default-true on the Serde interface used to be the bug; pin that pass 3 doesn't over-fire. | ||
|
|
||
| @Test | ||
| void fallsBackToStringWhenNoSerdeNominatesItself() { | ||
| // No serde overrides couldBePreferable to true. With the corrected interface default of | ||
| // false, pass 3 finds nothing and we fall through to StringSerde via the caller's .orElse(...) | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Other", instance("Other", StubSerde.alwaysApplicable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo(StringSerde.NAME); | ||
| } | ||
|
|
||
| // --- Pass 2 (explicit cluster default) -------------------------------------------------------- | ||
|
|
||
| @Test | ||
| void explicitDefaultValueSerdeWinsOverPreferable() { | ||
| // Even when a serde nominates itself via couldBePreferable, the cluster-level explicit | ||
| // default (defaultValueSerde) takes precedence — pass 2 beats pass 3. | ||
| var stringInst = stringInstance(); | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInst); | ||
| serdes.put("Preferable", instance("Preferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, stringInst); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo(StringSerde.NAME); | ||
| } | ||
|
|
||
| @Test | ||
| void explicitDefaultKeySerdeOnlyAppliesToKeyTarget() { | ||
| var stringInst = stringInstance(); | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInst); | ||
| serdes.put("Preferable", instance("Preferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, stringInst, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.KEY).getName()) | ||
| .isEqualTo(StringSerde.NAME); | ||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("Preferable"); | ||
| } | ||
|
|
||
| // --- Pass 1 (explicit topic patterns) --------------------------------------------------------- | ||
|
|
||
| @Test | ||
| void explicitTopicValuePatternStillMatchesEvenIfCouldBePreferableFalse() { | ||
| // Regression pin for the latent dead-skip bug from PR #1650: a serde with couldBePreferable | ||
| // returning false must still be selected when its topicValuesPattern matches. | ||
| var notPreferable = new StubSerde(true, true, false); | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Patterned", instance("Patterned", notPreferable, null, Pattern.compile(TOPIC))); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("Patterned"); | ||
| } | ||
|
|
||
| @Test | ||
| void explicitTopicPatternBeatsPreferableSerde() { | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Patterned", instance("Patterned", StubSerde.alwaysApplicable(), null, Pattern.compile(TOPIC))); | ||
| serdes.put("Preferable", instance("Preferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("Patterned"); | ||
| } | ||
|
|
||
| @Test | ||
| void keyPatternDoesNotMatchValueTarget() { | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("KeyOnly", instance("KeyOnly", StubSerde.alwaysApplicable(), Pattern.compile(TOPIC), null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.KEY).getName()) | ||
| .isEqualTo("KeyOnly"); | ||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo(StringSerde.NAME); | ||
| } | ||
|
|
||
| // --- Cross-pass guards ------------------------------------------------------------------------ | ||
|
|
||
| @Test | ||
| void preferableButCannotDeserializeIsSkipped() { | ||
| var preferableButCantDeser = new StubSerde(true, false, true); | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("Preferable", instance("Preferable", preferableButCantDeser, null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo(StringSerde.NAME); | ||
| } | ||
|
|
||
| @Test | ||
| void pass3IterationOrderFollowsRegistrationOrder() { | ||
| // Two serdes both nominate themselves; the first registered wins. | ||
| var serdes = new LinkedHashMap<String, SerdeInstance>(); | ||
| serdes.put(StringSerde.NAME, stringInstance()); | ||
| serdes.put("FirstPreferable", instance("FirstPreferable", StubSerde.preferable(), null, null)); | ||
| serdes.put("SecondPreferable", instance("SecondPreferable", StubSerde.preferable(), null, null)); | ||
|
|
||
| var cs = cluster(serdes, null, null); | ||
|
|
||
| assertThat(cs.suggestSerdeForDeserialize(TOPIC, Serde.Target.VALUE).getName()) | ||
| .isEqualTo("FirstPreferable"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an explicit no-op note in
StubSerde.configure.Line 46 overrides
configure(...)with an empty body; this is valid for a test stub, but it currently triggers Sonar and can fail CI quality gates.Suggested patch
`@Override` public void configure(PropertyResolver serdeProperties, PropertyResolver kafkaClusterProperties, PropertyResolver globalProperties) { + // no-op: test stub intentionally does not require configuration }📝 Committable suggestion
🧰 Tools
🪛 GitHub Check: SonarCloud Code Analysis
[failure] 46-46: Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation.
See more on https://sonarcloud.io/project/issues?id=kafbat_kafka-ui&issues=AZ4HoKQI68xee83T1wfo&open=AZ4HoKQI68xee83T1wfo&pullRequest=1842
🤖 Prompt for AI Agents