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

Fields on Schema should be nullable #7

Open
bombsimon opened this issue Mar 22, 2023 · 0 comments
Open

Fields on Schema should be nullable #7

bombsimon opened this issue Mar 22, 2023 · 0 comments

Comments

@bombsimon
Copy link

bombsimon commented Mar 22, 2023

I don't find this statement in the RFC but given this error in the jtd Rust crate and the fact that a Schema is an enum and not a struct on the Rust side you can't have both Type and Enum on a schema.

Given I marshal the schema jtd.Schema{Enum: []string{"foo", "bar"} I will get the following output:

{
  "definitions": null,
  "metadata": null,
  "nullable": false,
  "ref": null,
  "type": "",
  "enum": [
    "foo",
    "bar"
  ],
  "elements": null,
  "properties": null,
  "optionalProperties": null,
  "additionalProperties": false,
  "values": null,
  "discriminator": "",
  "mapping": null
}

If I send this schema to a Rust application to be deserialized by the Rust crate this will result in a jtd::SerdeSchema with a type that is Some("") and the enum set. If I then try to convert the jtd::SerdeSchema to a jtd::Schema this will fail since this is not allowed.

fn main() {
    let data = r#"{
      "definitions": null,
      "metadata": null,
      "nullable": false,
      "ref": null,
      "type": "",
      "enum": [
        "foo",
        "bar"
      ],
      "elements": null,
      "properties": null,
      "optionalProperties": null,
      "additionalProperties": false,
      "values": null,
      "discriminator": "",
      "mapping": null
    }"#;

    let s: jtd::SerdeSchema = serde_json::from_str(data).unwrap();
    let r = jtd::Schema::from_serde_schema(s);
    let e = r.err().unwrap();

    println!("{:?}: {}", e, e);
}
InvalidForm: invalid combination of keywords in schema

This would be solved by merging #5 but I wanted to add this issue to show a use case and a reason to why we would want to use omitempty.

This applies for at least Type, Discriminators and AdditionalProperties.

(The last one is a bit weird though, that should probably be fixed on the Rust side by ignoring false instead of using Optional<bool> but that's off topic for this issue)

@bombsimon bombsimon changed the title Type on Schema should be nullable Fields on Schema should be nullable Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant