Surnet / swagger-jsdoc

Generates swagger/openapi specification based on jsDoc comments and YAML files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot read properties of null (reading 'indexOf')

MatthewDownsNR opened this issue · comments

Describe the bug
Attempting to generate the OpenAPI in a TypeScript project is causing the error described in the title. It appears to be coming from the yaml library. So the likely scenario is that my JSDoc YAML is invalid, right? Interestingly, it still gives the error when I use the basic example on this repo from here.

To Reproduce
Steps to reproduce the behavior:

Setup a very basic typescript express application:

// src/app.ts
import express from 'express';
import { resolve } from 'path';
import swaggerJsdoc from 'swagger-jsdoc';
import loginRouter from './routers/login.ts';

cosnt app = express();

const openapi = swaggerJsdoc({
    definition: {
        openapi: '3.0.0',
        info: {
            title: 'Please work...'
        }
    },
    apis: [
       resolve(__dirname,  './routers/*.js')
       // I've also tried these paths:
       // './**/*.ts': Same error
       // './**/*.js': Same error
    ]
});

app.use('/login', loginRouter);

app.listen(3000, () => {
    console.log('Running on port 3000');
});
// src/routers/login.ts
import { Router } from 'express';

const router = Router();

/**
 * @openapi
 * /login:
 *   post:
 *     description: Login
 *     responses:
 *       200:
 *         description: Successfully login
 */
router.post('/', async (req, res) => {
    // ...
});

// I've also tried swapping "@openapi" with "@swagger"... same result

export default router;

Expected behavior
The JSDoc OpenAPI YAML spec should be parsed correctly

Desktop (please complete the following information):

  • OS: macOS 11.4

Additional context

  • Node: 17.4.0
  • NPM: 8.3.1
  • swagger-jsdoc: 6.1.0

As a last ditch effort, I cloned this repository and tried running the app example, and got the expected output. It doesn't make sense to me why it wouldn't be parsing the YAML correctly just because the source files are in TypeScript, since in my provided code example, I'm checking the compiled .js files. I also made sure comments are being emitted to the compiled code, and they are. Anyone know what could be causing the TypeError?

Figured it out, I had a rogue JSDoc somewhere in my code that has "@openapi", but with nothing else. This was sending a value of null to this function in the yaml library.

The suggestion then would be to throw a more meaningful error message when the JSDoc is empty.

+1 for a more helpful error in this scenario.