julianpeeters / avrohugger

Generate Scala case class definitions from Avro schemas
Apache License 2.0
201 stars 120 forks source link

Fixed type error and wrongly succeeding test #187

Closed steve-e closed 9 months ago

steve-e commented 9 months ago

Hi,

There are 2 issues.

  1. avrohugger is generating code that does not compile. We have this problem in our codebase
  2. there is a test which generates this code, but passes.

I have investigated the test. Here are my findings

The example class LogicalSc has an error at line 22

val scaledValue = fxField.bytes.setScale(scale)

Here fxField is a SpecificFixed and fxField.bytes returns an array of byte, byte[]. There no setScale method.

This class LogicalSc is commented as generated code, and is stored in avrohugger-core/src/test/expected. This not in the scala classpath. Therefore the build does not attempt to compile it.

The test SpecificFileToFileSpec.e24 generates its own LogicalSc and then asserts that it is the same as example. This passes, but neither source is compiled.

How to demonstrate this problem

git clone https://github.com/julianpeeters/avrohugger.git
cd avrohugger
mkdir -p avrohugger-core/src/test/scala/example
cp -r  avrohugger-core/src/test/expected/specific/example/logical  avrohugger-core/src/test/scala/example/.
sbt test

Error in sbt output

[error] 22 |        val scaledValue = fxField.bytes.setScale(scale)
[error]    |                          ^^^^^^^^^^^^^^^^^^^^^^
[error]    |                          value setScale is not a member of Array[Byte]
[warn] one warning found
[error] one error found
[error] (avrohugger-core / Test / compileIncremental) Compilation failed

The test could be updated to assert that it asserts that the generated code compiles.

steve-e commented 9 months ago

The code generation error looks to be caused by this line

val scale = tree.DOT("bytes").DOT("setScale").APPLY(REF("scale"))

I will look into providing a fix in a PR