GREsau / schemars

Generate JSON Schema documents from Rust code
https://graham.cool/schemars/
MIT License
791 stars 220 forks source link

Expected schemars description attribute to be a string #299

Closed Altair-Bueno closed 2 weeks ago

Altair-Bueno commented 3 months ago

Expected behavior

Expected macros like include_str, concat, etc to work without issues.

Current behavior

Usage of said macros results on either compilation errors or doc attributes being ignored.

Minimal example

#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
struct Foo {
    #[schemars(description = concat!("aa", "bbb"))]   
    pub f: usize,
}

fn main() {
    let schema = schemars::schema_for!(Foo);
    let schema = serde_json::to_string(&schema).unwrap();
    println!("{}", schema);
}
   Compiling sample v0.1.0 (/tmp/sample)
error: expected schemars description attribute to be a string: `description = "..."`
 --> src/main.rs:4:30
  |
4 |     #[schemars(description = concat!("aa", "bbb"))]   
  |                              ^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `Foo: JsonSchema` is not satisfied
   --> src/main.rs:9:40
    |
9   |     let schema = schemars::schema_for!(Foo);
    |                  ----------------------^^^-
    |                  |                     |
    |                  |                     the trait `JsonSchema` is not implemented for `Foo`
    |                  required by a bound introduced by this call
    |
    = help: the following other types implement trait `JsonSchema`:
              bool
              char
              isize
              i8
              i16
              i32
              i64
              i128
            and 130 others
note: required by a bound in `SchemaGenerator::into_root_schema_for`
   --> /home/compux72/.cargo/registry/src/index.crates.io-6f17d22bba15001f/schemars-0.8.21/src/gen.rs:329:45
    |
329 |     pub fn into_root_schema_for<T: ?Sized + JsonSchema>(mut self) -> RootSchema {
    |                                             ^^^^^^^^^^ required by this bound in `SchemaGenerator::into_root_schema_for`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `sample` (bin "sample") due to 2 previous errors
Altair-Bueno commented 3 months ago

Probably the relevant piece of code is this one:

https://github.com/GREsau/schemars/blob/e0c2c31dc9a86f813defe4621c403d5b9e4244de/schemars_derive/src/attr/mod.rs#L240

It should allow the use of macros

GREsau commented 2 weeks ago

This is fixed in schemars 1.0.0-alpha.12 🙂

The output from the example given in this issue (when prettified) is now:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Foo",
  "type": "object",
  "properties": {
    "f": {
      "description": "aabbb",
      "type": "integer",
      "format": "uint",
      "minimum": 0
    }
  },
  "required": [
    "f"
  ]
}