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

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

Closed
dbronecki opened this issue May 18, 2017 · 2 comments
Closed
Labels

Comments

@dbronecki
Copy link
Contributor

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"
      }
    }
  }
}
@dbronecki dbronecki changed the title Enums with default value are ommited from request Default Enum value is always used instead of the one passed in request May 18, 2017
@luksow luksow added the bug label May 19, 2017
@marcin-rzeznicki
Copy link
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 !

@dbronecki
Copy link
Contributor Author

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.

pk044 pushed a commit that referenced this issue Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants