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

Schemars omit serde(flatten) BTreeMap #259

Closed
JakkuSakura opened this issue Dec 16, 2023 · 4 comments · May be fixed by #275
Closed

Schemars omit serde(flatten) BTreeMap #259

JakkuSakura opened this issue Dec 16, 2023 · 4 comments · May be fixed by #275

Comments

@JakkuSakura
Copy link

JakkuSakura commented Dec 16, 2023

use schemars::{schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MyStruct {
    pub values: HashMap<String, String>,
    #[serde(flatten)]
    pub flatten: HashMap<String, String>,
}

fn main() {
    let schema = schema_for!(MyStruct);
    println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "required": [
    "values"
  ],
  "properties": {
    "values": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    }
  }
}
@JakkuSakura JakkuSakura changed the title Schemars omit serde(flatten) BTreeMap Schemars omit serde(flatten) BTreeMap/Vec Dec 16, 2023
@nightkr
Copy link
Contributor

nightkr commented Mar 22, 2024

That schema doesn't seem to correspond to those type definitions at all.

@JakkuSakura
Copy link
Author

Sorry. I pasted the wrong rust type. I've updated the example

@nightkr
Copy link
Contributor

nightkr commented Mar 22, 2024

Fwiw, it looks like a workaround for now is to add #[schemars(deny_unknown_fields)] to MyStruct.

@JakkuSakura JakkuSakura changed the title Schemars omit serde(flatten) BTreeMap/Vec Schemars omit serde(flatten) BTreeMap Mar 22, 2024
@GREsau
Copy link
Owner

GREsau commented Aug 18, 2024

This is fixed in schemars 1.0.0-alpha.6 (and I think earlier alphas) - the output from the repro is now:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "values": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      }
    }
  },
  "additionalProperties": {
    "type": "string"
  },
  "required": [
    "values"
  ]
}

@GREsau GREsau closed this as completed Aug 18, 2024
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

Successfully merging a pull request may close this issue.

3 participants