0
votes

serialization issue in a field that can be of type int or of type List[String], it is a field that in each element is a list that always has 2 elements in the list the first element the value field is always a list [ String] and in the second element it is always of type int; the issue is in the value field and it fails to infer it because of this behavior.

body:

enter image description here

json response:

{
  "custom_fields": [
    {
      "id": "d0c016df-e09a-492e-a7a2-cc92e1993627",
      "name": "Sprint",
      "type": "labels",
      "date_created": "1586927852599",
      "hide_from_guests": false,
      "value": [
        "64e19188-8440-4b35-8459-d49348c92e55"
      ],
      "required": false
    },
    {
      "id": "4513e77f-9866-4139-bcc5-458945db0deb",
      "name": "Story Points",
      "type": "drop_down",
      "date_created": "1579556449403",
      "hide_from_guests": false,
      "value": 1,
      "required": false
    }
  ]
}

issue on field:

value

scala cire code serializer:

  implicit val decodeCustomFields: Decoder[CustomFieldsItem] = new Decoder[CustomFieldsItem] {
    final def apply(c: HCursor): Decoder.Result[CustomFieldsItem] =
      for {
        id <- c.downField("id").as[Option[String]]
        name <- c.downField("name").as[Option[String]]
        typeF <- c.downField("type").as[Option[String]]
        date_created <- c.downField("date_created").as[Option[String]]
        hide_from_guests <- c.downField("hide_from_guests").as[Option[Boolean]]
        value <- c.downField("value").as[Option[List[String]]] // here it fails !!
        required <- c.downField("required").as[Option[Boolean]]
      } yield {
        CustomFieldsItem( id, name, typeF, date_created, hide_from_guests, value , required )
      }
  }

scala unmarshal code for http response, it takes the circe serializer :

  private def runRequest(req: HttpRequest): Future[CustomFieldsItem] =
    Http()
      .singleRequest(req)
      .flatMap {
        res =>
          val ser = Unmarshal(res).to[CustomFieldsItem]
          ser
      }
  • message error on unmarshal:

    An error has occurred: C[A]: DownField(value),MoveRight,DownArray,DownField(custom_fields),DownArray,DownField(tasks)

How could I improve or fix this serialization?

1

1 Answers

-1
votes

Make CustomFields be of type (CustomField[List[String]], CustomField[Int]) with a generic typed CustomField case class ?

The issue is that your modelisation doesn't reflect the reality of what you're receiving, and you're trying to fix that afterwards. If it always has 2 elements, it's not a list, it's a Tuple.