spring-projects / spring-projects/spring-hateoas

How to generate link with with optional hyphenated parameter?

Open
#1,168 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

I'm trying to implement an OGC API - Features service using Spring Boot. The specification includes links to self, parent collections and such, so I figured Spring HATEOAS could help with that. Unfortunately, I'm having trouble with the bbox-crs parameter.

An example demonstration:

@RestController
public class DemoController {

  @GetMapping(path = "/link", produces = "application/geo+json")
  public Link getLink(@RequestParam(required = false, name = "bbox-crs") String bboxCrs) {
    return linkTo(methodOn(DemoController.class).getLink(bboxCrs))
        .withSelfRel()
        .expand()
        .withType("application/geo+json");
  }

}

Test:

@SpringBootTest
@AutoConfigureMockMvc
class DemoApplicationTests {

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void getLinkWithParam() throws Exception {
    String expectedJson = new ObjectMapper().writeValueAsString(
        new Link("http://localhost/link?bbox-crs=hello").withType("application/geo+json"));

    mockMvc.perform(get("/link?bbox-crs=hello"))
        .andExpect(content().contentType("application/geo+json"))
        .andExpect(content().json(expectedJson));
  }

  @Test
  public void getLinkNoParam() throws Exception {
    String expectedJson = new ObjectMapper().writeValueAsString(
        new Link("http://localhost/link").withType("application/geo+json"));

    mockMvc.perform(get("/link"))
        .andExpect(content().contentType("application/geo+json"))
        .andExpect(content().json(expectedJson));
  }

}

The first test succeeds, but the second test fails with the error Illegal character in path at index 23: http://localhost/link{?bbox-crs}.

According to issue #799, this is working as intended, as the URI Template spec does not allow for hyphens in variable names. I'm trying to build an URI, though, not an URI Template. Is there some way to achieve what I'm after? Maybe by configuring Spring or by creating the link in a different way?

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.

Research direction

Start by reproducing the behavior in the DemoController entry point and the getLinkWithParam and getLinkNoParam tests in DemoApplicationTests. Trace how the optional bbox-crs parameter is expanded when it is absent, then verify that the generated link omits the parameter while retaining it when provided.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.