yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example or Tutorial?

awcchungster opened this issue · comments

Hi there,

I was able to generate the Swift files from my OpenAPI3 spec. However, I'm having a hard time importing the package into my Xcode workspace.

The steps I did are:

  1. Generate the package files
  2. Add local package in my existing project
  3. In my main code I tried added in variations of the "Example Request" code.

ValidatorPy is the package name I used when generating. Without even getting the models yet, both ValidatorPy and APIClient are not in scope "cannot find 'APIClient' in scope" error.

How should I be importing the Swaggen code?

let getUserRequest = ValidatorPy.User.GetUser.Request(id: 123)
let apiClient = APIClient.default

apiClient.makeRequest(getUserRequest) { apiResponse in
    switch apiResponse {
        case .result(let apiResponseValue):
        	if let user = apiResponseValue.success {
        		print("GetUser returned user \(user)")
        	} else {
        		print("GetUser returned \(apiResponseValue)")
        case .error(let apiError):
        	print("GetUser failed with \(apiError)")

There are two ways I tried importing the package.

  1. Dragging the package into my Xcode project
  2. Importing package dependency with these Apple Dev site steps

I also tried "import ValidatorPy" in addition the above steps.

@yonaskolb Could you help clarify please? I'd be happy to contribute this to documentation.

When I copy paste your example code, it doesn't work. I changed the request module path to my own generated route. After that it errors with wrong context.

In order to build, I have to change the apiResponse to apiResponse.result and also change .result/.error to .success/.failure. However, now the switch statement never finishes or is called.

I'm also a first time user of SwagGen and it works fine for me.
@awcchungster did you import your generated SPM before using it? (edit: yes you did)

import ValidatorPy

let apiClient = APIClient.default
  1. When you compile only your module, does it compile without errors?

  2. Did you link your module to your target where you are using it?

recording_2021-12-20 at 17 20 41

I moved off native iOS bindings for my API and use React Native. It was much easier to develop in.

ezoic increase your site revenue