Skip to content

Multiple Parameter Refs throws IllegalStateException (Duplicate key) #408

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

Closed
ssmtlbn opened this issue Feb 6, 2020 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@ssmtlbn
Copy link

ssmtlbn commented Feb 6, 2020

It is currently not possible to define multiple @parameter's (on a single method) that reference a definition.
E. g. something like that:

@GetMapping(path = "a", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(
    parameters = {
        @Parameter(ref = "#/components/parameters/paramA"),
        @Parameter(ref = "#/components/parameters/paramB")
    }
)
public void test() {}

The method org.springdoc.core.AbstractRequestBuilder#getParameterLinkedHashMap throws the following Exception:

Caused by: java.lang.IllegalStateException: Duplicate key class Parameter {
   name: null
   in: null
   description: null
   required: null
   deprecated: null
   allowEmptyValue: null
   style: null
   explode: null
   allowReserved: null
   schema: null
   examples: null
   example: null
   content: null
   $ref: #/components/parameters/paramA
}	

Thats because in this method the parameter name is used as key, but both parmeters have a null name.
It can be be fixed by using the parameters hash code as key instead of the name, e. g like:

private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes,
    List<Parameter> operationParameters, Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
  LinkedHashMap<String, Parameter> map = operationParameters.stream()
      .collect(Collectors.toMap(
          parameter -> Integer.toString(parameter.hashCode()), // was Parameter::getName before
          parameter -> parameter,
          (u, v) -> {
            throw new IllegalStateException(String.format("Duplicate key %s", u));
          },
          LinkedHashMap::new
      ));
...
@bnasslahsen
Copy link
Collaborator

Hi @ssmtlbn,

Yeeah, good catch.
The fix will be available on v1.2.31;

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants