julianpeeters / avrohugger

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

Invalid scala generated for record with 2 fixed types in different namespaces but same schema name #190

Closed steve-e closed 8 months ago

steve-e commented 9 months ago

We have come up against the following problem. If a record has 2 fixed types in different namespaces but same schema name then the code generated does not compile.

Here is an example avro schema dmonstrating the problem

{
  "type": "record",
  "name": "FixedTwo",
  "namespace": "fixedtwo",
  "fields": [
    {
      "name": "first",
      "type": {
        "type": "fixed",
        "name": "fixed",
        "namespace": "fixedtwo.one",
        "size": 16
      }
    },
    {
      "name": "second",
      "type": {
        "type": "fixed",
        "name": "fixed",
        "namespace": "fixedtwo.two",
        "size": 16
      }
    }
  ]
}

Using these typical test settings

    val infile = new java.io.File("avrohugger-core/src/test/avro/fixed_two.avsc")
    val gen = new Generator(SpecificRecord)
    val outDir = gen.defaultOutputDir + "/specific/"
    gen.fileToFile(infile, outDir)

This generates 3 source files

target/generated-sources/specific/fixedtwo/FixedTwo.scala target/generated-sources/specific/fixedtwo/one/fixed.scala target/generated-sources/specific/fixedtwo/two/fixed.scala

The fixed classes are in different packages so having the same simple class name is not a problem

$ find . -name fixed.scala
./target/generated-sources/specific/fixedtwo/one/fixed.scala
./target/generated-sources/specific/fixedtwo/two/fixed.scala                                                                                                                                       
$ find . -name fixed.scala -exec grep package {}  \;
package fixedtwo.one
package fixedtwo.two

However, the FixedTwo record class imports both fixed classes

/** MACHINE-GENERATED FROM AVRO SCHEMA. DO NOT EDIT DIRECTLY */
package fixedtwo

import scala.annotation.switch

import fixedtwo.two.fixed

import fixedtwo.one.fixed

final case class FixedTwo(var first: fixed, var second: fixed) extends org.apache.avro.specific.SpecificRecordBase {

This causes a compilation error as the name fixed is ambiguous.

julianpeeters commented 9 months ago

thanks again for the report, @steve-e

Possible solutions: as I see it,

julianpeeters commented 9 months ago

My own timeframe is pretty far out, currently, but happy to help with what I can:

Background: regarding avrohugger's import system,

Current Status: Slowly replacing the import system by instead generating fully qualified names (e.g., here, and here)

julianpeeters commented 8 months ago

fixed by #191