Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option[Int] stopped working with version >= 0.13.0 #72

Open
ElfoLiNk opened this issue Apr 5, 2018 · 6 comments
Open

Option[Int] stopped working with version >= 0.13.0 #72

ElfoLiNk opened this issue Apr 5, 2018 · 6 comments

Comments

@ElfoLiNk
Copy link

ElfoLiNk commented Apr 5, 2018

After we upgraded to 0.13.0 Option[Int] is no more converted correctly.

This test passes with 0.12.0 but not after:


it should "process Model with Scala Option Int" in {
    val converter = ModelConverters.getInstance()
    val schemas = converter.readAll(classOf[ModelWOptionInt]).asScala.toMap
    val model = schemas.get("ModelWOptionInt")
    model should be('defined)
    val optInt = model.get.getProperties.get("optInt")
    optInt should not be null
    optInt shouldBe a[properties.BaseIntegerProperty]
    optInt.getRequired should be(false)
  }

case class ModelWOptionInt(optInt: Option[Int])

io.swagger.models.properties.ObjectProperty@d9eac59d was not an instance of io.swagger.models.properties.BaseIntegerProperty, but an instance of io.swagger.models.properties.ObjectProperty

@pjfanning
Copy link
Collaborator

The real issue is that the Jackson Databind code is type erasing the inner type for Option[Int].
The reference type returned is java.lang.Object instead scala.Int (nextType in code below).

  case rt: ReferenceType if isOption(cls) =>
    val nextType = rt.getContentType

So based on this, all that swagger-akka-http can do is treat this as an object as opposed to an int.

@ElfoLiNk
Copy link
Author

ElfoLiNk commented Apr 5, 2018

Ok but why was working with 0.12.0 ?

@pjfanning
Copy link
Collaborator

code changed in 0.13.0 but the working 0.12.0 code is accidental - all Option[T] where T is a Scala primitive like Int, Boolean, etc were being treated as Numbers due a matching error and fixing the Boolean case highlighted the general erasure issue
So in the end, it is probably best to go to jackson-scala-module and produce a test case showing the issue with Option[T] where T is a Scala primitive like Int, Boolean, etc

@pjfanning
Copy link
Collaborator

the only safe workaround is to use swagger annotations to correct the issue, eg

case class ModelWOptionInt(
  @(ApiModelProperty @field)(value="this is an Option[Int] attribute", required = false, dataType = "integer") optInt: Option[Int])

@pjfanning
Copy link
Collaborator

this jackson issue seems similar to FasterXML/jackson-module-scala#368

  • I took that test case and modified it to have Option[BigDecimal] and that worked ok so that test seems to be exhibiting similar issues with Option[T} where T is scala.Int, scala.Double, scala.Boolean but if T is something like scala.math.BigDecimal then the inner type is not erased.

@pjfanning
Copy link
Collaborator

This issue should be resolved by using the latest version of https://github.com/swagger-akka-http/swagger-scala-module

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants