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

Disallow "true", "false", and "null" as enum values #3223

Merged
merged 1 commit into from Aug 4, 2021

Conversation

Yogu
Copy link
Contributor

@Yogu Yogu commented Aug 4, 2021

The specification disallows these in the SDL grammar definition, and using one of these enum values would result in a schema that would throw validation errors on creation.

Fixes #3221.

I also changed the type of EnumValueDefinitionNode.name from NameNode to EnumValueNode, so this is definitely breaking. We might even want to rename the name property to value, but that would be even more breaking.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 4, 2021

CLA Signed

The committers are authorized under a signed CLA.

@Yogu Yogu force-pushed the reject-invalid-enum-values-in-sdl branch 3 times, most recently from c01d402 to aa52fdb Compare August 4, 2021 08:26
Copy link
Member

@IvanGoncharov IvanGoncharov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yogu Thanks for the change 👍

Can you please make it more in line with what we already have for restricting fragment names:

/**
* FragmentName : Name but not `on`
*/
parseFragmentName(): NameNode {
if (this._lexer.token.value === 'on') {
throw this.unexpected();
}
return this.parseName();
}

No need for custom AST node type just apple restriction during parsing.

But please keep your custom error message, since it improves DX a lot.

@Yogu
Copy link
Contributor Author

Yogu commented Aug 4, 2021

@IvanGoncharov

No need for custom AST node type just apple restriction during parsing.

It isn't a "custom" AST node type. EnumValueNode already existed and that's the name of the non-terminal used in the spec at the respective location. If we were to start fresh, it would probably be best to use EnumValueNode and rename name to value. Do you want to avoid the breaking change by continue using NameNode as the type for EnumValueDefinitionNode? I wouldn't have a problem with that, just checking that I understood you correctly.

@Yogu Yogu force-pushed the reject-invalid-enum-values-in-sdl branch from aa52fdb to 51ac1c1 Compare August 4, 2021 12:04
@Yogu Yogu changed the title BREAKING: Disallow "true", "false", and "null" as enum values Disallow "true", "false", and "null" as enum values Aug 4, 2021
@Yogu Yogu force-pushed the reject-invalid-enum-values-in-sdl branch 3 times, most recently from 4e44107 to fc88bd4 Compare August 4, 2021 12:10
@Yogu Yogu requested a review from IvanGoncharov August 4, 2021 12:14
/**
* EnumValue : Name but not `true`, `false` or `null`
*/
parseEnumValueName(): NameNode {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed this to parseEnumValueName() because it's no longer parsing into an EnumValue. but still having it in its own function keeps things clean.

The specification disallows these in the SDL grammar definition, and using one of these enum values would result in a schema that would throw validation errors on creation.

Fixes graphql#3221.
@Yogu Yogu force-pushed the reject-invalid-enum-values-in-sdl branch from fc88bd4 to 36f2edb Compare August 4, 2021 12:58
@IvanGoncharov
Copy link
Member

It isn't a "custom" AST node type. EnumValueNode already existed and that's the name of the non-terminal used in the spec at the respective location.

I checked the spec and you are completely right 👍

If we were to start fresh, it would probably be best to use EnumValueNode and rename name to value.

I think it's would be surprising behaviors, e.g. you wrote your visitor for EnumValue so most devs including me will assume you would be triggered only on literal enum value (inside arguments or inside default values).
Vise versa if you register a visitor on Name you expect to be called on all names.
I think using value inside literals and name inside definition is pretty intuitive.

But I agree that this creates a mismatch with the spec.
However, I'm not sure how to address it in spec.

this._lexer.token.start,
`${getTokenDesc(
this._lexer.token,
)} is reserved and cannot be used for an enum value.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW. Adding a similar message to fragment name parsing is welcomed if you want to work on such PR.

@IvanGoncharov IvanGoncharov added the PR: bug fix 🐞 requires increase of "patch" version number label Aug 4, 2021
@IvanGoncharov IvanGoncharov merged commit af878f3 into graphql:main Aug 4, 2021
@Yogu Yogu deleted the reject-invalid-enum-values-in-sdl branch August 4, 2021 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bug fix 🐞 requires increase of "patch" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parsing SDL allows true, false, and null for enum value names
2 participants