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

MatchesParam - Add new "OR" like syntax #4116

Closed
lucasamoroso opened this issue Feb 24, 2022 · 1 comment · Fixed by #4138
Closed

MatchesParam - Add new "OR" like syntax #4116

lucasamoroso opened this issue Feb 24, 2022 · 1 comment · Fixed by #4138

Comments

@lucasamoroso
Copy link

Hi!

Given the following routes:

  @Get("/hello")
  @MatchesParam("with_status!=true")
  @MatchesParam("!with_status")
  def hello(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello!")

  @Get("/hello")
  @MatchesParam("with_status=true")
  @MatchesParam("with_status")
  def helloWithStatus(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello! My status is ok")

I'd like to call the helloWithStatus route in the following ways:

  • curl http://localhost:8000/hello\?with_status\=true // This works
  • curl http://localhost:8000/hello\?with_status // This doesn't work, returns a NOT_FOUND

The only way to make this work as expected is with the following code:

  @Get("/hello")
  @MatchesParam("with_status=false")
  def hello(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello!")

  @Get("/hello")
  @MatchesParam("!with_status")
  def hello2(): HttpResponse = hello()

  @Get("/hello")
  @MatchesParam("with_status=true")
  def helloWithStatus(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello! My status is ok")

  @Get("/hello")
  @MatchesParam("with_status")
  def helloWithStatus2() = helloWithStatus()

This seems to be because @MatchesParam is applying a logical AND.

So, with all that said, @trustin proposed a new syntax like @MatchesParam("with_status=true || with_status") to make my first example work. This is the the code that will use this new syntax.

  @Get("/hello")
  @MatchesParam("with_status!=true || !with_status")
  def hello(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello!")

  @Get("/hello")
  @MatchesParam("with_status=true || with_status")
  def helloWithStatus(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello! My status is ok")

And this should be the result:

  • curl http://localhost:8000/hello\?with_status\=true // "Hello! My status is ok"
  • curl http://localhost:8000/hello\?with_status // "Hello! My status is ok"
  • curl http://localhost:8000/hello // "Hello!"
  • curl http://localhost:8000/hello\?with_status\=false // "Hello!"

Can we add this new syntax?

@trustin
Copy link
Member

trustin commented Feb 25, 2022

Thanks a lot for the detailed description, @lucasamoroso. LGTM 👍

ikhoon pushed a commit that referenced this issue Mar 24, 2022
Motivation:

Add more flexibility to RoutingPredicate with a new OR syntax

Modifications:

- RoutingPredicate  syntax

Result:

- Closes #4116. (If this resolves the issue.)
- You can now use `@MatchesParam` or `@MatchesHeader` with this new syntax. Example:
```scala
  @get("/hello")
  @MatchesParam("with_status!=true || !with_status")
  def hello(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello!")

  @get("/hello")
  @MatchesParam("with_status=true || with_status")
  def helloWithStatus(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello! My status is ok")
```

And this should be the result:
 - `curl http://localhost:8000/hello\?with_status\=true` // "Hello! My status is ok"
 - `curl http://localhost:8000/hello\?with_status` //  "Hello! My status is ok"
 - `curl http://localhost:8000/hello` // "Hello!"
 - `curl http://localhost:8000/hello\?with_status\=false` //  "Hello!"
heowc pushed a commit to heowc/armeria that referenced this issue Mar 25, 2022
Motivation:

Add more flexibility to RoutingPredicate with a new OR syntax

Modifications:

- RoutingPredicate  syntax

Result:

- Closes line#4116. (If this resolves the issue.)
- You can now use `@MatchesParam` or `@MatchesHeader` with this new syntax. Example:
```scala
  @get("/hello")
  @MatchesParam("with_status!=true || !with_status")
  def hello(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello!")

  @get("/hello")
  @MatchesParam("with_status=true || with_status")
  def helloWithStatus(): HttpResponse = HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT, "Hello! My status is ok")
```

And this should be the result:
 - `curl http://localhost:8000/hello\?with_status\=true` // "Hello! My status is ok"
 - `curl http://localhost:8000/hello\?with_status` //  "Hello! My status is ok"
 - `curl http://localhost:8000/hello` // "Hello!"
 - `curl http://localhost:8000/hello\?with_status\=false` //  "Hello!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants