Paul C
Paul C

Reputation: 8457

Referencing an avro schema in a different file does not work - avro throws an error

I have two schemas,

a.avsc

{
  "namespace": "my.ns",
  "type": "record",
  "name": "A",
  "fields": [
    {
      "name": "b",
      "type": "my.ns.B"
    }
  ]
}

b.avsc

{
  "namespace": "my.ns",
  "type": "record",
  "name": "B",
  "fields": [
  ]
}

This throws an exception:

[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.10.2:schema (default) on project schema: 
Execution default of goal org.apache.avro:avro-maven-plugin:1.10.2:schema failed: 
"my.ns.B" is not a defined name. The type of the "b" field must be a defined name or a {"type": ...} expression. -> [Help 1]

If I put them both in the same file, it works though. But B has to be declared first.

[
  {
    "namespace": "my.ns",
    "type": "record",
    "name": "B",
    "fields": [
    ]
  },
  {
    "namespace": "my.ns",
    "type": "record",
    "name": "A",
    "fields": [
      {
        "name": "b",
        "type": "my.ns.B"
      }
    ]
  }
]

My maven plugin is setup like:

            <plugin>
                <groupId>org.apache.avro</groupId>
                <artifactId>avro-maven-plugin</artifactId>
                <version>${avro.version}</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>schema</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${project.basedir}/src/main/avro/
                            </sourceDirectory>
                            <outputDirectory>${project.basedir}/target/generated-sources/main/java/
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Upvotes: 3

Views: 1674

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

This is expected behaviour; you cannot create cross-reference AVSC files.

As an alternative, I would suggest you use IDL files

Update the POM

<goals>
    <goal>schema</goal>
    <goal>idl-protocol</goal>
</goals>

Create some records.idl file

@namespace("my.ns")
protocol MyProtocol {
  record B {

  }

  record A {
    B b;
  }
}

The resulting schema file will be very similar to the combined AVSC file that you defined.

Upvotes: 2

Related Questions