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

Scala primitives inside a List become "Object" #8

Closed
ghost opened this issue Jun 10, 2015 · 3 comments
Closed

Scala primitives inside a List become "Object" #8

ghost opened this issue Jun 10, 2015 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 10, 2015

A property of type List[String] will be correctly described as an array of strings, but if it's List[Int] it'll be described as an array of objects. The same goes for any parameterized type, if the type parameter is a primitive— for instance, when I tried to add support for Option[T] (see issue 7), I found that Option[String] worked correctly but Option[Int] did not.

This may be a problem with the underlying Jackson Scala module; a comment on this issue mentioned that Java reflection sometimes misreads Scala primitives as being java.lang.Object. It looked like someone had fixed that in jackson-scala-module, but maybe not. Whatever the reason, it's not currently possible to document a schema that has an array of numbers or booleans in it.

@ghost
Copy link
Author

ghost commented Jun 10, 2015

A test to reproduce this problem, based on an existing test in ScalaModelTest that used a Vector[String]:

case class ModelWithIntVector (
  ints: Vector[Int]
)

it should "read a model with vector of ints" in {
  val schemas = ModelConverters.getInstance().readAll(classOf[ModelWithIntVector]).asScala
  val model = schemas("ModelWithIntVector")
  val prop = model.getProperties().get("ints")
  prop.isInstanceOf[ArrayProperty] should be (true)
  prop.asInstanceOf[ArrayProperty].getItems.getType should be ("int")  // this fails; it is "object"
}

@fehguy
Copy link
Contributor

fehguy commented Jan 26, 2016

I think it's technically impossible to determine the type of a scala primitive from Java (or scala for that matter). There's really no other way to do it than use java.lang.Integer for types. Even Option[java.lang.Integer] will work just fine, but I don't think, without depending on a specific version of scalap, that we can interpret Option[Int] :(

@jimschubert
Copy link

I've tested this against the current develop branch (08cd397) and the List passes as and ArrayProperty of type number:

diff --git a/src/test/scala/ScalaModelTest.scala b/src/test/scala/ScalaModelTest.scala
index 6321f6b..c75756c 100644
--- a/src/test/scala/ScalaModelTest.scala
+++ b/src/test/scala/ScalaModelTest.scala
@@ -56,8 +56,20 @@ class ScalaModelTest extends FlatSpec with Matchers {
     prop.isInstanceOf[ArrayProperty] should be (true)
     prop.asInstanceOf[ArrayProperty].getItems.getType should be ("number")
   }
+
+  it should "read a model with list of ints" in {
+    val schemas = ModelConverters.getInstance().readAll(classOf[ModelWithIntList]).asScala
+    val model = schemas("ModelWithIntList")
+    val prop = model.getProperties().get("ints")
+    prop.isInstanceOf[ArrayProperty] should be (true)
+    prop.asInstanceOf[ArrayProperty].getItems.getType should be ("number")
+  }
 }
 
+case class ModelWithIntList (
+  ints: List[Int]
+)
+
 case class ModelWithVector (
   name: String,
   friends: Vector[String])

@fehguy fehguy closed this as completed Dec 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants