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

OperationParameterReader does not process optional path parameters correctly #2220

Closed
raphw opened this issue Jan 31, 2018 · 1 comment
Closed
Labels
Milestone

Comments

@raphw
Copy link

raphw commented Jan 31, 2018

When a Spring controller offers an optional parameter such as in:

@RequestMapping({"/foo", "/foo/{bar}"})
public void foo(@PathVariable(value = "bar", required = false) String bar) {
  if (bar == null) { ... } else { ... }
}

Springfox will correctly create two mappings for /foo and /foo/{bar} but define the bar parameter as required for both mappings. I think the problem lies in the OperationParameterReader which should detect that the parameter for bar should be ignored for the mapping of /foo.

I think that a solution could be as simple as adding a condition to the OperationParameterReader:shouldIgnore method that looks something similar to:

if (parameter.hasParameterAnnotation(PathVariable.class)
  && !parameter.getAnnotation(PathVariable.class).required()
  && !context.requestMappingPattern().contains("{" + methodParameter.defaultName() + "}") {
    return true;
}

This way any parameter would be suppressed that is:

  1. Representing a path variable
  2. Is not required
  3. Is not part of the mapped path.
@raphw raphw changed the title ParameterRequiredReader does not process optional path parameters correctly OperationParameterReader does not process optional path parameters correctly Jan 31, 2018
@dilipkrish dilipkrish added the bug label Feb 2, 2018
@dilipkrish
Copy link
Member

There is an underlying assumption that PathVariables are always required. Thanks for catching this!

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