Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
bclehmann opened this issue Jan 31, 2022 · 3 comments · Fixed by #662
Labels
bug Confirmed bug good first issue Good for newcomers

Comments

@bclehmann
Copy link

Prerequisites

  • I have written a descriptive issue title
  • 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 bclehmann changed the title Fastify schema allows omission of the type attribute for objects Fastify schema allows omission of the type attribute for objects, causing crashes in fastify-swagger Jan 31, 2022
@bclehmann bclehmann changed the title Fastify schema allows omission of the type attribute for objects, causing crashes in fastify-swagger Fastify schema allows omission of the type attribute for objects in path parameters, causing crashes in fastify-swagger Jan 31, 2022
@mcollina
Copy link
Member

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

@bclehmann
Copy link
Author

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

@mcollina mcollina transferred this issue from fastify/fastify Feb 1, 2022
@mcollina
Copy link
Member

mcollina commented Feb 1, 2022

I transfered this to fastify-swagger

@mcollina mcollina added bug Confirmed bug good first issue Good for newcomers labels Feb 1, 2022
@Uzlopak Uzlopak linked a pull request Sep 5, 2022 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants