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

RepresentationModelProcessor nullpointer having mandatory field in controller spring 2.7.4 #1872

Open
csigmanek opened this issue Nov 9, 2022 · 2 comments
Assignees
Labels

Comments

@csigmanek
Copy link

csigmanek commented Nov 9, 2022

Hello,

I have a ResourceProcessor that's adding links to a resource:

linkTo(methodOn(MyController.class)
  .findMyElements(file.getId(), null, null))
  .withRel("filterFiles"))

The controller looks like this:

@RequestMapping(method = { POST, GET }, value = "/myFiles/{fileId}/filterFiles")
  public List<FileDto> findFilterFiles(
      @PathVariable("fileId") final Long fileId,
      @RequestParam(name = "fileType") final Long fileType,
      @RequestBody(required = false) final FileFilterDto filter) { … }

In Spring 2.6.4, it used to work. Now, I've tried upgrading to 2.7.4, and it does not work anymore. I've tracked the issue down, and it seems like the required request parameter must not be null anymore.

I'm getting a null pointer exception with text like this:
Illegal character in query at index 93: http://localhost:8080/files/10013/filterFiles?fileType={fileType}
with the index pointing to '=' of fileType={fileType} .

Is it a bug? If yes - how can I fix it? Passing a constant number fixes the null-pointer exception:

linkTo(methodOn(MyController.class)
  .findMyElements(file.getId(), 1L, null))
  .withRel("filterFiles"))

but it leads to incorrect code.

@csigmanek csigmanek changed the title Links with variable parameters cannot be created RepresentationModelProcessor nullpointer having mandatory field in controller spring 2.7.4 Nov 11, 2022
@csigmanek
Copy link
Author

@odrotbohm
Copy link
Member

If I add some test code like this:

linkTo(methodOn(PersonControllerImpl.class).findFilterFiles(4711L, null, null)).withRel("foo").getHref()

I get the following output:

http://localhost/people/myFiles/4711/filterFiles?fileType={fileType}

The exception you show here seems to indicate that some code is trying to turn that String into a URI, which won't work as the curly braces demarcating the template variables are not valid characters in a URI. You will need to properly expand the URI template by providing a value for fileType to end up with a valid URI. Note that you won't be able to expand the template without a value for fileType as your controller method declares the parameter required which renders the template variable in non-optional syntax (?fileType} would be the optional one, but you'd have to set the required attribute on @RequestParam to false or turn the parameter type into an Optional<Long>.

@odrotbohm odrotbohm self-assigned this Jan 12, 2023
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

2 participants