fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

Fastify schema allows omission of the type attribute for objects in path parameters, causing crashes in fastify-swagger

Open bclehmann opened this issue 3 years ago • 3 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.27.0

Plugin version

fastify-swagger: 4.13.1

Node.js version

16.13.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11 21H2 Build 22000.434

Description

Routes with schemas like this are treated as valid by fastify, but not by fastify-swagger:

fastify.get('/path', {
        handler: async () => 'foo',
        schema: {
            params: {
                // Note that it should say type: 'object' here
                // type: 'object',
                properties: {
                    a: {
                        type: 'number'
                    }
                }
            },
        }
    });

This throws in fastify-swagger

TypeError: Cannot read properties of undefined (reading 'split')
    at resolveLocalRef (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify-swagger\lib\util\common.js:176:36)
    at plainJsonObjectToSwagger2 (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify-swagger\lib\spec\swagger\utils.js:99:15)
    at resolveCommonParams (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify-swagger\lib\spec\swagger\utils.js:205:15)
    at prepareSwaggerMethod (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify-swagger\lib\spec\swagger\utils.js:298:24)
    at Object.swagger (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify-swagger\lib\spec\swagger\index.js:44:29)
    at C:\Users\benny\Programming\Darkhorse\wayne-state-api\dist\server.js:37:16
    at manageErr (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify\fastify.js:505:11)
    at exit (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify\lib\hooks.js:90:5)
    at manageTimeout (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\fastify\lib\hooks.js:107:11)
    at _encapsulateThreeParam (C:\Users\benny\Programming\Darkhorse\wayne-state-api\node_modules\avvio\boot.js:551:7)

Note that the error message is not informative to the user, to me it looks like it doesn't validate the data and it tries to read something which doesn't exist.

Steps to Reproduce

fastify.register(fastifySwagger, {
	swagger: {
		info: {
			title: 'Foo',
			version: '0.1.0'
		},
		host: 'localhost',
		schemes: ['http', 'https'],
		consumes: ['application/json'],
		produces: ['application/json'],
	},
	exposeRoute: process.env.NODE_ENV === "development"
});

fastify.get('/path', {
	handler: async () => 'foo',
	schema: {
		params: {
			// Note that it should say type: 'object' here
			// type: 'object',
			properties: {
				a: {
					type: 'number'
				}
			}
		},
	}
});
    
fastify.ready(err => {
	if(err) {
		throw err;
	}

	server.swagger();
});

Expected Behavior

Either fastify should treat the schema as invalid (perfect world) or fastify-swagger should treat the schema as valid.

Because of backwards-compatibility, I think the next best thing is to give a useful warning in fastify or a useful error message in fastify-swagger. At least until the next major release.

bclehmann avatar Jan 31 '22 19:01 bclehmann

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jan 31 '22 21:01 mcollina

I'm interested but I can't guarantee I'll be able to work on it anytime soon.

bclehmann avatar Feb 01 '22 04:02 bclehmann

I transfered this to fastify-swagger

mcollina avatar Feb 01 '22 11:02 mcollina