Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

It is impossible to reject malformed optional form fields with anyParams #1125

@at-nap

Description

@at-nap

When using anyParams, it is impossible to reject malformed optional form fields; it unconditionally falls back to the query param, which is missing but optional, and therefor valid.

I.e:

  // Passes as expected
  "anyParam directive" should "reject malformed optional query parameters" in {
    Post("/?foo=bar", FormData(Seq.empty)) ~> anyParams('foo.as[Option[Int]]) {
      foo => complete(s"foo: $foo")
    } ~> check {
      rejections.head shouldBe a [MalformedQueryParamRejection]
    }
  }

  // Fails!
  it should "reject malformed optional form fields" in {
    Post("/", FormData(Seq("foo" -> "bar"))) ~> anyParams('foo.as[Option[Int]]) {
      foo => complete(s"foo: $foo")
    } ~> check {
      rejections.head shouldBe a [MalformedFormFieldRejection]
    }
  }

The second test fails:

...
[info] - should reject malformed optional form fields *** FAILED *** (8 milliseconds)
[info]   Request was not rejected, response was HttpResponse(200 OK,HttpEntity(text/plain; charset=UTF-8,foo: None),List(),HTTP/1.1)
...

This behaviour is unexpected. It seems anyParams ought to fall back to query params only on missing form fields. (Or anyParams ought to be renamed any*Valid*Params, if you get my drift.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions