openapi-generator-cli
A node package wrapper for https://github.com/OpenAPITools/openapi-generator
Top Related Projects
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
API Blueprint
📘 OpenAPI/Swagger-generated API Reference Documentation
Quick Overview
OpenAPITools/openapi-generator-cli is a command-line interface (CLI) tool for generating client libraries, server stubs, and documentation from OpenAPI (formerly Swagger) specifications. It supports a wide range of programming languages and frameworks, making it easier for developers to create API-first applications and maintain consistency between API definitions and implementations.
Pros
- Supports over 50 programming languages and frameworks
- Generates both client and server code from a single OpenAPI specification
- Customizable templates for fine-tuning generated code
- Active community and frequent updates
Cons
- Generated code may require manual tweaking for optimal performance
- Learning curve for advanced customization options
- Some generated code may not follow best practices for specific languages
- Large codebase can lead to longer build times
Getting Started
To use openapi-generator-cli, follow these steps:
- Install the tool using npm:
npm install @openapitools/openapi-generator-cli -g
- Generate code from an OpenAPI specification:
openapi-generator-cli generate -i path/to/openapi.yaml -g javascript -o ./output
This command generates JavaScript client code from the specified OpenAPI file and saves it in the ./output
directory.
- For more options and customization, refer to the official documentation:
openapi-generator-cli help generate
Competitor Comparisons
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
Pros of swagger-codegen
- More established project with a longer history
- Wider range of supported languages and frameworks
- Larger community and ecosystem of plugins
Cons of swagger-codegen
- Slower development cycle and release frequency
- Less active maintenance and fewer recent updates
- Some reported issues with generated code quality in certain languages
Code Comparison
swagger-codegen:
public class SwaggerCodegen {
public static void main(String[] args) {
SwaggerCodegen.main(new String[] { "generate", "-i", "swagger.yaml", "-l", "java" });
}
}
openapi-generator-cli:
public class OpenAPIGenerator {
public static void main(String[] args) {
OpenAPIGenerator.main(new String[] { "generate", "-i", "openapi.yaml", "-g", "java" });
}
}
Both tools offer similar functionality for generating client libraries, server stubs, and documentation from OpenAPI (formerly Swagger) specifications. However, openapi-generator-cli is generally considered more up-to-date and actively maintained, with faster release cycles and better support for newer OpenAPI versions. It also tends to produce higher quality code in many languages.
swagger-codegen, while still widely used, has seen a decline in development activity and community support. Some users have reported issues with generated code quality, especially for certain languages or complex API specifications.
Ultimately, the choice between these tools depends on specific project requirements, target languages, and the need for long-term support and updates.
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Pros of openapi-generator
- More comprehensive and feature-rich, offering a wider range of generators and customization options
- Supports a broader set of programming languages and frameworks
- Provides more advanced configuration capabilities for complex API specifications
Cons of openapi-generator
- Larger project size and potentially more complex setup process
- May have a steeper learning curve for new users due to its extensive features
- Requires more system resources for installation and execution
Code comparison
openapi-generator:
public class OpenAPIGenerator {
public static void main(String[] args) {
CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setInputSpec("path/to/openapi.yaml")
.setOutputDir("output");
DefaultGenerator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();
}
}
openapi-generator-cli:
openapi-generator-cli generate -i path/to/openapi.yaml -g java -o output
The openapi-generator provides more programmatic control and customization options, while openapi-generator-cli offers a simpler command-line interface for quick generation tasks. The choice between the two depends on the specific needs of the project and the level of customization required.
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
Pros of Prism
- Lightweight and fast mock server for API development and testing
- Supports dynamic responses and request validation
- Easy to set up and use with minimal configuration
Cons of Prism
- Limited code generation capabilities compared to openapi-generator-cli
- Fewer language and framework options for client/server code generation
- Less extensive community support and ecosystem
Code Comparison
Prism (starting a mock server):
prism mock api.yaml
openapi-generator-cli (generating client code):
openapi-generator-cli generate -i api.yaml -g python -o ./client
Key Differences
- Prism focuses on API mocking and validation, while openapi-generator-cli specializes in code generation
- openapi-generator-cli offers a wider range of language and framework support for code generation
- Prism provides a more streamlined experience for API prototyping and testing
Use Cases
- Choose Prism for quick API mocking, testing, and validation during development
- Opt for openapi-generator-cli when you need to generate client/server code in multiple languages
- Consider using both tools in combination for a comprehensive API development workflow
Community and Ecosystem
- openapi-generator-cli has a larger community and more extensive documentation
- Prism benefits from integration with other Stoplight tools and services
- Both projects are actively maintained and regularly updated
API Blueprint
Pros of api-blueprint
- Simpler syntax and more human-readable format
- Better support for documentation-first API design
- Easier to learn for non-technical stakeholders
Cons of api-blueprint
- Less widely adopted compared to OpenAPI Specification
- Fewer tools and integrations available
- Limited support for complex API structures
Code Comparison
api-blueprint:
# GET /message
+ Response 200 (text/plain)
Hello World!
openapi-generator-cli:
paths:
/message:
get:
responses:
'200':
description: Successful response
content:
text/plain:
schema:
type: string
example: Hello World!
Key Differences
- api-blueprint uses a Markdown-like syntax, while openapi-generator-cli uses YAML or JSON
- api-blueprint focuses on simplicity and readability, while openapi-generator-cli offers more detailed specifications
- openapi-generator-cli provides more extensive code generation capabilities across multiple languages and frameworks
- api-blueprint is better suited for smaller projects or rapid prototyping, while openapi-generator-cli is more appropriate for large-scale, complex API development
Use Cases
- Choose api-blueprint for quick API design and documentation, especially when collaborating with non-technical team members
- Opt for openapi-generator-cli when working on enterprise-level APIs, requiring extensive code generation, or integrating with a wide range of tools and services
📘 OpenAPI/Swagger-generated API Reference Documentation
Pros of Redoc
- Focuses on API documentation rendering with a sleek, interactive UI
- Easier to set up and use for quick API documentation needs
- Supports themes and customization for branding purposes
Cons of Redoc
- Limited to documentation generation; doesn't generate client libraries or server stubs
- Less flexibility in output formats compared to openapi-generator-cli
Code Comparison
Redoc (HTML-based rendering):
<!DOCTYPE html>
<html>
<head>
<title>API Documentation</title>
<meta charset="utf-8"/>
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
</head>
<body>
<redoc spec-url="https://petstore.swagger.io/v2/swagger.json"></redoc>
</body>
</html>
openapi-generator-cli (CLI-based generation):
openapi-generator-cli generate -i https://petstore.swagger.io/v2/swagger.json -g python -o ./generated-client
Summary
Redoc excels in creating visually appealing API documentation with minimal setup, making it ideal for projects prioritizing user-friendly documentation. openapi-generator-cli offers more comprehensive code generation capabilities, supporting multiple languages and output formats, but requires more configuration. Choose Redoc for quick, attractive API docs, and openapi-generator-cli for broader code generation needs across various platforms and languages.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
@openapitools/openapi-generator-cli
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported). Please see OpenAPITools/openapi-generator.
The OpenAPI Generator is a Java project. openapi-generator-cli
will download the appropriate JAR file and invoke the java
executable to run the OpenAPI Generator. You must have the java
binary executable available on your PATH
for this to work. (JDK 11 is the minimal version supported. To install OpenJDK, please visit https://adoptium.net/)
If you find this tool useful, please consider sponsoring this project financially via https://opencollective.com/openapi_generator or directly to Kay Schecker (the author of this tool) :pray:
Version 2.x.x
[update] The command has been renamed
You need to execute openapi-generator-cli
instead of openapi-generator
from now on.
[added] semver support! ð
To make that happen, a version management was added to the package.
The first time you run the command openapi-generator-cli
the last stable version
of OpenAPITools/openapi-generator is downloaded by default.
That version is saved in the file openapitools.json. Therefore you should include this file in your version control, to ensure that the correct version is being used next time you call the command.
If you would like to use a different version of the OpenAPITools/openapi-generator, you could change it by using one of the following commands:
openapi-generator-cli version-manager list
openapi-generator-cli version-manager set <versionTags...>
[added] generator config
You will now be able to configure the code generation in openapitools.json. This makes it more convenient to generate code for every file that matches the given glob expression. For more information, please check out the configuration documentation below.
Installation
Locally (recommended)
npm install @openapitools/openapi-generator-cli
or using yarn
yarn add @openapitools/openapi-generator-cli
After the installation has finished you can run npx openapi-generator-cli
or add a script like this:
{
"name": "my-cool-package",
"version": "0.0.0",
"scripts": {
"my-awesome-script-name": "openapi-generator-cli generate -i docs/openapi.yaml -g typescript-angular -o generated-sources/openapi --additional-properties=ngVersion=6.1.7,npmName=restClient,supportsES6=true,npmVersion=6.9.0,withInterfaces=true",
}
}
Note the whitespace sensitivity when using multiple additional-properties:
--additional-properties=ngVersion=6.1.7,npmName=restClient,supportsES6=true,npmVersion=6.9.0,withInterfaces=true
Globally
npm install -g @openapitools/openapi-generator-cli
or using yarn
yarn global add @openapitools/openapi-generator-cli
After the installation has finished you can run openapi-generator-cli
Usage
Mac/Linux:
openapi-generator-cli generate -g ruby -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /var/tmp/ruby-client
Windows:
openapi-generator-cli generate -g ruby -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o C:\temp\ruby-client
Configuration
If you have installed the package locally and executed the command openapi-generator-cli
at least once,
you will find a new file called openapitools.json along with the package.json. Please add this file to your VCS.
Initially the file has the following content:
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.8.0" // or the current latest version ;)
}
}
This configuration indicates the following:
- the json file shall be formatted using 2 spaces
- the jar files shall be downloaded to ./my/custom/storage/dir
- the generator-cli version 7.8.0 is used
Further it is also possible to configure generators, for example:
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.8.0",
"storageDir": "~/my/custom/storage/dir", // optional
"generators": { // optional
"v2.0": { // any name you like (just printed to the console log or reference it using --generator-key)
"generatorName": "typescript-angular",
"output": "#{cwd}/output/v2.0/#{ext}/#{name}",
"glob": "examples/v2.0/{json,yaml}/*.{json,yaml}",
"additionalProperties": {
"ngVersion": "6.1.7",
"npmName": "restClient",
"supportsES6": "true",
"npmVersion": "6.9.0",
"withInterfaces": true
}
},
"v3.0": { // any name you like (just printed to the console log or reference it using --generator-key)
"generatorName": "typescript-fetch",
"output": "#{cwd}/output/v3.0/#{ext}/#{name}",
"glob": "examples/v3.0/petstore.{json,yaml}"
}
}
}
}
If openapi-generator-cli generate
is called without further arguments, then the configuration
is automatically used to generate your code. ð
Available placeholders
placeholder | description | example |
---|---|---|
name | just file name | auth |
Name | just file name, but starting with a capital letter | Auth |
cwd | the current cwd | /Users/some-user/projects/some-project |
base | file name and extension | auth.yaml |
path | full path and filename | /Users/some-user/projects/some-project/docs/auth.yaml |
dir | path without the filename | /Users/some-user/projects/some-project/docs |
relDir | directory name of file relative to the glob provided | docs |
relPath | file name and extension of file relative to the glob provided | docs/auth.yaml |
ext | just file extension | yaml |
Using custom / private maven registry
If you're using a private maven registry you can configure the downloadUrl
and queryUrl
like this:
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.8.0",
"repository": {
"queryUrl": "https://private.maven.intern/solrsearch/select?q=g:${group.id}+AND+a:${artifact.id}&core=gav&start=0&rows=200",
"downloadUrl": "https://private.maven.intern/maven2/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar"
}
}
}
If the version
property param is set it is not necessary to configure the queryUrl
.
Use locally built JAR
In order to use a locally built jar of the generator CLI, you can copy the jar from your local build (i.e. if you were to build
the OpenAPITools/openapi-generator repository it would be in ~/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar
) into ./node_modules/@openapitools/openapi-generator-cli/versions/
and change the version
in the openapitools.json
file to the base name of the jar file.
E.g.:
cd openapi-generator
./mvnw clean package
cp ./modules/openapi-generator-cli/target/openapi-generator-cli.jar /your/project/node_modules/@openapitools/openapi-generator-cli/versions/my-local-snapshot.jar
and then:
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "my-local-snapshot",
}
}
Use nightly SNAPSHOT
build
Change your openapitools.json
to:
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.9.0-20240829.123431-22",
"repository": {
"downloadUrl": "https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.9.0-SNAPSHOT/openapi-generator-cli-${versionName}.jar"
}
}
}
Example is with a snapshot of 7.9.0
, please change the version
and downloadUrl
accordingly.
You can find all snapshots here.
Run specific generators
cmd | v3.0 runs | v2.0 runs |
---|---|---|
openapi-generator-cli generate --generator-key v3.0 | yes | no |
openapi-generator-cli generate --generator-key v3.0 v2.0 | yes | yes |
openapi-generator-cli generate --generator-key foo | no | no |
Use Docker instead of running java locally
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"useDocker": true
}
}
If useDocker
option is used, the spec file gets mounted to path /local/<your-spec-file-location>
within container. So, if you would configure spec file as
-i openapi/my-api.yml
if running locally, with useDocker option set, you would have to configure it like this: -i /local/openapi/my-api.yml
.
Custom Generators
Custom generators can be used by passing the --custom-generator=/my/custom-generator.jar
argument.
Further Documentation
Please refer to the official openapi-generator docs for more information about the possible arguments and a detailed usage manual of the command line interface.
Install previous version
npm install @openapitools/openapi-generator-cli@previous
npm i @openapitools/openapi-generator-cli@1.0.18-4.3.1
or using yarn
yarn add @openapitools/openapi-generator-cli@previous
yarn add @openapitools/openapi-generator-cli@1.0.18-4.3.1
You like the package?
Please leave a star.
Top Related Projects
swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
API Blueprint
📘 OpenAPI/Swagger-generated API Reference Documentation
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot