Skip to content

Instantly share code, notes, and snippets.

@philippelatulippe
Created November 17, 2016 10:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save philippelatulippe/f40539600934819a731a8b1f0bf61b4e to your computer and use it in GitHub Desktop.
Save philippelatulippe/f40539600934819a731a8b1f0bf61b4e to your computer and use it in GitHub Desktop.
How to generate Swift 3 client library with swagger-codegen

How to generate Swift 3 code with swagger-codegen

  1. Clone the swagger-codegen repo, checkout the branch "2.3.0"

  2. mvn clean package

  3. Create a shell script to make it easier to run your version of swagger-codegen:

    /usr/local/bin/swagger-codegen-2.3.0 #!/bin/bash exec java -jar ~/Code/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar "$@"

  4. Navigate to your swagger specification

  5. Create a config file for swagger-codegen. It will specify the name and metadata of the generated library. I called mine swagger-codegen.config.json

     {
       "podSummary": "PetstoreClient",
       "podHomepage": "https://github.com/petshopboys/PetstoreClient-swift",
       "podAuthors": "Pet Shop Boys",
       "projectName": "PetstoreClient"
     }
    
  6. Generate your code. Put it in its own repository and put it in the root of your xcode project. I will not judge you.

     swagger-codegen-2.3.0 generate -l swift3 -i swagger.yaml -c swagger-codegen.config.json -o ~/Code/PetstoreClient
    
  7. Add an entry to your project's Podfile to add your client library to your project.

     pod "PetstoreClient", :path => "./PetstoreClient/"
    

    Change the path parameter to point to the directory where the generated code was output. The path in the example above works when the code has been placed in the root directory of the project.

TODO

  1. I thought the Podfile path would be "./". How come I had to specify the directory?

  2. What about the tests? Something about setting inherit_paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment