为 java 客户端使用 openapi 生成器时,通用对象名称未序列化

问题描述

我使用 openapi generate version 5.0.0 生成一个 java 客户端,使用除模型包等之外的所有认值。

我让它在这样的 swagger 编辑器中正确呈现。

enter image description here

该组件的特定 yaml 如下所示。

components:
  schemas:
    WorkItem:
      properties:
        workitems:
          type: array
          items:
            properties:
              json:
                type: object
                additionalProperties:
                  type: string

使用客户端的示例代码如下所示。

    ApiKeyAuth ApiKeyAuth = (ApiKeyAuth) defaultClient.getAuthentication("ApiKeyAuth");
    ApiKeyAuth.setApiKey(result.getToken());

    WorkItem workItem = new WorkItem();

    Map map = new HashMap();
    map.put("test1","test1");
    map.put("test2","test2");
    map.put("test3","test3");

    workItem.addWorkitemsItem(map);


    apiInstance.addWorkItem("3","9",workItem);

然而,当我记录信息时,它显示请求正文中的有效负载看起来像这样。

{"workitems":[{"test2":"test2","test3":"test3","test1":"test1"}]}

而不是上面显示的招摇。我期待它渲染出对象名称在这种情况下,它是一个通用映射,而不是一个名为“json”的对象。我构建 openapi 的方式有什么问题吗?

如果我尝试在不使用其他属性的情况下使其更具体,那么在这种情况下,客户端仍然会使用通用对象。

解决方法

解决方案是使用单例映射而不是常规映射。

workItem.addWorkitemsItem(Collections.singletonMap("json",map));