This project is a simple example of how OpenAPI Generator can be used with a Node.js and Express.js API.
The main purpose of this project is to understand how OpenAPI Generator works.
The project demonstrates:
Express API
↓
OpenAPI Specification
↓
OpenAPI Generator
↓
Generated JavaScript Client
OpenAPI is a specification used to describe REST APIs.
It describes:
- API endpoints
- HTTP methods
- Parameters
- Request body
- Response
- Data models
- API information
For example:
GET /users
POST /users
GET /users/{id}
These APIs are described inside:
openapi/openapi.yaml
The OpenAPI file works like a blueprint or contract for the API.
OpenAPI Generator is a tool that reads an OpenAPI specification and generates code from it.
In this project, we use the JavaScript generator.
The basic idea is:
openapi.yaml
↓
OpenAPI Generator
↓
JavaScript Generator
↓
Generated JavaScript Client
Instead of manually writing repetitive API client code, the OpenAPI specification can be used to generate that code automatically.
The purpose of this project is to understand:
- How to create a simple Express API.
- How to describe that API using OpenAPI.
- How to validate the OpenAPI specification.
- How OpenAPI Generator reads the specification.
- How the JavaScript generator is selected.
- How templates are used to generate JavaScript code.
- Where the generated files are created.
Before running the generator:
openapi-generator-demo/
│
├── server/
│ └── server.js
│
├── openapi/
│ └── openapi.yaml
│
├── package.json
└── package-lock.json
After running the JavaScript generator:
openapi-generator-demo/
│
├── server/
│ └── server.js
│
├── openapi/
│ └── openapi.yaml
│
├── generated/
│ └── Generated JavaScript Files
│
├── package.json
└── package-lock.json
The
generatedfolder is created by OpenAPI Generator. It does not need to be created manually.
The Express server is located at:
server/server.js
Example:
const express = require("express");
const app = express();
app.use(express.json());
// GET all users
app.get("/users", (req, res) => {
const users = [
{
id: 1,
name: "Srinivas",
email: "srinivas@example.com"
},
{
id: 2,
name: "Ravi",
email: "ravi@example.com"
}
];
res.json(users);
});
// GET user by ID
app.get("/users/:id", (req, res) => {
const id = Number(req.params.id);
const user = {
id: id,
name: "Srinivas",
email: "srinivas@example.com"
};
res.json(user);
});
// Create user
app.post("/users", (req, res) => {
const user = {
id: 3,
name: req.body.name,
email: req.body.email
};
res.status(201).json(user);
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});Run:
node .\server\server.jsExpected output:
Server running on http://localhost:3000
Now open:
http://localhost:3000/users
The API should return the users.
Create:
openapi/openapi.yaml
The OpenAPI file describes the APIs created in the Express server.
Example:
openapi: 3.0.3
info:
title: User API
version: 1.0.0
description: Simple User Management API
servers:
- url: http://localhost:3000
paths:
/users:
get:
operationId: getUsers
summary: Get all users
responses:
"200":
description: Successfully retrieved users
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/User"
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
email:
type: stringBefore generating code, validate the OpenAPI file.
Run:
npx @openapitools/openapi-generator-cli validate -i openapi/openapi.yamlThis checks whether the OpenAPI specification is valid.
The flow is:
openapi.yaml
↓
Validator
↓
Valid OpenAPI Specification
Run:
npx @openapitools/openapi-generator-cli generate -i openapi/openapi.yaml -g javascript -o generatedThe command contains three important options.
-i openapi/openapi.yaml
Means:
Use
openapi.yamlas the input.
-g javascript
Means:
Use the JavaScript generator.
-o generated
Means:
Put the generated files inside the
generateddirectory.
The internal process can be understood as:
openapi.yaml
│
▼
Read / Parse
│
▼
Understand API Structure
│
▼
Internal API Model
│
▼
Select JavaScript Generator
│
▼
Templates
│
▼
Insert API Information
│
▼
Generate JavaScript
│
▼
generated/
From the OpenAPI file, the generator understands:
/users:It understands:
Path = /users
get:It understands:
Method = GET
operationId: getUsersIt understands:
Operation = getUsers
For:
/users/{id}
it understands that id is a path parameter.
For:
"200":it understands that the successful response uses HTTP status 200.
For:
User:it understands that User is a data model.
OpenAPI Generator uses templates to create the output.
Conceptually, a template may contain:
function {{operationId}}() {
// API request
}
The OpenAPI specification provides:
operationId = getUsers
The generator combines the API information with the template.
Conceptually:
Template
+
OpenAPI Information
↓
Generated JavaScript
The actual generated files contain more complete code depending on the selected generator.
After running:
npx @openapitools/openapi-generator-cli generate -i openapi/openapi.yaml -g javascript -o generatedOpenAPI Generator creates the output directory.
The generated structure can vary depending on the generator and version, but it will contain JavaScript client files and supporting files.
For example:
generated/
│
├── src/
│ ├── api/
│ ├── model/
│ └── ...
│
├── package.json
├── README.md
└── ...
There are two different things in this project.
server/server.js
This contains the actual API implementation.
It tells the application:
HOW the API works.
openapi/openapi.yaml
This describes:
WHAT the API looks like.
It describes:
- Endpoint
- Method
- Parameters
- Request
- Response
- Models
Express Server
│
▼
REST API
│
API Description
│
▼
openapi.yaml
│
▼
OpenAPI Generator
│
▼
JavaScript Generator
│
▼
Templates
│
▼
Generated JavaScript
│
▼
generated/
npm install expressnpm install @openapitools/openapi-generator-cli -Djava -versionnpx @openapitools/openapi-generator-cli versionnode .\server\server.jsnpx @openapitools/openapi-generator-cli validate -i openapi/openapi.yamlnpx @openapitools/openapi-generator-cli generate -i openapi/openapi.yaml -g javascript -o generatedOpenAPI Generator uses Java internally.
Therefore, Java must be installed even though this project generates JavaScript.
The flow is:
Java
↓
Runs OpenAPI Generator
↓
JavaScript Generator
↓
JavaScript Output
Java is only required to run the generator. The generated application/client is JavaScript.
Without OpenAPI Generator:
Developer
↓
Manually write API client
↓
Manually write models
↓
Manually maintain API methods
With OpenAPI Generator:
OpenAPI Specification
↓
OpenAPI Generator
↓
Generated JavaScript Client
This can reduce repetitive development work and keep generated API client code aligned with the API specification.
The main concept of this project is:
OpenAPI = API Blueprint
OpenAPI Generator = Code Generation Tool
JavaScript Generator = Generates JavaScript
Generated Folder = Generated Output
In one sentence:
OpenAPI Generator reads the API definition from
openapi.yaml, converts the API information into an internal model, applies the selected JavaScript generator and its templates, and generates JavaScript client files automatically.
This project demonstrates:
- Creating a REST API using Express.js
- Creating an OpenAPI specification
- Validating an OpenAPI specification
- Installing OpenAPI Generator
- Selecting the JavaScript generator
- Generating JavaScript client code
- Understanding the generator and template process
- Understanding the difference between an API implementation and an API specification
- OpenAPI Generator: https://openapi-generator.tech/
- Installation: https://openapi-generator.tech/docs/installation
- Usage: https://openapi-generator.tech/docs/usage
- Generators: https://openapi-generator.tech/docs/generators
- Templating: https://openapi-generator.tech/docs/templating