0
0
Expressframework~20 mins

swagger-jsdoc setup in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swagger-jsdoc Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this swagger-jsdoc setup output?
Given this Express app setup with swagger-jsdoc, what will the /api-docs.json endpoint return?
Express
import express from 'express';
import swaggerJSDoc from 'swagger-jsdoc';

const app = express();

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Sample API',
      version: '1.0.0'
    }
  },
  apis: ['./routes/*.js']
};

const swaggerSpec = swaggerJSDoc(options);

app.get('/api-docs.json', (req, res) => {
  res.json(swaggerSpec);
});

app.listen(3000);
AAn HTML page showing API documentation
BA plain text string describing the API routes
CA JSON object with OpenAPI info and paths from './routes/*.js' files
DAn error because './routes/*.js' files are missing
Attempts:
2 left
💡 Hint
swagger-jsdoc generates a JSON spec from options and comments in specified files.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes swagger-jsdoc?
Choose the correct way to initialize swagger-jsdoc with OpenAPI 3.0 and info title 'My API'.
AswaggerJSDoc({ openapi: '3.0.0', info: { title: 'My API' }, apis: ['./api.js'] })
BswaggerJSDoc({ openapi: '3.0.0', apis: ['./api.js'] })
CswaggerJSDoc({ info: { title: 'My API' }, apis: ['./api.js'] })
DswaggerJSDoc({ definition: { openapi: '3.0.0', info: { title: 'My API' } }, apis: ['./api.js'] })
Attempts:
2 left
💡 Hint
The OpenAPI info must be inside a 'definition' object.
🔧 Debug
advanced
2:00remaining
Why does swagger-jsdoc return an empty paths object?
You set up swagger-jsdoc with correct options but the generated JSON has an empty 'paths' object. What is the most likely cause?
Express
const options = {
  definition: {
    openapi: '3.0.0',
    info: { title: 'API', version: '1.0' }
  },
  apis: ['./src/routes/*.js']
};
const swaggerSpec = swaggerJSDoc(options);
AThe files in './src/routes/*.js' do not contain any JSDoc comments with Swagger annotations
BThe 'definition' object is missing the 'servers' property
CThe 'apis' path is relative and must be absolute
DThe swagger-jsdoc package version is incompatible with OpenAPI 3.0
Attempts:
2 left
💡 Hint
swagger-jsdoc extracts API paths from special comments inside the files.
state_output
advanced
2:00remaining
What is the effect of calling swaggerJSDoc multiple times with different options?
If you call swaggerJSDoc twice with different 'apis' arrays and merge the results, what will happen?
Express
const spec1 = swaggerJSDoc({ definition: { openapi: '3.0.0', info: { title: 'API1' } }, apis: ['./routes1/*.js'] });
const spec2 = swaggerJSDoc({ definition: { openapi: '3.0.0', info: { title: 'API2' } }, apis: ['./routes2/*.js'] });
const merged = { ...spec1, paths: { ...spec1.paths, ...spec2.paths } };
AswaggerJSDoc caches results and returns the first spec always
BThe merged object combines paths from both specs but info will be from spec1 only
CThe merged object will throw an error because swaggerJSDoc outputs are not mergeable
DThe merged object will only contain paths from spec2
Attempts:
2 left
💡 Hint
swaggerJSDoc returns a plain object; merging is manual.
🧠 Conceptual
expert
2:00remaining
What is the role of the 'apis' option in swagger-jsdoc?
Why do you specify file paths in the 'apis' option when configuring swagger-jsdoc?
ATo tell swagger-jsdoc where to find JSDoc comments with Swagger annotations to generate API documentation
BTo specify where swagger-jsdoc should output the generated JSON file
CTo list the API endpoints that swagger-jsdoc will automatically create routes for
DTo define the order in which API routes are loaded in the Express app
Attempts:
2 left
💡 Hint
Think about how swagger-jsdoc knows what to document.