@eqxjs/swagger-codegen 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +230 -0
- package/FORMATS.md +180 -0
- package/OPENAPI-COMPARISON.md +355 -0
- package/QUICKSTART.md +118 -0
- package/README.md +405 -0
- package/SUMMARY.md +184 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +49 -0
- package/dist/cli.js.map +1 -0
- package/dist/file-writer.d.ts +24 -0
- package/dist/file-writer.d.ts.map +1 -0
- package/dist/file-writer.js +95 -0
- package/dist/file-writer.js.map +1 -0
- package/dist/generator.d.ts +6 -0
- package/dist/generator.d.ts.map +1 -0
- package/dist/generator.js +104 -0
- package/dist/generator.js.map +1 -0
- package/dist/generators/client-service.generator.d.ts +6 -0
- package/dist/generators/client-service.generator.d.ts.map +1 -0
- package/dist/generators/client-service.generator.js +203 -0
- package/dist/generators/client-service.generator.js.map +1 -0
- package/dist/generators/controller.generator.d.ts +6 -0
- package/dist/generators/controller.generator.d.ts.map +1 -0
- package/dist/generators/controller.generator.js +241 -0
- package/dist/generators/controller.generator.js.map +1 -0
- package/dist/generators/dto.generator.d.ts +10 -0
- package/dist/generators/dto.generator.d.ts.map +1 -0
- package/dist/generators/dto.generator.js +221 -0
- package/dist/generators/dto.generator.js.map +1 -0
- package/dist/generators/module.generator.d.ts +10 -0
- package/dist/generators/module.generator.d.ts.map +1 -0
- package/dist/generators/module.generator.js +52 -0
- package/dist/generators/module.generator.js.map +1 -0
- package/dist/generators/service.generator.d.ts +6 -0
- package/dist/generators/service.generator.d.ts.map +1 -0
- package/dist/generators/service.generator.js +179 -0
- package/dist/generators/service.generator.js.map +1 -0
- package/dist/parser.d.ts +24 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +82 -0
- package/dist/parser.js.map +1 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +38 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +114 -0
- package/dist/utils.js.map +1 -0
- package/package.json +40 -0
- package/test-all-formats.sh +32 -0
- package/test.sh +27 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
# OpenAPI 3.0 vs Swagger 2.0 Examples
|
|
2
|
+
|
|
3
|
+
This document compares the key differences between Swagger 2.0 and OpenAPI 3.0 specifications as demonstrated in the example files.
|
|
4
|
+
|
|
5
|
+
## Quick Reference
|
|
6
|
+
|
|
7
|
+
| Feature | Swagger 2.0 | OpenAPI 3.0 |
|
|
8
|
+
|---------|-------------|-------------|
|
|
9
|
+
| Version Field | `swagger: "2.0"` | `openapi: "3.0.0"` |
|
|
10
|
+
| Schema Location | `definitions` | `components.schemas` |
|
|
11
|
+
| Request Body | `parameters` with `in: body` | `requestBody` object |
|
|
12
|
+
| Response Content | Direct `schema` | `content` > media type > `schema` |
|
|
13
|
+
| Servers | `host`, `basePath`, `schemes` | `servers` array |
|
|
14
|
+
| Example Files | `example-swagger.json/yaml` | `openapi3-example.json/yaml` |
|
|
15
|
+
|
|
16
|
+
## Detailed Differences
|
|
17
|
+
|
|
18
|
+
### 1. API Metadata
|
|
19
|
+
|
|
20
|
+
**Swagger 2.0:**
|
|
21
|
+
```yaml
|
|
22
|
+
swagger: '2.0'
|
|
23
|
+
host: api.example.com
|
|
24
|
+
basePath: /api/v1
|
|
25
|
+
schemes:
|
|
26
|
+
- https
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**OpenAPI 3.0:**
|
|
30
|
+
```yaml
|
|
31
|
+
openapi: 3.0.0
|
|
32
|
+
servers:
|
|
33
|
+
- url: https://api.example.com/v2
|
|
34
|
+
description: Production server
|
|
35
|
+
- url: https://staging.example.com/v2
|
|
36
|
+
description: Staging server
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Schema Definitions
|
|
40
|
+
|
|
41
|
+
**Swagger 2.0:**
|
|
42
|
+
```yaml
|
|
43
|
+
definitions:
|
|
44
|
+
User:
|
|
45
|
+
type: object
|
|
46
|
+
properties:
|
|
47
|
+
id:
|
|
48
|
+
type: string
|
|
49
|
+
required:
|
|
50
|
+
- id
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**OpenAPI 3.0:**
|
|
54
|
+
```yaml
|
|
55
|
+
components:
|
|
56
|
+
schemas:
|
|
57
|
+
Product:
|
|
58
|
+
type: object
|
|
59
|
+
properties:
|
|
60
|
+
id:
|
|
61
|
+
type: string
|
|
62
|
+
required:
|
|
63
|
+
- id
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 3. Request Body
|
|
67
|
+
|
|
68
|
+
**Swagger 2.0:**
|
|
69
|
+
```yaml
|
|
70
|
+
post:
|
|
71
|
+
parameters:
|
|
72
|
+
- in: body
|
|
73
|
+
name: body
|
|
74
|
+
required: true
|
|
75
|
+
schema:
|
|
76
|
+
$ref: '#/definitions/CreateUserDto'
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**OpenAPI 3.0:**
|
|
80
|
+
```yaml
|
|
81
|
+
post:
|
|
82
|
+
requestBody:
|
|
83
|
+
required: true
|
|
84
|
+
description: Product information
|
|
85
|
+
content:
|
|
86
|
+
application/json:
|
|
87
|
+
schema:
|
|
88
|
+
$ref: '#/components/schemas/CreateProductDto'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 4. Response Definition
|
|
92
|
+
|
|
93
|
+
**Swagger 2.0:**
|
|
94
|
+
```yaml
|
|
95
|
+
responses:
|
|
96
|
+
'200':
|
|
97
|
+
description: Successful response
|
|
98
|
+
schema:
|
|
99
|
+
type: array
|
|
100
|
+
items:
|
|
101
|
+
$ref: '#/definitions/User'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**OpenAPI 3.0:**
|
|
105
|
+
```yaml
|
|
106
|
+
responses:
|
|
107
|
+
'200':
|
|
108
|
+
description: Successful response
|
|
109
|
+
content:
|
|
110
|
+
application/json:
|
|
111
|
+
schema:
|
|
112
|
+
type: array
|
|
113
|
+
items:
|
|
114
|
+
$ref: '#/components/schemas/Product'
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### 5. Query Parameters
|
|
118
|
+
|
|
119
|
+
**Swagger 2.0:**
|
|
120
|
+
```yaml
|
|
121
|
+
parameters:
|
|
122
|
+
- name: page
|
|
123
|
+
in: query
|
|
124
|
+
type: integer
|
|
125
|
+
required: false
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
**OpenAPI 3.0:**
|
|
129
|
+
```yaml
|
|
130
|
+
parameters:
|
|
131
|
+
- name: page
|
|
132
|
+
in: query
|
|
133
|
+
required: false
|
|
134
|
+
schema:
|
|
135
|
+
type: integer
|
|
136
|
+
default: 1
|
|
137
|
+
minimum: 1
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 6. Path Parameters
|
|
141
|
+
|
|
142
|
+
**Swagger 2.0:**
|
|
143
|
+
```yaml
|
|
144
|
+
parameters:
|
|
145
|
+
- name: id
|
|
146
|
+
in: path
|
|
147
|
+
required: true
|
|
148
|
+
type: string
|
|
149
|
+
description: User ID
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**OpenAPI 3.0:**
|
|
153
|
+
```yaml
|
|
154
|
+
parameters:
|
|
155
|
+
- name: id
|
|
156
|
+
in: path
|
|
157
|
+
required: true
|
|
158
|
+
description: Product ID
|
|
159
|
+
schema:
|
|
160
|
+
type: string
|
|
161
|
+
format: uuid
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Code Generation Output
|
|
165
|
+
|
|
166
|
+
Both specifications generate the same NestJS structure:
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
generated/
|
|
170
|
+
├── dtos/ # DTOs with @ApiProperty decorators
|
|
171
|
+
├── products/ # Product feature module
|
|
172
|
+
│ ├── products.service.ts
|
|
173
|
+
│ ├── products.controller.ts
|
|
174
|
+
│ └── products.module.ts
|
|
175
|
+
├── categories/ # Category feature module
|
|
176
|
+
│ ├── categories.service.ts
|
|
177
|
+
│ ├── categories.controller.ts
|
|
178
|
+
│ └── categories.module.ts
|
|
179
|
+
├── app.module.ts # Root module
|
|
180
|
+
└── index.ts # Barrel exports
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Note:** Service, controller, and module for each resource are grouped together by tag name.
|
|
184
|
+
|
|
185
|
+
### Example DTO Output
|
|
186
|
+
|
|
187
|
+
Both generate similar TypeScript classes:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { ApiProperty } from '@nestjs/swagger';
|
|
191
|
+
|
|
192
|
+
export class Product {
|
|
193
|
+
@ApiProperty({ description: 'Unique product identifier', type: String })
|
|
194
|
+
id: string;
|
|
195
|
+
|
|
196
|
+
@ApiProperty({ description: 'Product name', type: String })
|
|
197
|
+
name: string;
|
|
198
|
+
|
|
199
|
+
@ApiProperty({ description: 'Product price in USD', type: Number })
|
|
200
|
+
price: number;
|
|
201
|
+
|
|
202
|
+
@ApiProperty({ description: 'Product tags', isArray: true })
|
|
203
|
+
tags?: string[];
|
|
204
|
+
|
|
205
|
+
@ApiProperty({
|
|
206
|
+
description: 'Product status',
|
|
207
|
+
enum: ['active', 'inactive', 'discontinued']
|
|
208
|
+
})
|
|
209
|
+
status?: string;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### Example Controller Output
|
|
214
|
+
|
|
215
|
+
Both generate controllers with proper decorators:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
@ApiTags('products')
|
|
219
|
+
@Controller('products')
|
|
220
|
+
export class ProductsController {
|
|
221
|
+
constructor(private readonly productsService: ProductsService) {}
|
|
222
|
+
|
|
223
|
+
@ApiOperation({ summary: 'List all products' })
|
|
224
|
+
@Get('')
|
|
225
|
+
async listProducts(
|
|
226
|
+
@Query('page') page: string,
|
|
227
|
+
@Query('limit') limit: string
|
|
228
|
+
): Promise<any> {
|
|
229
|
+
return this.productsService.listProducts(page, limit);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
@ApiOperation({ summary: 'Create a new product' })
|
|
233
|
+
@Post('')
|
|
234
|
+
async createProduct(@Body() body: CreateProductDto): Promise<any> {
|
|
235
|
+
return this.productsService.createProduct(body);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Testing the Examples
|
|
241
|
+
|
|
242
|
+
### Swagger 2.0
|
|
243
|
+
```bash
|
|
244
|
+
# JSON
|
|
245
|
+
npm run dev -- generate -i ./example-swagger.json -o ./generated
|
|
246
|
+
|
|
247
|
+
# YAML
|
|
248
|
+
npm run dev -- generate -i ./example-swagger.yaml -o ./generated
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
**Generated:**
|
|
252
|
+
- 5 DTOs (User, CreateUserDto, UpdateUserDto, Post, CreatePostDto)
|
|
253
|
+
- 2 Services (UsersService, PostsService)
|
|
254
|
+
- 2 Controllers (UsersController, PostsController)
|
|
255
|
+
- 2 Modules (UsersModule, PostsModule)
|
|
256
|
+
|
|
257
|
+
### OpenAPI 3.0
|
|
258
|
+
```bash
|
|
259
|
+
# JSON
|
|
260
|
+
npm run dev -- generate -i ./openapi3-example.json -o ./generated
|
|
261
|
+
|
|
262
|
+
# YAML
|
|
263
|
+
npm run dev -- generate -i ./openapi3-example.yaml -o ./generated
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
**Generated:**
|
|
267
|
+
- 6 DTOs (Product, CreateProductDto, UpdateProductDto, Category, CreateCategoryDto, Error)
|
|
268
|
+
- 2 Services (ProductsService, CategoriesService)
|
|
269
|
+
- 2 Controllers (ProductsController, CategoriesController)
|
|
270
|
+
- 2 Modules (ProductsModule, CategoriesModule)
|
|
271
|
+
|
|
272
|
+
## Advanced Features in OpenAPI 3.0
|
|
273
|
+
|
|
274
|
+
The OpenAPI 3.0 examples demonstrate additional features:
|
|
275
|
+
|
|
276
|
+
### 1. Multiple Servers
|
|
277
|
+
Define multiple deployment environments:
|
|
278
|
+
```yaml
|
|
279
|
+
servers:
|
|
280
|
+
- url: https://api.example.com/v2
|
|
281
|
+
description: Production server
|
|
282
|
+
- url: https://staging.example.com/v2
|
|
283
|
+
description: Staging server
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### 2. Enhanced Parameter Validation
|
|
287
|
+
```yaml
|
|
288
|
+
schema:
|
|
289
|
+
type: integer
|
|
290
|
+
minimum: 1
|
|
291
|
+
maximum: 100
|
|
292
|
+
default: 10
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 3. Content Negotiation
|
|
296
|
+
```yaml
|
|
297
|
+
content:
|
|
298
|
+
application/json:
|
|
299
|
+
schema: {...}
|
|
300
|
+
application/xml:
|
|
301
|
+
schema: {...}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### 4. Reusable Error Schemas
|
|
305
|
+
```yaml
|
|
306
|
+
components:
|
|
307
|
+
schemas:
|
|
308
|
+
Error:
|
|
309
|
+
type: object
|
|
310
|
+
required:
|
|
311
|
+
- code
|
|
312
|
+
- message
|
|
313
|
+
properties:
|
|
314
|
+
code:
|
|
315
|
+
type: string
|
|
316
|
+
message:
|
|
317
|
+
type: string
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Migration from Swagger 2.0 to OpenAPI 3.0
|
|
321
|
+
|
|
322
|
+
This tool supports both versions seamlessly, but if you're migrating:
|
|
323
|
+
|
|
324
|
+
### Key Changes to Make:
|
|
325
|
+
|
|
326
|
+
1. **Update version field:**
|
|
327
|
+
- Change `swagger: "2.0"` to `openapi: "3.0.0"`
|
|
328
|
+
|
|
329
|
+
2. **Move schemas:**
|
|
330
|
+
- Move `definitions` to `components.schemas`
|
|
331
|
+
|
|
332
|
+
3. **Update request bodies:**
|
|
333
|
+
- Replace body parameters with `requestBody` object
|
|
334
|
+
|
|
335
|
+
4. **Update responses:**
|
|
336
|
+
- Wrap response schemas in `content` object with media types
|
|
337
|
+
|
|
338
|
+
5. **Update servers:**
|
|
339
|
+
- Replace `host`, `basePath`, `schemes` with `servers` array
|
|
340
|
+
|
|
341
|
+
6. **Update parameter schemas:**
|
|
342
|
+
- Wrap parameter types in `schema` object
|
|
343
|
+
|
|
344
|
+
## Recommendation
|
|
345
|
+
|
|
346
|
+
- **Use Swagger 2.0** if you're maintaining legacy APIs or need broader tool support
|
|
347
|
+
- **Use OpenAPI 3.0** for new projects to take advantage of:
|
|
348
|
+
- Better content negotiation
|
|
349
|
+
- Clearer request/response definitions
|
|
350
|
+
- Multiple server support
|
|
351
|
+
- Enhanced validation options
|
|
352
|
+
- Callback definitions
|
|
353
|
+
- Link definitions
|
|
354
|
+
|
|
355
|
+
Both versions generate **identical NestJS code** with this tool! 🎉
|
package/QUICKSTART.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Quick Start Guide
|
|
2
|
+
|
|
3
|
+
## Available Example Files
|
|
4
|
+
|
|
5
|
+
### Swagger 2.0 Examples
|
|
6
|
+
- **`example-swagger.json`** (8.1KB) - Users & Posts API
|
|
7
|
+
- **`example-swagger.yaml`** (5.8KB) - Users & Posts API
|
|
8
|
+
- 5 DTOs, 2 Services, 2 Controllers, 2 Modules
|
|
9
|
+
|
|
10
|
+
### OpenAPI 3.0 Examples
|
|
11
|
+
- **`openapi3-example.json`** (14KB) - Products & Categories API
|
|
12
|
+
- **`openapi3-example.yaml`** (10KB) - Products & Categories API
|
|
13
|
+
- 6 DTOs, 2 Services, 2 Controllers, 2 Modules
|
|
14
|
+
|
|
15
|
+
## Quick Commands
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Install & Build
|
|
19
|
+
npm install
|
|
20
|
+
npm run build
|
|
21
|
+
|
|
22
|
+
# Test a Single Format
|
|
23
|
+
npm run dev -- generate -i ./example-swagger.json -o ./generated
|
|
24
|
+
|
|
25
|
+
# Test All Formats
|
|
26
|
+
npm run test:all
|
|
27
|
+
|
|
28
|
+
# Clean Generated Files
|
|
29
|
+
npm run clean
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Generate Code
|
|
33
|
+
|
|
34
|
+
Choose any example file:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# Swagger 2.0 - JSON
|
|
38
|
+
npm run dev -- generate -i ./example-swagger.json -o ./output
|
|
39
|
+
|
|
40
|
+
# Swagger 2.0 - YAML (28% smaller than JSON)
|
|
41
|
+
npm run dev -- generate -i ./example-swagger.yaml -o ./output
|
|
42
|
+
|
|
43
|
+
# OpenAPI 3.0 - JSON
|
|
44
|
+
npm run dev -- generate -i ./openapi3-example.json -o ./output
|
|
45
|
+
|
|
46
|
+
# OpenAPI 3.0 - YAML (29% smaller than JSON)
|
|
47
|
+
npm run dev -- generate -i ./openapi3-example.yaml -o ./output
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Generated Structure
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
output/
|
|
54
|
+
├── dtos/
|
|
55
|
+
│ ├── *.dto.ts # DTOs with @ApiProperty
|
|
56
|
+
├── products/
|
|
57
|
+
│ ├── products.service.ts # Injectable service
|
|
58
|
+
│ ├── products.controller.ts # Controller with routes
|
|
59
|
+
│ └── products.module.ts # Feature module
|
|
60
|
+
├── categories/
|
|
61
|
+
│ ├── categories.service.ts
|
|
62
|
+
│ ├── categories.controller.ts
|
|
63
|
+
│ └── categories.module.ts
|
|
64
|
+
├── app.module.ts # Root module
|
|
65
|
+
└── index.ts # Barrel exports
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Structure:** Each resource (tag) has its service, controller, and module grouped together in the same folder.
|
|
69
|
+
|
|
70
|
+
## Example Output
|
|
71
|
+
|
|
72
|
+
### DTO
|
|
73
|
+
```typescript
|
|
74
|
+
import { ApiProperty } from '@nestjs/swagger';
|
|
75
|
+
|
|
76
|
+
export class Product {
|
|
77
|
+
@ApiProperty({ description: 'Product ID', type: String })
|
|
78
|
+
id: string;
|
|
79
|
+
|
|
80
|
+
@ApiProperty({ description: 'Product name', type: String })
|
|
81
|
+
name: string;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Controller
|
|
86
|
+
```typescript
|
|
87
|
+
@ApiTags('products')
|
|
88
|
+
@Controller('products')
|
|
89
|
+
export class ProductsController {
|
|
90
|
+
@ApiOperation({ summary: 'List products' })
|
|
91
|
+
@Get('')
|
|
92
|
+
async listProducts(): Promise<Product[]> {
|
|
93
|
+
return this.productsService.listProducts();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Documentation
|
|
99
|
+
|
|
100
|
+
- **[README.md](README.md)** - Main documentation
|
|
101
|
+
- **[FORMATS.md](FORMATS.md)** - JSON vs YAML comparison
|
|
102
|
+
- **[OPENAPI-COMPARISON.md](OPENAPI-COMPARISON.md)** - Swagger 2.0 vs OpenAPI 3.0
|
|
103
|
+
- **[ARCHITECTURE.md](ARCHITECTURE.md)** - System architecture
|
|
104
|
+
- **[SUMMARY.md](SUMMARY.md)** - Project overview
|
|
105
|
+
|
|
106
|
+
## Key Features
|
|
107
|
+
|
|
108
|
+
✅ Supports Swagger 2.0 & OpenAPI 3.0
|
|
109
|
+
✅ Accepts JSON & YAML formats
|
|
110
|
+
✅ Generates type-safe TypeScript code
|
|
111
|
+
✅ Creates complete NestJS modules
|
|
112
|
+
✅ Includes Swagger decorators
|
|
113
|
+
✅ Handles complex schemas & references
|
|
114
|
+
✅ Supports path, query, and body parameters
|
|
115
|
+
|
|
116
|
+
## Need Help?
|
|
117
|
+
|
|
118
|
+
See the full documentation in [README.md](README.md) for detailed usage instructions and examples.
|