Skip to content

Commit ab68fc6

Browse files
Merge branch 'Kimsunmin-kimsunmin/swagger-other-features-patch'
2 parents eb78ef9 + d826b87 commit ab68fc6

File tree

1 file changed

+102
-6
lines changed

1 file changed

+102
-6
lines changed

content/openapi/other-features.md

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ async function bootstrap() {
5858
.addTag('cats')
5959
.build();
6060

61-
const catDocumentFactory = () => SwaggerModule.createDocument(app, options, {
62-
include: [CatsModule],
63-
});
61+
const catDocumentFactory = () =>
62+
SwaggerModule.createDocument(app, options, {
63+
include: [CatsModule],
64+
});
6465
SwaggerModule.setup('api/cats', app, catDocumentFactory);
6566

6667
const secondOptions = new DocumentBuilder()
@@ -70,9 +71,10 @@ async function bootstrap() {
7071
.addTag('dogs')
7172
.build();
7273

73-
const dogDocumentFactory = () => SwaggerModule.createDocument(app, secondOptions, {
74-
include: [DogsModule],
75-
});
74+
const dogDocumentFactory = () =>
75+
SwaggerModule.createDocument(app, secondOptions, {
76+
include: [DogsModule],
77+
});
7678
SwaggerModule.setup('api/dogs', app, dogDocumentFactory);
7779

7880
await app.listen(process.env.PORT ?? 3000);
@@ -93,3 +95,97 @@ Navigate to `http://localhost:3000/api/cats` to see the Swagger UI for cats:
9395
In turn, `http://localhost:3000/api/dogs` will expose the Swagger UI for dogs:
9496

9597
<figure><img src="/assets/swagger-dogs.png" /></figure>
98+
99+
#### Dropdown in the explorer bar
100+
101+
To enable support for multiple specifications in the dropdown menu of the explorer bar, you'll need to set `explorer: true` and configure `swaggerOptions.urls` in your `SwaggerCustomOptions`.
102+
103+
> info **Hint** Ensure that `swaggerOptions.urls` points to the JSON format of your Swagger documents! To specify the JSON document, use `jsonDocumentUrl` within `SwaggerCustomOptions`. For more setup options, check [here](/openapi/introduction#setup-options).
104+
105+
Here’s how to set up multiple specifications from a dropdown in the explorer bar:
106+
107+
```typescript
108+
import { NestFactory } from '@nestjs/core';
109+
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
110+
import { AppModule } from './app.module';
111+
import { CatsModule } from './cats/cats.module';
112+
import { DogsModule } from './dogs/dogs.module';
113+
114+
async function bootstrap() {
115+
const app = await NestFactory.create(AppModule);
116+
117+
// Main API options
118+
const options = new DocumentBuilder()
119+
.setTitle('Multiple Specifications Example')
120+
.setDescription('Description for multiple specifications')
121+
.setVersion('1.0')
122+
.build();
123+
124+
// Create main API document
125+
const document = SwaggerModule.createDocument(app, options);
126+
127+
// Setup main API Swagger UI with dropdown support
128+
SwaggerModule.setup('api', app, document, {
129+
explorer: true,
130+
swaggerOptions: {
131+
urls: [
132+
{
133+
name: '1. API',
134+
url: 'api/swagger.json',
135+
},
136+
{
137+
name: '2. Cats API',
138+
url: 'api/cats/swagger.json',
139+
},
140+
{
141+
name: '3. Dogs API',
142+
url: 'api/dogs/swagger.json',
143+
},
144+
],
145+
},
146+
jsonDocumentUrl: '/api/swagger.json',
147+
});
148+
149+
// Cats API options
150+
const catOptions = new DocumentBuilder()
151+
.setTitle('Cats Example')
152+
.setDescription('Description for the Cats API')
153+
.setVersion('1.0')
154+
.addTag('cats')
155+
.build();
156+
157+
// Create Cats API document
158+
const catDocument = SwaggerModule.createDocument(app, catOptions, {
159+
include: [CatsModule],
160+
});
161+
162+
// Setup Cats API Swagger UI
163+
SwaggerModule.setup('api/cats', app, catDocument, {
164+
jsonDocumentUrl: '/api/cats/swagger.json',
165+
});
166+
167+
// Dogs API options
168+
const dogOptions = new DocumentBuilder()
169+
.setTitle('Dogs Example')
170+
.setDescription('Description for the Dogs API')
171+
.setVersion('1.0')
172+
.addTag('dogs')
173+
.build();
174+
175+
// Create Dogs API document
176+
const dogDocument = SwaggerModule.createDocument(app, dogOptions, {
177+
include: [DogsModule],
178+
});
179+
180+
// Setup Dogs API Swagger UI
181+
SwaggerModule.setup('api/dogs', app, dogDocument, {
182+
jsonDocumentUrl: '/api/dogs/swagger.json',
183+
});
184+
185+
await app.listen(3000);
186+
}
187+
188+
bootstrap();
189+
```
190+
191+
In this example, we set up a main API along with separate specifications for Cats and Dogs, each accessible from the dropdown in the explorer bar.

0 commit comments

Comments
 (0)