-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve error handling of invalid Avro identifier (#6239)
* fix: improve error message on invalid Avro identifier fixes issue where using a column name in ksqlDB that was incompatible with Avro, e.g. one that starts with a numeric, either wasn't detected at the time the statement was issued (in the case of C* statements), or returned a ambiguous error message (in the case of C*AS statements). Co-authored-by: Andy Coates <big-andy-coates@users.noreply.github.com>
- Loading branch information
1 parent
0f04061
commit 8dd3942
Showing
3 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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
91 changes: 91 additions & 0 deletions
91
ksqldb-serde/src/test/java/io/confluent/ksql/serde/avro/AvroFormatTest.java
This file contains 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,91 @@ | ||
package io.confluent.ksql.serde.avro; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient; | ||
import io.confluent.ksql.name.ColumnName; | ||
import io.confluent.ksql.schema.ksql.PersistenceSchema; | ||
import io.confluent.ksql.schema.ksql.SimpleColumn; | ||
import io.confluent.ksql.schema.ksql.types.SqlTypes; | ||
import io.confluent.ksql.serde.EnabledSerdeFeatures; | ||
import io.confluent.ksql.serde.FormatInfo; | ||
import io.confluent.ksql.util.KsqlConfig; | ||
import io.confluent.ksql.util.KsqlException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AvroFormatTest { | ||
|
||
@Mock | ||
private KsqlConfig config; | ||
@Mock | ||
private Supplier<SchemaRegistryClient> srFactory; | ||
@Mock | ||
private FormatInfo formatInfo; | ||
|
||
private AvroFormat format; | ||
private Map<String, String> formatProps; | ||
|
||
@Before | ||
public void setUp() { | ||
format = new AvroFormat(); | ||
formatProps = new HashMap<>(); | ||
|
||
when(formatInfo.getProperties()).thenReturn(formatProps); | ||
} | ||
|
||
@Test | ||
public void shouldThrowWhenCreatingSerdeIfSchemaContainsInvalidAvroNames() { | ||
// Given: | ||
final PersistenceSchema schema = PersistenceSchema.from( | ||
ImmutableList.of(column("1AintRight")), | ||
EnabledSerdeFeatures.of() | ||
); | ||
|
||
// When: | ||
final Exception e = assertThrows( | ||
KsqlException.class, | ||
() -> format.getSerde(schema, formatProps, config, srFactory) | ||
); | ||
|
||
// Then: | ||
assertThat(e.getMessage(), is("Schema is not compatible with Avro: Illegal initial character: 1AintRight")); | ||
} | ||
|
||
@Test | ||
public void shouldThrowWhenBuildingAvroSchemafSchemaContainsInvalidAvroNames() { | ||
// Given: | ||
final PersistenceSchema schema = PersistenceSchema.from( | ||
ImmutableList.of(column("2Bad")), | ||
EnabledSerdeFeatures.of() | ||
); | ||
|
||
// When: | ||
final Exception e = assertThrows( | ||
KsqlException.class, | ||
() -> format.toParsedSchema(schema, formatInfo) | ||
); | ||
|
||
// Then: | ||
assertThat(e.getMessage(), is("Schema is not compatible with Avro: Illegal initial character: 2Bad")); | ||
} | ||
|
||
private static SimpleColumn column(final String name) { | ||
final SimpleColumn column = mock(SimpleColumn.class); | ||
when(column.name()).thenReturn(ColumnName.of(name)); | ||
when(column.type()).thenReturn(SqlTypes.BIGINT); | ||
return column; | ||
} | ||
} |