Skip to content

bug: JsonSchemaAs for SetPreventDuplicates<DisplayFromStr> schema is "string" not "array" of "string" #906

@aatifsyed

Description

@aatifsyed

here's a test case

#[test]
fn test() {
    use serde_with::*;
    use std::collections::BTreeSet;

    #[serde_as]
    #[derive(schemars::JsonSchema)]
    struct MyStruct {
        #[serde_as(as = "SetPreventDuplicates<DisplayFromStr>")]
        via_display_from_str: BTreeSet<u32>, // incorrect schema

        #[serde_as(as = "SetPreventDuplicates<Same>")]
        via_same: BTreeSet<u32>, // correct schema
    }
    let schema = format!("{:#}", schemars::schema_for!(MyStruct).as_value());
    expect_test::expect![[r#"
        {
          "$schema": "https://json-schema.org/draft/2020-12/schema",
          "properties": {
            "via_display_from_str": {
              "type": "string"
            },
            "via_same": {
              "items": {
                "format": "uint32",
                "minimum": 0,
                "type": "integer"
              },
              "type": "array",
              "uniqueItems": true
            }
          },
          "required": [
            "via_display_from_str",
            "via_same"
          ],
          "title": "MyStruct",
          "type": "object"
        }"#]]
    .assert_eq(&schema);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions