julianpeeters / avrohugger

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

Namespace of fixed type in record not renamed #192

Open steve-e opened 7 months ago

steve-e commented 7 months ago

I have a record with a fixed type. Both have the record and the fixed type have namespaces set. I can rename the namespace of the record with avrohugger, but not the namespace of the fixed type

Example avro schema.

{
  "type": "record",
  "name": "same_name",
  "namespace": "namespace.conflict",
  "fields": [
    {
      "name": "search_results",
      "type": [
        "null",
        {
          "type": "fixed",
          "name": "fixed",
          "namespace": "namespace.conflict.same_name.search_results",
          "size": 16,
          "logicalType": "decimal",
          "precision": 38,
          "scale": 0
        }
      ],
      "default": null
    }
  ]
}

Unit test demonstrating the issue


class SpecificSameRecordNameAsNamespaceSpec extends mutable.Specification {
  "a Generator" should {
    "generate files that compile even if the name of a record is the same as the name of a namespace" in {
      val infile = new java.io.File("avrohugger-core/src/test/avro/SpecificSameRecordNameAsNamespace.avsc")
      val gen = new Generator(SpecificRecord, avroScalaCustomNamespace = Map(
        "namespace.conflict.same_name" -> "namespace.renamed",
        "namespace.conflict" -> "namespace.no_conflict"
      ))
      val outDir = gen.defaultOutputDir + "/specific"
      gen.fileToFile(infile, outDir)
      val sourceRecord = scala.io.Source.fromFile(s"$outDir/namespace/no_conflict/same_name.scala").mkString
      sourceRecord ==== util.Util.readFile("avrohugger-core/src/test/expected/specific/namespace/no_conflict/same_name.scala")
      val sourceFixedType = scala.io.Source.fromFile(s"$outDir/namespace/renamed/fixed.scala").mkString
      sourceFixedType ==== util.Util.readFile("avrohugger-core/src/test/expected/specific/namespace/renamed/fixed.scala")
    }
  }
}

This test fails because the fixed type is not renamed and so is written to the un-renamed directory

[info] a Generator should
[error]   ! generate files that compile even if the name of a record is the same as the name of a namespace
[error]    java.io.FileNotFoundException: target/generated-sources/specific/namespace/renamed/fixed.scala (No such file or directory) (SpecificSameRecordNameAsNamespaceSpec.scala:19)
> find target/generated-sources/ -name "*scala"
target/generated-sources//specific/namespace/no_conflict/same_name.scala
target/generated-sources//specific/namespace/conflict/same_name/search_results/fixed.scala

For my use case, renaming only the record's namespace solved the issue I had (which is that the generated class FQN was the same as the fixed type package for this schema, resulting in compilation error for generated code).