0
0
Expressframework~20 mins

Swagger/OpenAPI specification in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swagger/OpenAPI Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Swagger UI setup code?
Consider this Express code snippet that sets up Swagger UI for API documentation. What will the user see when they visit /api-docs in their browser?
Express
import express from 'express';
import swaggerUi from 'swagger-ui-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.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

app.listen(3000);
AA web page showing interactive API documentation with endpoints from './routes/*.js' files.
BA JSON file download of the OpenAPI specification.
CA blank page because no routes are defined in the Express app.
DAn error message because swaggerJsdoc is missing required fields.
Attempts:
2 left
💡 Hint
Think about what swaggerUi.serve and swaggerUi.setup do with the swaggerSpec object.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a basic OpenAPI info object in swagger-jsdoc?
You want to define the API title and version in swagger-jsdoc options. Which snippet is syntactically correct?
A"info: { 'title': 'API', 'version': 1.0.0 }"
B"info: { title: 'API', version: 1.0 }"
C"info: { title: 'API', version: '1.0.0' }"
D"info: { title: API, version: '1.0.0' }"
Attempts:
2 left
💡 Hint
Check the quotes around strings and the version format.
🔧 Debug
advanced
2:30remaining
Why does this Swagger UI setup show an empty page?
This Express app uses swagger-ui-express and swagger-jsdoc but the Swagger UI page is empty. What is the likely cause?
Express
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';

const app = express();

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

const swaggerSpec = swaggerJsdoc(options);

app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

app.listen(3000);
AThe 'apis' path './nonexistent/*.js' does not match any files, so no endpoints are documented.
BThe swaggerUi.setup function requires a second argument with custom CSS to display content.
CThe swaggerJsdoc options object is missing the 'servers' property.
DThe Express app is missing a route handler for '/docs'.
Attempts:
2 left
💡 Hint
Check if the 'apis' pattern points to existing files with JSDoc comments.
state_output
advanced
1:30remaining
What is the value of 'swaggerSpec.info.title' after running this code?
Given this swagger-jsdoc options object, what is the value of the title property in the generated swaggerSpec.info?
Express
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'My API',
      version: '2.0.0',
      description: 'API description here'
    }
  },
  apis: []
};

const swaggerSpec = swaggerJsdoc(options);

// What is swaggerSpec.info.title?
A'2.0.0'
B'API description here'
Cundefined
D'My API'
Attempts:
2 left
💡 Hint
Look inside the 'definition' object under 'info'.
🧠 Conceptual
expert
2:00remaining
Which option best describes the role of 'swagger-jsdoc' in an Express app?
In an Express app using Swagger/OpenAPI, what is the main purpose of the 'swagger-jsdoc' package?
AIt serves the Swagger UI web interface to display API documentation.
BIt generates an OpenAPI specification object by parsing JSDoc comments in source files.
CIt validates incoming API requests against the OpenAPI schema.
DIt automatically generates Express route handlers from the OpenAPI spec.
Attempts:
2 left
💡 Hint
Think about what swagger-jsdoc does with your code comments.