spring-projects / spring-projects/spring-hateoas

Inconstant behavior for name/value property of @RequestParam

Open
#664 3 comments 2 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Nov 6, 2017.

process: waiting for feedback
Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

According to the documentation for @RequestParam the value property acts as an alias for the name property.

Unfortunately there is an inconstant behavior when generating links by using the name property.

Here is my dummy controller:

@Controller
public class MyController {

    @GetMapping("/foo")
    public ResponseEntity<String> methodWithRequestParamNamedByValue(@RequestParam(value="age") int a){
        return null;
    }

    @GetMapping("/foo")
    public ResponseEntity<String> methodWithRequestParamNamedByName(@RequestParam(name="age") int a){
        return null;
    }
}

And here is the test to this controller:

public class MyControllerTest {

    @Before
    public void setUp() {
        ServletRequestAttributes requestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
        RequestContextHolder.setRequestAttributes(requestAttributes);
    }


    @Test
    public void methodWithRequestParamNamedByValue() throws Exception {
        Link link = linkTo(methodOn(MyController.class).methodWithRequestParamNamedByValue(1)).withSelfRel();
        assertThat(link.getHref(), is("http://localhost/foo?age=1"));
    }


    // FAILING with "http://localhost/foo?a=1"
    @Test
    public void methodWithRequestParamNamedByName() throws Exception {
        Link link = linkTo(methodOn(MyController.class).methodWithRequestParamNamedByName(1)).withSelfRel();
        assertThat(link.getHref(), is("http://localhost/foo?age=1"));
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.