Skip to content

Generate types for the path string #668

Open
@atacan

Description

@atacan

Motivation

paths:
  /greet:
    get:
      operationId: getGreeting
  /count:
    get:
      operationId: getCount

The actual path of the endpoints that we define is only used in the comment or documentation of the operations. It would be useful to have the string of the path, /greet and /count in the above example, available in the Swift code as well.

One use case that I encountered was when I was making a request to a third-party API. I provide a webhook URL, meaning when the third party finishes, they will call back my endpoint that I defined in my OpenAPI YAML file for the webhook requests. In this case, I need to pass this path to this third-party provider. It would be nice to have it type-safe from changes in the OpenAPI path namings.

Proposed solution

For the simple paths this can be straightforward a constant in the Operations enum. But for the paths that have parameters in them, we might use a simple function with path parameters as its arguments.

public enum Operations {
    public enum greet {
       public static let path = "/greet"
// ...
    }
}

If we have a yaml like

paths:
  /users/{userId}:
    get:
      operationId: getUser

then

public enum Operations {
    public enum getUser {
       public static let path: @Sendable (String) -> String = { "/users/\($0)" }
// ...
    }
}

we can make use of the type of the path parameter

paths:
  /users/{id}:
    get:
      operationId: getUser
      parameters:
        - in: path
          name: id
          required: true
          schema:
            type: integer
            minimum: 1
public enum Operations {
    public enum getUser {
       public static let path: @Sendable (Int) -> String = { "/users/\($0)" }
// ...
    }
}

Alternatives considered

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureNew feature.status/triageCollecting information required to triage the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions