Apple 的 Swift OpenAPI 生成器生成过于冗长的代码

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

这是 Apple 的 Swift OpenAPI 生成器在我的包中创建的详细代码的示例。

    public func get_sol_myGroups(_ input: Operations.get_sol_myGroups.Input) async throws -> Operations.get_sol_myGroups.Output {
        try await client.send(
            input: input,
            forOperation: Operations.get_sol_myGroups.id,
            serializer: { input in
                let path = try converter.renderedPath(
                    template: "/myGroups",
                    parameters: []
                )
                var request: HTTPTypes.HTTPRequest = .init(
                    soar_path: path,
                    method: .get
                )
                suppressMutabilityWarning(&request)
                converter.setAcceptHeader(
                    in: &request.headerFields,
                    contentTypes: input.headers.accept
                )

所以调用方看起来也很笨拙:

let response = try await client.get_sol_myGroups etc.

json 源是完全正常的 openapi 内容,常规 OpenAPI 生成器创建完全正常的代码。生成的代码中不应有 sol 和 Operations 等内容,就像 Apple 的文档中那样。这是配置问题吗?

我的配置:

generate:
  - types
  - client
accessModifier: public

部分来源:

  "paths": {
    "/myGroups": {
      "get": {
        "tags": [
          "MyGroups"
        ],
        "summary": "Fetch myGroups collection",
        "responses": {
          "200": {
            "description": "ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/myGroupsCollectionResponse"
                }
              }
            }
          }
        }
      },
swift openapi-generator
1个回答
0
投票

这是设计使然,请参阅 https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/project-scope-and-goals

可以通过指定OpenAPI文档中的

operationId
来自定义方法名称:https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields -8

Operations
Components
这样的命名空间是必需的,因为 OpenAPI 文档可以在不同的命名空间中具有以相同方式命名的实体(例如,名为
Foo
的模式和名为
Foo
的响应)。如果没有命名空间,有效的 OpenAPI 文档可能会生成无效的 Swift 代码,这将违背此生成器的原则。

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