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

Support for omitting fields from the schema #27

Closed
untitaker opened this issue Apr 29, 2020 · 6 comments
Closed

Support for omitting fields from the schema #27

untitaker opened this issue Apr 29, 2020 · 6 comments

Comments

@untitaker
Copy link

I would like schemars to honor serde(skip) and schemars(skip) to be able to omit internal datastructures. Ex:

#[derive(JsonSchema)]
struct Foo {
    #[schemars(skip)]
    bar: String
}

would generate a schema against which the input {} validates.

@GREsau
Copy link
Owner

GREsau commented Apr 29, 2020

This is already implemented - when I run

use schemars::{schema_for, JsonSchema};

#[derive(JsonSchema)]
struct Foo {
    #[schemars(skip)]
    bar: String
}

fn main() {
    let schema = schema_for!(Foo);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}

It outputs:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Foo",
  "type": "object"
}

Have you encountered a case where this doesn't happen?

@untitaker
Copy link
Author

You're right it does, sorry for the noise. The actual issue I am running into is that all of the schemas generated by schemars do not have additionalProperties set to false, so this attempt failed:

#[derive(JsonSchema)]
struct Foo {
    #[schemars(skip)]
    #[serde(other)]
    extra_data: ...
}

Is there a way to generically set the default of additionalProperties?

@GREsau
Copy link
Owner

GREsau commented Apr 29, 2020

The #[serde(other)] attribute is not valid in your example. If you're trying to add a field that contains all additional properties when desrializing, then this would be done with the #[serde(flatten)] attribute as documented here: https://serde.rs/attr-flatten.html#capture-additional-fields

And in this case, additionalProperties will be set to true, e.g.

#[derive(JsonSchema)]
struct Foo {
    bar: String,
    #[serde(flatten)]
    extra: HashMap<String, Value>,
}

Outputs:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Foo",
  "type": "object",
  "required": [
    "bar"
  ],
  "properties": {
    "bar": {
      "type": "string"
    }
  },
  "additionalProperties": true
}

Unless you added #[schemars(skip)] to the extra field, in which case additionalProperties would not be set.

@GREsau
Copy link
Owner

GREsau commented Apr 29, 2020

Schemars doesn't set additionalProperties to false in derived schemas, mainly because serde allows unknown properties by default.

If you have a struct with #[serde(deny_unknown_fields)], then it would make sense for the schema to set additionalProperties to false. I've planned on implementing that for a while (hence this comment!), it's just one of the things I haven't got around to and no-one's asked for

@untitaker
Copy link
Author

Thanks, I did mean flatten. The real background is that we are using a custom deserialization library other than serde and I was trying to keep the examples short.

I am a bit confused why you are either setting additionalProperties=true vs omitting it, as omission is the same thing as setting it to true afaik.

In any case deny_unknown_fields across all structs is likely closest to what I would need as our deserialization framework's default behavior diverges from serde here.

@untitaker
Copy link
Author

I'm going to close this since the underlying feature I am missing is already tracked in sourcecode and this thread is quite long-winded.

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

2 participants