# Map 22+ query params to case class

**URL:** <https://softwaremill.community/t/map-22-query-params-to-case-class/301>\
**Category:** tapir\
**Created:** [October 24, 2023, 2:41pm UTC](https://softwaremill.community/t/map-22-query-params-to-case-class/301 "2023-10-24T14:41:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ravenow](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/ravenow/32/196_2.png) [@Ravenow](https://softwaremill.community/u/Ravenow)\
**Post date:** [October 24, 2023, 2:41pm UTC](https://softwaremill.community/t/map-22-query-params-to-case-class/301/1 "2023-10-24T14:41:28Z")

</div>

Hi, I have endpoint with 23+ query params, and of sake of convenience I`ve mapped them to case class.

```auto
    query[Option[String]]("q1")
      .and(query[Option[String]]("q2"))
      .and(query[Option[String]]("q3"))
      //....
    .and(query[Option[String]]("q22"))
    .mapTo[EndpointParams]

```

and it works just fine untill 23rd parameter in list. The error is following:

```auto
The arity of the source type doesn't match the arity of the target type

```

So my question is how to handle such cases(23+ query params mapped to case class)?

---

<div class="post-metadata">

**Author:** ![hochgi](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/hochgi/32/32_2.png) [@hochgi](https://softwaremill.community/u/hochgi)\
**Post date:** [October 25, 2023, 2:51am UTC](https://softwaremill.community/t/map-22-query-params-to-case-class/301/2 "2023-10-25T02:51:47Z")

</div>

This is a limitation in scala (2, in scala 3 [it could’ve worked](https://docs.scala-lang.org/scala3/reference/dropped-features/limit22.html), but tapir is cross built to scala 2 as well)

1. As a general rule of thumb, you probably want to avoid classes and function with high arity
2. You can group subsets of parameters, and compose, e.g. something like: `p1.and(p2).mapTo[CaseCls1].and(p3.and(p4).mapTo[CaseCls2]).mapTo[CaseCls3]`

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [October 31, 2023, 3:15pm UTC](https://softwaremill.community/t/map-22-query-params-to-case-class/301/3 "2023-10-31T15:15:17Z")

</div>

Yes, we have tuple operations defined only up to 22: [https://github.com/softwaremill/tapir/blob/462ca9dfa4ee78c7e8dd0c3422ef779f1e44968c/core/src/main/boilerplate-gen/sttp/tapir/typelevel/TupleOps.scala#L724](https://github.com/softwaremill/tapir/blob/462ca9dfa4ee78c7e8dd0c3422ef779f1e44968c/core/src/main/boilerplate-gen/sttp/tapir/typelevel/TupleOps.scala#L724)

As @hochgi mentioned, we could fix this using Scala 3, but so far this code is kept the same for Scala 2/ Scala 3. Not that we wouldn’t welcome a PR changing this, but so far nobody complained 🙂

---

<div class="post-metadata">

**Author:** ![lelik112](https://avatars.discourse-cdn.com/v4/letter/l/c4cdca/32.png) [@lelik112](https://softwaremill.community/u/lelik112)\
**Post date:** [July 25, 2025, 8:05am UTC](https://softwaremill.community/t/map-22-query-params-to-case-class/301/4 "2025-07-25T08:05:10Z")

</div>

```auto
trait FilterInput[F] {
  def input(filter: F): EndpointInput[F]
}

object FilterInput {

  def input[F](filter: F)(implicit input: FilterInput[F]): EndpointInput[F] = input.input(filter)

  implicit def filterInstance[T <: FilterType](implicit w: Witness.Aux[T]): FilterInput[Filter[T]] =
    _ =>
      w.value.input
        .asInstanceOf[EndpointInput[Option[T#Content]]] // scalafix:ok
        .map(Filter[T](_))(_.value)

  implicit def lastInstance[H](implicit lastInput: FilterInput[H]): FilterInput[H :: HNil] =
    (last: H :: HNil) => lastInput.input(last.head).map(_ :: HNil)(_.head)

  implicit def hListInstance[H, L <: HList](implicit
    headInput: FilterInput[H],
    tailInput: FilterInput[L]
  ): FilterInput[H :: L] = { case head :: tail =>
    (headInput.input(head) and tailInput.input(tail))
      .map(t => t._1 :: t._2) { case head :: tail => head -> tail }
  }
}

object SomewhereElse {
  final case class Filter[T <: FilterType](value: Option[T#Content]) extends AnyVal

  object Filter {
    def empty[T <: FilterType]: Filter[T] = Filter(None)
  }

  sealed trait FilterType extends EnumEntry {
    type Content
    def input: EndpointInput[Option[Content]]
    // other methods, for instance def filter(content: Content): Cat => Boolean
  }

  object FilterType extends Enum[FilterType] {
    case object Filter1 extends FilterType {
      override type Content = String
      override def input: EndpointInput[Option[String]] = query[Option[String]]("q1")
    }
    case object Filter2 extends FilterType {
      override type Content = String
      override def input: EndpointInput[Option[String]] = query[Option[String]]("q2")
    }
    override def values: IndexedSeq[FilterType] = findValues
  }

  final case class Params(
    filter1: Filter[Filter1.type] = Filter.empty,
    filter2: Filter[Filter2.type] = Filter.empty
  )

  object Params {
    private val Empty = Params()

    val Input: EndpointInput[Params] =
      FilterInput
        .input(Generic[Params].to(Empty))
        .map(Generic[Params].from(_))(Generic[Params].to(_))
  }
}

```
