有没有办法阻止 Autorest.Powershell 扁平化响应对象? inlining-threshold 设置no-inline 指令低级变换

问题描述

我的 swagger.json 文件中有一个响应对象,其中包含一个嵌套对象作为其字段之一。当我使用 Autorest.Powershell 为这个 API 生成一个客户端时,它会展平嵌套的对象。所以当服务返回以下响应时:

{
  "code": 200,"status": "OK","data": {
    "FileName": "gameserver.zip","AssetUploadUrl": "https://example.com"
  }
}

我的 Autorest.Powershell 客户端返回一个扁平化的对象,如下所示:

{
  "code": 200,"dataFileName": "gameserver.zip","dataAssetUploadUrl": "https://example.com"
}

是否可以使用某种配置设置来禁用此行为?

以下是我的 swagger.json 文件的相关部分,如果有帮助的话:

"definitions": {
  "GetAssetUploadUrlResponse": {
    "type": "object","properties": {
      "AssetUploadUrl": {
        "description": "The asset's upload URL.","type": "string"
      },"FileName": {
        "description": "The asset's file name to get the upload URL for.","type": "string"
      }
    },"example": {
      "FileName": "gameserver.zip","AssetUploadUrl": "https://example.com"
    }
  }
},"responses": {
  "GetAssetUploadUrlResponse": {
    "description": "","schema": {
      "type": "object","properties": {
        "code": {
          "type": "integer","description": "The Http status code. If X-ReportErrorAsSuccess header is set to true,this will report the actual http error code."
        },"status": {
          "type": "string","description": "The Http status code as a string."
        },"data": {
          "$ref": "#/definitions/GetAssetUploadUrlResponse"
        }
      },"example": {
        "code": 200,"data": {
          "FileName": "gameserver.zip","AssetUploadUrl": "https://example.com"
        }
      }
    }
  }
}

解决方法

有几种方法,但没有一种是真正直接的(因为,我开始相信,大多数与 AutoRest 相关的事情都是如此;抱歉,无法抗拒:-P)。

有三种半官方方式。半官方在这里意味着它们基于公共 AutoRest 机制,但本身没有记录。作为半官方,它们可能只适用于某些版本的 AutoRest 组件,所以,这里是我使用的那些 (来自autorest --info):

  • @autorest/core (3.0.6369)
  • @autorest/modelerfour (4.15.414)
  • @autorest/powershell (3.0.421)

最后,以下是 AutoRest 代码库的相关部分:inline properties plug-inconfiguration directive definition

inlining-threshold 设置

此设置控制内部对象可以具有的最大属性数,以便将其视为符合内联条件。您可以在命令行或“literate config”.md 文件中进行设置。

```yaml
inlining-threshold: 0
```

理论上,将它设置为 0 应该可以防止任何内部成员的属性被内联,但是插件有一个硬编码异常,如果内部对象在本身名为 properties 的属性,则该限制将被忽略并且它仍然是扁平化的。

definitions:
  SomeSchema:
    type: "object"
    properties:
      detail_info: # <-- threshold honored
        $ref: "#/definitions/InfoSchema"
      properties: # <-- this is always flattened because of its special name
        $ref: "#/definitions/OtherSchema"

no-inline 指令

PowerShell AutoRest 插件还定义了一个自定义指令,用于指定不应内联某些架构。使用“文学配置”,就像

```yaml
directive:
- no-inline:
  - OtherSchema
  - ThirdSchema
```

这种方法的优点是 no-inline 指令覆盖了上面提到的“名为 properties 的属性中的始终内联属性”异常,因此它可以用来缓解问题。

缺点是应该明确列出所有模式名称。 (似乎指令也应该支持 Rx 名称表达式,但我无法让 no-inline: ".*" 工作)

低级变换

这种方法在所有情况下都无条件地禁用内联,但它与 AutoRest 使用的特定内部代码模型耦合。 (原则上,模型应该是稳定的,至少在主要版本中)。它还依赖于使用特定(非合同)属性的 PowerShell 插件来标记从内联中排除的架构。

```yaml
directive:
- from: code-model-v4-no-tags
  where: $.schemas.objects.*
  transform: |
    $.language.default['skip-inline'] = true;
```

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...