Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

FIX: Different namespace in referenced class didn't compile #37

Merged
merged 2 commits into from
Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object AvroTypeMatcher {
}
}

schema.getType match {
schema.getType match {
case Schema.Type.ARRAY => {
val typeTree = tq"List[${toScala(namespace, schema.getElementType, c)}]"
expandNestedTypes(typeTree)
Expand All @@ -30,14 +30,18 @@ object AvroTypeMatcher {
case Schema.Type.LONG => typeOf[Long]
case Schema.Type.INT => typeOf[Int]
case Schema.Type.NULL => typeOf[Null]
case Schema.Type.RECORD => {
case Schema.Type.RECORD => {
schema.getName match {
//cases where a record is found as a field vs found as a member of a union vs found as an element of an array
case "array" | "union" => tq"schema.getName".tpe
case recordName => {
val fullName = namespace match {
case null => recordName
case ns: String => (ns + "." + recordName)
// Prefer the namespace of the type, can be different.
val ns = Seq(schema.getNamespace, namespace).
map(Option(_)).flatten.filterNot(_.isEmpty).headOption

val fullName = ns match {
case None => recordName
case Some(ns) => (ns + "." + recordName)
}
try {
c.mirror.staticClass(fullName).toType
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[
{ "type":"record",
"name":"Person",
"namespace":"com.miguno.avro.differentns",
"fields":[
{"name":"id", "type":"long"},
{"name":"name","type":"string"}
],
"doc:":"A basic schema for a user, to be reused across namespaces."
},
{ "type":"record",
"name":"Tweet",
"namespace":"com.miguno.avro.differentns.twitter",
"fields":[
{
"name":"author",
"type":"com.miguno.avro.differentns.Person"
},
{
"name":"text", "type":"string"
}
],
"doc:":"A basic schema for storing Twitter messages"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.miguno.avro.differentns

import org.specs2.mutable.Specification

import test.TestUtil

import com.julianpeeters.avro.annotations._

@AvroTypeProvider("tests/src/test/resources/AvroTypeProviderTestDifferentNamespace.avsc")
@AvroRecord
case class Person()

// Nested namespace
package twitter {
@AvroTypeProvider("tests/src/test/resources/AvroTypeProviderTestDifferentNamespace.avsc")
@AvroRecord
case class Tweet()
}

class AvroTypeProviderDifferentNamespaceTest extends Specification {

"A case class with types provided from a .avsc avro schema file referencing another class in a different namespace" should {
"serialize and deserialize correctly" in {
val record = twitter.Tweet(Person(id = 1, name = "John"), "Yo!")
TestUtil.verifyWriteAndRead(List(record))
}
}
}