Skip to content

Multiple Parameter Refs throws IllegalStateException (Duplicate key) #408

@ssmtlbn

Description

@ssmtlbn

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
      ));
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions