Open karvus opened 3 years ago
Hi, thanks for taking the time to report this bug report!
This is indeed a bug. The schema you provided is in fact not a valid JSON Type Definition schema -- the schema at /properties/bar/mapping/foo
is of the empty form, but mapping schemas must be of the properties form -- but jtd-codegen
should be providing a clear error message to this effect, and certainly shouldn't be panicking.
A bit more details on what schemas can appear inside mappings
is covered here: https://jsontypedef.com/docs/jtd-in-5-minutes/
You can only use properties / optionalProperties / additionalProperties in the schemas you put directly in mapping. You can’t use any other kind of schema, otherwise things would become ambiguous.
Admittedly, that language is a bit loose, and I understand how you would think that using no keywords at all would be kosher. The RFC is a bit more explicit: https://tools.ietf.org/html/rfc8927#section-2
; discriminator describes the "discriminator" schema form.
;
; There are additional constraints on this form that cannot be
; expressed in CDDL. Section 2.2.8 describes these additional
; constraints in detail.
discriminator = (
discriminator: tstr,
; Note well: this rule is defined in terms of the "properties"
; CDDL rule, not the "schema" CDDL rule.
mapping: { * tstr => { properties } }
shared,
)
But again: jtd-codegen
should not be leaving you out to dry like this.
In the immediate term, I suspect you'll have success if you alter your schema to be a valid JTD schema. For instance, just adding "properties": {}
to foo
would make your schema valid. This schema seems to work, at least for me:
{
"properties": {
"tag": {
"enum": [
"foo"
]
},
"bar": {
"discriminator": "tag",
"mapping": {
"foo": {
"properties": {}
}
}
}
}
}
In the longer term, the jtd-codegen
bug here is that the tool should validate a schema's correctness before attempting to generate code from it, and produce an error message rather than generate code (and ultimately crash).
Thank you for the prompt reply, and for taking the time to explain!
A minimal example is as follows:
As I understand the spec, this should be a valid Typedef. When I try to run it by
$ RUST_BACKTRACE=full jtd-codegen foo.jtd.json --log-format minimal --typescript-out .
I get the following panic and backtrace.
Version is 0.4.0.
I feel that this is a bug.
Cheers
Edit: I tried with the latest release 0.4.1, and the problem remains the same.