Search code examples
springswaggerswagger-codegenswagger-codegen-maven-plugin

Swagger codegen maven plugin with path variables and request params OpenApi produces not working code


I'm facing a problem with swagger codegen generatered stubs. I have 2 services. First exposes REST api with two methods with path var and request param:

    @GetMapping(value = "/start/{pathVar}/operators", params = "login")
    public Operator getOperatorByLogin(
                @ApiParam @PathVariable Long pathVar, 
                @ApiParam(required = true) @RequestParam  String login) {
        return operatorRepository.findDistinctByLogin(login);
    }

and

    @GetMapping(value = "/start/{pathVar}/operators", params = "ids")
    public List<Operator> getOperatorsByIds(
                @ApiParam @PathVariable Long pathVar, 
                @ApiParam( allowMultiple = true) @RequestParam List<Long> ids) {
        return operatorRepository.findAllByOperatorIdIn(ids);
    }

There are 2 endpoints with same URLs but diffrent params. Spring-web framework works with that. Next, I genereate OpenApi json and get 2 paths:

"/start/{pathVar}/operators{?ids}": ...
"/start/{pathVar}/operators{?login}": ...

Then, Im trying to generate with swagger-codegen-maven-plugin stubs for that endpoints and then I'm facing the problem.

Exception in thread "main" java.lang.IllegalArgumentException: Map has no value for '?login'

URLs in that form are hardcoded in generated classes.

(...)
final Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("pathVar", pathVar);
String path = UriComponentsBuilder.fromPath(
"/start/{pathVar}/opeartors{?login}").buildAndExpand(uriVariables).toUriString();
(...)

throws exception because of lacking of login map key-value in uriVariables.


Solution

  • Your should be careful on your definition file (.json or .yml) to define the correct type for your paramaters as there is two kind of params :

    1. Path parameter (GET /users/{id})
    2. Query parameter (GET /user/findByLogin?name=myUserLogin)

    Those two have two differents declaration in OpenAPI

    1) Path parameter

    paths:
      /users/{id}:
        get:
          parameters:
            - in: path
              name: id   # Note the name is the same as in the path
              required: true
              schema:
                type: integer
                minimum: 1
              description: The user ID
    

    2) Query parameter

    parameters:
        - in: query
          name: myUserLogin
          schema:
            type: integer
            description: The number of items to skip before starting to collect the result set
    

    For more detailed check the official docuementation