question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using java.lang.Integer as Field Type

See original GitHub issue

Hello,

Once I convert this

case class SomeClass (
 someField: Int
)

to

case class SomeClass (
 someField: java.lang.Integer
)

I start getting an error on

    for {
      ad <- Gen.oneOf(random[SomeClass](testsNum))
    } yield {
      ad
        .toJson
    }

which is below.

Error:(17, 49) not enough arguments for method random: (implicit evidence$3: reflect.runtime.universe.WeakTypeTag[SomeClass], implicit evidence$4: org.scalacheck.Arbitrary[SomeClass])Seq[SomeClass].
Unspecified value parameter evidence$4.
      ad <- Gen.oneOf(random[SomeClass](testsNum))
Error:(17, 49) could not find implicit value for evidence parameter of type org.scalacheck.Arbitrary[SomeClass]
      ad <- Gen.oneOf(random[SomeClass](testsNum))

Could you help understanding how to tackle that? Sorry for the noise.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pete-protoncommented, Feb 6, 2020

I found a way to stick with Ints and it made the life way easier. The workaround is below

import com.fasterxml.jackson.databind.{ BeanProperty, DeserializationContext, JsonDeserializer }
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import javax.annotation.Nonnull
import scala.reflect.ClassTag

abstract class NonNullDeserializer[T](
  nullable: Boolean = true,
  fieldName: String = null)(implicit classTag: ClassTag[T])
  extends StdDeserializer[T](classTag.runtimeClass)
  with ContextualDeserializer {

  def createContextual(
    ctxt: DeserializationContext,
    property: BeanProperty): JsonDeserializer[_] = {
    val isNullable = property.getAnnotation(classOf[Nonnull]) == null
    val fieldName = property.getName

    createContextual(isNullable, fieldName)
  }

  protected def createContextual(
    isNullable: Boolean,
    fieldName: String): JsonDeserializer[T] = {
    val constructor = getClass.getConstructor(classOf[Boolean], classOf[String])

    constructor
      .newInstance(java.lang.Boolean.valueOf(isNullable), fieldName)
      .asInstanceOf[JsonDeserializer[T]]
  }

  override def getNullValue(ctxt: DeserializationContext): T = {
    if (!nullable) {
      ExceptionHandler.nullFieldValueFieldErr(fieldName) // handling nullability here
    }
    super.getNullValue(ctxt)
  }
}
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext

class NonnullIntDeserializer(
  nullable: Boolean = true,
  fieldName: String = null)
  extends NonNullDeserializer[Int](nullable, fieldName) {

  override def deserialize(
    p: JsonParser,
    ctxt: DeserializationContext): Int = {
    super._parseIntPrimitive(p, ctxt)
  }

  override def getNullValue(ctxt: DeserializationContext): Int = {
    super.getNullValue(ctxt)

    0
  }
}

then install it


import akka.http.scaladsl.model.DateTime
import com.fasterxml.jackson.databind.{ BeanDescription, DeserializationFeature, JsonMappingException, ObjectMapper }
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import javax.annotation.Nonnull
import scala.collection.JavaConversions._
import scala.reflect.ClassTag
import scala.util.Try

    val mapper = (new ObjectMapper() with ScalaObjectMapper)
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
      .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)

    val simpleModule = new SimpleModule()
      .addDeserializer(
        classOf[Int],
        new NonnullIntDeserializer())

so, I guess, we can resolve the ticket, which I will do with your permission, @DanielaSfregola

1reaction
DanielaSfregolacommented, Jan 30, 2020

Weird, if you are using case classes the type class derivation should have no problem in automatically generating an Arbitrary for each case class, even if nested.

I’d do some investigation and try to replicate the issue! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Integer (Java Platform SE 7 ) - Oracle Help Center
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field...
Read more >
Java.lang.Integer class in Java - GeeksforGeeks
Integer class is a wrapper class for the primitive type int which contains several methods to effectively deal with an int value like ......
Read more >
Java.lang.Integer Class - Tutorialspoint
The java.lang.Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field...
Read more >
Class java.lang.Integer - Washington
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field...
Read more >
Can not set java.lang.Integer field to java.lang.Integer
What happens if you change your HQL query to from UserPattern where user.id = :user_id and pattern.id = :pattern_id ?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found