Skip to content

Default Enum value is always used instead of the one passed in request #2

Closed

Description

@dbronecki

I'm using enum as parameter with default value. Default value is always used instead of the one I passed to request.

Test case:

import org.scalatest.{Matchers, WordSpec}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import akka.http.scaladsl.server._
import Directives._
import enumeratum.{Enum, EnumEntry}

sealed trait SortOrder extends EnumEntry
object SortOrder extends Enum[SortOrder] {
  case object Asc extends SortOrder
  case object Desc extends SortOrder

  override val values = findValues
}

class KebsTest extends WordSpec with Matchers with ScalatestRouteTest {

  import pl.iterators.kebs.unmarshallers._
  import enums._

  val smallRoute =
    get {
      path("test_enum") {
        parameter('sort.as[SortOrder] ? SortOrder.Desc) { sort =>
          complete {
            s"Sort was $sort"
          }
        }
      }
    }

  "Kebs" should {

    "work with default enum values" in {
      Get("/test_enum?sort=Asc") ~> smallRoute ~> check {
        responseAs[String] shouldBe "Sort was Asc"
      }
    }
  }
}

Activity

  1. changed the title [-]Enums with default value are ommited from request[/-] [+]Default Enum value is always used instead of the one passed in request[/+] on May 18, 2017
  2. marcin-rzeznicki commented on May 22, 2017

    @marcin-rzeznicki
    Contributor

    Thank you for the analysis. So, as you discovered, it is because the ? method changes type of NameReceptacle (it is defined as def ?[B](default: B) = new NameDefaultReceptacle(name, default)) - so, ong story short - the Unmarshaller that is materialized is FromStringUnmarshaller[SortOrder.Desc.type] - provided by kebs for case objects. The unmarshaller for case object ignores its argument and returns constant object - which is the direct cause for the bug. Thanks @dbronecki for analysis! I fixed that in 71ad0b2 by removing case objects' unmarshallers - it doesn't look like a feature that is needed. I think that it was worth sacrificing this unneeded feature for less surprising behavior. Let me know wdyt @dbronecki !

  3. dbronecki commented on May 23, 2017

    @dbronecki
    ContributorAuthor

    Yeah, it's better now - no surprising behaviour. I have no idea how to fix it otherwise, so for now we can mark this issue as resolved.

  4. added a commit that references this issue on Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions