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

Valid JSON Schema not working for null-able response #388

Closed
2 tasks done
andyburke opened this issue Feb 8, 2022 · 7 comments · Fixed by #496
Closed
2 tasks done

Valid JSON Schema not working for null-able response #388

andyburke opened this issue Feb 8, 2022 · 7 comments · Fixed by #496
Labels
bug Confirmed bug good first issue Good for newcomers

Comments

@andyburke
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.22.1

Plugin version

No response

Node.js version

14.x

Operating system

Linux

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

20.04

Description

When trying to define a GET request that can return either an object or a null, the valid JSON Schema of type: [ 'object', 'null' ] is not supported by Fastify.

FastifyError [FST_ERR_SCH_SERIALIZATION_BUILD]: Failed building the serialization schema for GET: /test, due to error object,null unsupported

schema: {
	summary: 'Test',
	response: {
		200: {
			type: [ 'object', 'null' ],
			properties: {
				foo: {
					type: 'string'
				}
			}
		}
	}
}

I understand you are supposed to pass nullable but we do a lot of automatic schema generation, and needing to do this only in the case where the top-level response is in this situation is frustrating. It seems like this is at the intersection of Fastify, Open API 3 and JSON Schema, so I don't envy trying to navigate this, but as it stands this is a pretty confusing error to get.

Steps to Reproduce

Create an endpoint whose response can be either a valid value or null.

Expected Behavior

Passing valid JSON Schema for a response that's a possible null object should work as it does at other levels schema definition.

@VigneshMurugan
Copy link
Contributor

Let me work on this

@matthyk
Copy link
Contributor

matthyk commented Feb 8, 2022

If you need a quick fix

schema: {
	summary: 'Test',
	response: {
		200: {
			type: 'object',
                         nullable: true,
			properties: {
				foo: {
					type: 'string'
				}
			}
		}
	}
}

does the trick.

@andyburke
Copy link
Author

Yeah, we are currently doing something like:

200: { ...as_json_schema( model ), nullable: true }

Although there is a workaround, this still feels out of place because it's only necessary at this top level (at least as I currently understand the issue).

@Eomm Eomm transferred this issue from fastify/fastify Feb 11, 2022
@Eomm Eomm added bug Confirmed bug good first issue Good for newcomers labels Feb 11, 2022
@mcollina
Copy link
Member

@Eomm why is it a good first issue? I'd not have a clue on how to fix this in Fastify.

@matthyk
Copy link
Contributor

matthyk commented Feb 11, 2022

As I understand it, the problem lies in fast-json-stringify in this switch case

switch (schema.type) {
as this does not support an array of types. So the problem is not with the null but with the array. Accordingly, { type: [ "string", "number" ] } does not work either.
So the question is whether we want to support arrays to specify different types?

@mcollina
Copy link
Member

I think it would be a great idea!

@andyburke
Copy link
Author

andyburke commented Feb 11, 2022

@matthyk FWIW, this "type as an array" comes from the JSON Schema way of specifying these. Not sure what impacts that would have on fast-json-stringify, as I'm not very familiar with it.

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.

5 participants