Skip to content

Commit 0a06ab6

Browse files
author
Yann Rouillard
committed
fix: improve avro library detection
So far we checked for the presence of the library in the target schema class file but this library could be loaded indirectly and not explicitely listed. As a fallback, we now: - look for the library string without the import keyword, to allow to specify it using the schemas static annotation if needed (for instance '@Schema(library="com.sksamuel.avro4s")') - search for the library in the build.sbt as well.
1 parent a9a7941 commit 0a06ab6

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

kafka/check-local-schemas.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,27 @@ find_schema_class() {
6161
echo "${schema_package}.${schema_class_name}"
6262
}
6363

64+
is_library_used() {
65+
local library="$1" candidate_class_file="$2"
66+
67+
# if the library is not directly found in the candidate class file
68+
# we fallback on checking the build.sbt file itself
69+
# This doesn't fully protect against from indirect library loading
70+
# but it's a good enough heuristic for now
71+
for candidate in "${candidate_class_file}" build.sbt; do
72+
if grep -q -E "[^#]*${library}" "${candidate}"; then
73+
return 0
74+
fi
75+
done
76+
return 1
77+
}
78+
6479
find_avro_library() {
6580
local schema_class_file="$1"
6681

67-
if grep -q "import com.sksamuel.avro4s" "${schema_class_file}"; then
82+
if is_library_used "com.sksamuel.avro4s" "${schema_class_file}"; then
6883
echo "avro4s"
69-
elif grep -q "import vulcan" "${schema_class_file}"; then
84+
elif is_library_used "vulcan" "${schema_class_file}"; then
7085
echo "vulcan"
7186
else
7287
error "Could not find any avro library import in ${schema_class_file}"

0 commit comments

Comments
 (0)