Swagger Codegen Maven插件,带有路径变量和请求参数,OpenApi产生不起作用的代码

问题描述 投票:0回答:1

我正面临着摇摇欲坠的代码生成的存根的问题。我有2个服务。首先使用路径var和请求参数的两种方法公开REST api:

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

    @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);
    }

有两个端点具有相同的URL,但参数不同。 Spring-web框架与此配合工作。接下来,我生成OpenApi json并获取2条路径:

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

然后,我试图用swagger-codegen-maven-plugin存根生成该端点,然后我遇到了问题。

线程“主”中的异常java.lang.IllegalArgumentException:映射没有'?login'的值

这种形式的URL被硬编码在生成的类中。

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

由于uriVariables中缺少登录映射键值而引发异常。

spring swagger swagger-codegen swagger-codegen-maven-plugin
1个回答
0
投票

您应谨慎使用定义文件(.json或.yml)来为参数定义正确的类型,因为有两种参数:

  1. 路径参数(GET / users / {id})
  2. 查询参数(GET / user / findByLogin?name = myUserLogin)

这两个在OpenAPI中有两个不同的声明

1)路径参数

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)查询参数

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

有关更多详细信息,请检查official docuementation

© www.soinside.com 2019 - 2024. All rights reserved.