swiftopenapi-generator

Apple's Swift OpenAPI Generator generates too verbose code


This is an example of the all-to verbose code Apple's Swift OpenAPI generator creates in my package.

    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
                )

so the call side also looks clunky:

let response = try await client.get_sol_myGroups etc.

The json source is perfectly normal openapi stuff and the regular OpenAPI generator creates perfectly sane code. There should be no sol's and no Operations etc. in the generated code, like in Apple's documentation. Is this a configuration issue?

My config:

generate:
  - types
  - client
accessModifier: public

Part of the source:

  "paths": {
    "/myGroups": {
      "get": {
        "tags": [
          "MyGroups"
        ],
        "summary": "Fetch myGroups collection",
        "responses": {
          "200": {
            "description": "ok",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/myGroupsCollectionResponse"
                }
              }
            }
          }
        }
      },

Solution

  • This is by design, see https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/project-scope-and-goals

    The name of the method can be customized by specifying the operationId in the OpenAPI document: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#fixed-fields-8

    The namespaces like Operations and Components are required, as the OpenAPI document can have entities named the same way in different namespaces (for example, both a schema called Foo and a response called Foo). Without the namespaces, a valid OpenAPI document could generated invalid Swift code, which would go against the principles of this generator.