Closed feuxfollets1013 closed 4 years ago
The parser enforces first character of json property name as alphabet for safety. And yes, underscore prefix can be accepted.
I will create a PR for this issue. For now, I think there isn't any workaround for this. You can get entire json field then parse on client side, or pick nearest parent that doesn't contain underscore prefix
@hgiasac Thank you for your comment.
I confirmed your PR. I assume you'd better to allow digits and hyphen as with underscore, because it looks like the parser allows digits, hyphen and underscore in json property name. But I don't know Haskell at all.
@feuxfollets1013 IMO, number and hyphen isn't best practices for property name's first character
JSON Schema recommends first character is only alphabet and underscore
{
"type": "object",
"propertyNames": {
"pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
}
}
Google guideline allows $
too, but I don't think it's useful. By the way, @coco98 @lexi-lambda @0x777 @paf31 what do you think?
@feuxfollets1013 PS: From second character, underscore, hyphen and numbers are allowed. Is your idea that we should support hyphen and numbers on first char?
@hgiasac At least, I would be glad if you accepted underscore. I use some industry group standard on production. The JSON data defined in the standard has some property name's first character is underscore.
I agree Google guideline, because the javascript clients can access properties using dot notation.
I have never seen first character hyphen, digit and $
except JSON Schema $ref
or $schema
, so I think you don't have to support them.
But if you would like to followed JSON Specification strictly, you would have to support them.
IMO, for special characters, we can use bracket notation, e.g [$abc]
instead. It is similar to Javascript:
headers['Hello world'] // ok
headers.Hello world // error
Yes, we can access properties with your notation, but it is easier to access with dot notation(e.g. headers.helloWorld
or headers.$hello
) than bracket notation(e.g. headers['hello world']
or headers['$hello']
) on Javascript.
So I guess google style guide set the property naming rule.
JSON Schema recommends first character is only alphabet and underscore
I guess it is not a recommendation, but an example for JSON schema beginners.
JSON Schema Spec. only says if propertyNames
sets, the parser tests property names by using propertyNames
value.
In summary to the discussion, we can choise three proposal.
following JSON Specification: the first character of JSON property name doesn't have any constraints without letter which need to escape in JSON.
following JSON Google style guide:
the first character of JSON property name must be letter or underscore or doller sign($
).
creating original rule for Hasura: for example, the first character of JSON property name must be letter or underscore.
I recommend google style guide rule, bacause I think JSON spec. rule is too free.
I'm having a similar problem but with characters within the JSONB object keys (I really just want the dot, but testing some alternatives):
The table field 'mapping' is the JSONB field.
I'm fairly confident that this makes sense as regular JSON to a javascript parser, but not sure if this is JS doing some funky stuff under the hood:
I then tried it as part of the query variables and it worked, despite the error in the validator:
Hopefully this helps someone who is searching around for "jsonb" and "object keys" as I did.
@toddheslin About your case, it is best practice if you input entire JSON field input into variables instead of inputting field by field directly into GraphQL query, because special characters violate GraphQL specs too.
Yes, keep in mind that input objects in GraphQL are not JSON, even if they have a lot of similarities. Strictly speaking, the right approach is probably to pass the JSON object as a string (passed through JSON.stringify
).
Thanks @hgiasac and @lexi-lambda. Both very useful comments :-)
@lexi-lambda Is anyone working on this?
@0x777 I fixed it with this PR https://github.com/hasura/graphql-engine/pull/3892
Hi, I try to search JSONB data. It includes following data.
{ "hello": "world", "_hello": "_world" }
I executed query including the key starts with "_" on Hasura console.
query MyQuery { test1 { data(path: "$._hello") } }
I received following response.
{ "errors": [ { "extensions": { "path": "$.selectionSet.test1", "code": "parse-failed" }, "message": "parse json path error: the first character of property name must be a letter." } ] }
So I confirmed JSON specification RFC8259. The document says following. I suppose the first character of JSON object key doesn't have any constraints without unescaped letter, but Hasura cannot use "_".
Is it a constraint Hasura or GraphQL? Or is there any workaround?
Thanks.