Comparison guide: OpenAPI/Swagger Go client generation
Speakeasy generates idiomatic Go SDKs based on OpenAPI specifications.
In this post, we’ll take a look at why many of our users choose to switch from OpenAPI Generate and other open-source generators to Speakeasy to generate their Go SDKs.
Open-source SDK generators play an important role in experimentation and smaller custom integrations but teams should publish high-quality SDKs for their APIs that offer the best developer experience. Usable SDKs drive adoption by making it easier for developers to switch to the API.
At Speakeasy, we generate idiomatic client SDKs in a variety of languages. Our generators follow principles that ensure we generate SDKs that offer the best developer experience so that you can focus on building your API, and your developer-users can focus on delighting their users.
Below is the summary of how the four evaluated generators compare. Our recommendation is to use Speakeasy for generating Go SDKs for your APIs (1st SDK free). If you are committed to using an open source generator, we strongly recommend that you use oapi-codegen
Feature
Version Support
Speakeasy
✅ Go 1.14+
OpenAPI Generator
⚠️ Variable support
oapi-codegen
✅ Good compatibility
ogen
⚠️ Newer Go versions
Dependencies
Speakeasy
✅ 3 deps
OpenAPI Generator
❌ 1500+ deps, requires Java
oapi-codegen
✅ No external deps
ogen
✅ No external deps
Go Idiomaticity
Speakeasy
✅ Native Go patterns
OpenAPI Generator
❌ Non-idiomatic
oapi-codegen
✅ Simple patterns
ogen
⚠️ Custom patterns
Nil-safe Getters
Speakeasy
✅ Built-in safety
OpenAPI Generator
❌ No safety
oapi-codegen
❌ No safety
ogen
❌ Custom optionals
Union Types
Speakeasy
✅ Full support
OpenAPI Generator
❌ No support
oapi-codegen
⚠️ Limited support
ogen
✅ Full support
Enums
Speakeasy
✅ Type-safe
OpenAPI Generator
❌ Strings only
oapi-codegen
✅ Type-safe
ogen
✅ Type-safe
Complex Numbers
Speakeasy
✅ Big decimal
OpenAPI Generator
❌ Basic types
oapi-codegen
❌ Basic types
ogen
❌ Basic types
Error Handling
Speakeasy
✅ Custom types
OpenAPI Generator
⚠️ Generic errors
oapi-codegen
⚠️ Basic errors
ogen
✅ Good errors
Retries
Speakeasy
✅ Built-in
OpenAPI Generator
❌ No support
oapi-codegen
❌ No support
ogen
❌ No support
Pagination
Speakeasy
✅ Built-in
OpenAPI Generator
⚠️ Manual only
oapi-codegen
⚠️ Manual only
ogen
⚠️ Manual only
Documentation
Speakeasy
✅ Complete docs
OpenAPI Generator
⚠️ Basic docs
oapi-codegen
❌ Minimal docs
ogen
⚠️ Basic docs
CI/CD Integration
Speakeasy
✅ GitHub Actions
OpenAPI Generator
❌ Manual only
oapi-codegen
❌ Manual only
ogen
❌ Manual only
Feature
Speakeasy
OpenAPI Generator
oapi-codegen
ogen
Version Support
✅ Go 1.14+
⚠️ Variable support
✅ Good compatibility
⚠️ Newer Go versions
Dependencies
✅ 3 deps
❌ 1500+ deps, requires Java
✅ No external deps
✅ No external deps
Go Idiomaticity
✅ Native Go patterns
❌ Non-idiomatic
✅ Simple patterns
⚠️ Custom patterns
Nil-safe Getters
✅ Built-in safety
❌ No safety
❌ No safety
❌ Custom optionals
Union Types
✅ Full support
❌ No support
⚠️ Limited support
✅ Full support
Enums
✅ Type-safe
❌ Strings only
✅ Type-safe
✅ Type-safe
Complex Numbers
✅ Big decimal
❌ Basic types
❌ Basic types
❌ Basic types
Error Handling
✅ Custom types
⚠️ Generic errors
⚠️ Basic errors
✅ Good errors
Retries
✅ Built-in
❌ No support
❌ No support
❌ No support
Pagination
✅ Built-in
⚠️ Manual only
⚠️ Manual only
⚠️ Manual only
Documentation
✅ Complete docs
⚠️ Basic docs
❌ Minimal docs
⚠️ Basic docs
CI/CD Integration
✅ GitHub Actions
❌ Manual only
❌ Manual only
❌ Manual only
If you want the detailed technical breakdown, full of code comparisons, read on!
Installing SDK generators
To start our comparison, we installed all four generators on a local machine running macOS.
Installing the OpenAPI Generator CLI
OpenAPI Generator depends on Java, which we covered at length previously. We concluded that managing the OpenAPI Generator Java dependencies manually just wasn’t worth the effort.
Installing openapi-generator using Homebrew installs openjdk@11 and its many dependencies:
This adds the openapi-generator CLI to our path.
Installing oapi-codegen
We install oapi-codegen using the Go package manager:
This command installs the oapi-codegen Go module and its dependencies .
Next, authenticate with Speakeasy by running the following:
This installs the Speakeasy CLI as a single binary without any dependencies.
Downloading the Swagger Petstore specification
Before we run our generators, we’ll need an OpenAPI specification to generate a Go SDK for. The standard specification for testing OpenAPI SDK generators and Swagger UI generators is the Swagger Petstore .
Both the OpenAPI Generator and Speakeasy CLI can validate an OpenAPI spec. We’ll run both and compare the output.
Validation using OpenAPI Generator
To validate petstore.yaml using OpenAPI Generator, run the following in the terminal:
The OpenAPI Generator returns two warnings:
Validation using Speakeasy
We’ll validate the spec with Speakeasy by running the following in the terminal:
The Speakeasy validator returns ten warnings, seven hints that some methods don’t specify any return values and three unused components. Each warning includes a detailed JSON-formatted error with line numbers.
Since both validators validated the spec with only warnings, we can assume that both will generate SDKs without issues.
Generating an SDK
Now that we know our OpenAPI spec is valid, we can start generating SDKs.
We’ll generate each SDK in a unique subdirectory, relative to where we saved the petstore.yaml spec file.
OpenAPI generate
OpenAPI Generator features SDK generators for multiple languages, often with multiple options per language. We’ll test the Go generator in this post.
We’ll generate an SDK by running the following in the terminal:
This command will print a list of files generated and populate the new petstore-sdk-go-openapi directory.
Generating an SDK using oapi-codegen
Before we generate an SDK using oapi-codegen, we’ll need to create a new directory for this SDK.
Run the following in the terminal:
Create a Go module in the new directory:
Then run the oapi-codegen Go module:
Generating an SDK using ogen
We followed the ogen quick start documentation.
Create a new directory for our ogen SDK and navigate to it in the terminal:
Create a new Go module in this directory:
Copy the petstore.yaml spec into this directory:
Create a new file called generate.go with the following contents:
Then run the following from our new directory:
In our testing, this resulted in a stack trace and returned an error status:
The function isParamAllowed in gen/gen_parameters.go on line 184 throws the error that nested objects are not allowed in form parameters. This seems to indicate that ogen does not yet support generating an SDK for a form request that takes nested objects as parameters, such as a pet’s tags, when updating a pet in our schema.
The ogen documentation references a spec to download, so we’ll replace petstore.yaml with their spec by running the following:
With this new simplified spec, we’ll try the generator again:
The generator runs without errors and prints a log line:
This log line seems to indicate that some operations don’t return a default response.
Speakeasy generate
Finally, we’ll generate an SDK using the Speakeasy CLI.
This command prints a log of warnings and information, then completes successfully.
SDK code compared: Package structure
We now have four different Go SDKs for the Petstore API:
./petstore-sdk-go-openapi/, generated by OpenAPI Generator.
./petstore-sdk-go-oapi-codegen/, generated by oapi-codegen.
./petstore-sdk-go-ogen/, generated by ogen.
./petstore-sdk-go-speakeasy/, generated by Speakeasy.
We’ll start our comparison by looking at the structure of each generated SDK.
Let’s print a tree structure for each SDK’s directory. Run tree in the terminal from our root directory:
We’ll split the output by directory for each SDK below.
OpenAPI Generator SDK structure
oapi-codegen creates only one file for all generated code, with no tests or documentation outside this file. This generator appears to be better suited to generating a small and specific client or server as part of a larger project, rather than generating a usable SDK that can be packaged for users.
ogen SDK structure
ogen also generates relatively few files, which does not seem to be because this generation was based on a simpler spec. This generator does not seem to split schemas into different files and does not create any tests or documentation.
Speakeasy SDK structure
Speakeasy generates a clear file structure, split into directories for models, types, and other utils. It also creates documentation, split by models and endpoints.
Models
Let’s compare how a pet is represented in each of the four SDKs:
OpenAPI Generator pet model
OpenAPI Generator does not seem to take the spec’s enum for pet status when generating the pet model. Status in this model is a pointer to a string, while other generators create a type to validate the status field. This model includes struct tags for JSON only.
oapi-codegen pet model
The oapi-codegen pet model is similar to the OpenAPI Generator version, but it makes the Tags field a pointer to a slice of Tag, making it possible for this field to be nil (which would be omitted from the JSON due to omitempty).
The Status field is not a simple string pointer, but a pointer to PetStatus, which provides better type safety, since PetStatus is a type alias for string with specific allowable values.
ogen pet model
The pet model generated by ogen lacks the Tags and Category fields because these fields are not present in the simplified spec used.
This struct uses a different approach to optional fields. It uses OptInt64 and OptPetStatus types. We’ll look at how ogen differs from Speakeasy in terms of nullable fields below.
Speakeasy pet model
This struct is similar to the OpenAPI Generator version but includes additional form struct tags, which are likely used to specify how these fields should be encoded and decoded when used in form data (such as in an HTTP POST request).
Like the oapi-codegen version, Status is a *PetStatus rather than a *string.
Nullable fields
Let’s focus on the difference between how ogen and Speakeasy handle the nullable Status field.
Here’s the relevant code generated by ogen:
While much safer than the OpenAPI Generator’s pointer to a string type, the ogen OptPetStatus is not idiomatic and provides no benefit over using pointers, as Speakeasy does:
The Speakeasy approach provides the same strong typing as the ogen version. It defines PetStatus as a custom string type and defines allowable values as constants. This practice ensures that you can’t accidentally set a PetStatus to an invalid value.
The way Speakeasy handles the PetStatus type is more idiomatic to Go, which generally favors simplicity and readability. Instead of defining a new struct like OptPetStatus, Speakeasy uses a built-in language feature (pointers) to achieve the same effect. This approach is simpler, more consistent with the rest of the language, and easier to understand and use.
SDK dependencies
The ogen and oapi-codegen SDKs don’t add any dependencies to the generated modules, so we’ll compare dependencies between OpenAPI Generator and Speakeasy SDKs.
We’ll run the following for each of these two SDKs:
For Speakeasy, this command prints the following:
The output for the OpenAPI Generator version is too long to show here, so we’ll do a count instead:
Speakeasy purposefully generates SDKs with fewer dependencies, which leads to faster installs, reduced build times, and less exposure to potential security vulnerabilities.
To see why the Speakeasy SDK depends on an exponential backoff module, let’s discuss retries.
Retries
The SDK generated by Speakeasy can automatically retry failed network requests or retry requests based on specific error responses, providing a straightforward developer experience for error handling.
To enable this feature, we need to use the Speakeasy x-speakeasy-retries extension to the OpenAPI spec. We’ll update the OpenAPI spec to add retries to the addPet operation as a test.
Edit petstore.yaml and add the following to the addPet operation:
Add this snippet to the operation:
Now we’ll rerun the Speakeasy generator to enable retries for failed network requests when creating a new pet. It is also possible to enable retries for the SDK as a whole by adding global x-speakeasy-retries at the root of the OpenAPI spec.
Generated documentation
Both Speakeasy and OpenAPI generate documentation for the generated code.
Each generator creates a README.md file at the base directory of the generated SDK. This file serves as a primary source of documentation for the SDK users. You have the option to customize this README using Speakeasy to suit your needs better. For example, you could add your brand’s logo, provide links for support, outline a code of conduct, or include any other information that could be useful to the developers using the SDK.
The Speakeasy-generated documentation really shines when it comes to usage examples, which include working usage examples for all operations, complete with imports and appropriately formatted string examples. For instance, if a string is formatted as email in our OpenAPI spec, Speakeasy generates usage examples with strings that look like email addresses. Types formatted as uri will generate examples that look like URLs. This makes example code clear and scannable.
We’ll test this by adding format: uri to the items in the photoUrls array. Let’s compare the generated example code for the addPet endpoint after adding this string format.
Usage example generated by OpenAPI
Here’s the example from the OpenAPI-generated documentation:
Note how the OpenAPI example only includes required fields and ignores the URI string format from our spec.
Usage example generated by Speakeasy
This is what Speakeasy generates as a usage example:
The example generated by Speakeasy includes all available fields and correctly formats the example string in the PhotoUrls field.
We’ll also compare how OpenAPI and Speakeasy generate documentation for the Status field in our pet model.
OpenAPI generate does not document enums
The OpenAPI-generated documentation reflects the generated code’s omission of valid options for the Status field. Here’s the pet model documentation generated by OpenAPI:
In the example above, PetStatus links to the following documentation:
PetStatus values generated by Speakeasy
Name
Value
available
Value
pending
Value
sold
Name
Value
available
pending
sold
Automation
This comparison focuses on the installation and usage of command line generators, but the Speakeasy generator can also run as part of a CI workflow, for instance as a GitHub Action , to make sure your SDK is always up to date when your API spec changes.
Summary
We’ve seen how Speakeasy generates lightweight, idiomatic SDKs for Go.
If you’re building an API that developers rely on and would like to publish full-featured Go SDKs that follow best practices, we strongly recommend giving the Speakeasy SDK generator a try.
Join our Slack community to let us know how we can improve our Go SDK generator or to suggest features.