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

Option no longer has a default of null as of v0.8.18 #295

Closed
stevenengler opened this issue May 22, 2024 · 2 comments · Fixed by #296
Closed

Option no longer has a default of null as of v0.8.18 #295

stevenengler opened this issue May 22, 2024 · 2 comments · Fixed by #296

Comments

@stevenengler
Copy link

In schemars v0.8.17, an Option with a default of None results in "default": null.

use schemars::{schema_for, JsonSchema};

#[derive(JsonSchema, Default)]
#[schemars(default = "Self::default")]
pub struct MyStruct {
    pub my_int: Option<i32>,
}

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",
  "properties": {
    "my_int": {
      "default": null,
      "type": [
        "integer",
        "null"
      ],
      "format": "int32"
    }
  }
}

In schemars v0.8.18 (and the latest v0.8.20), the same Option has no default.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "properties": {
    "my_int": {
      "type": [
        "integer",
        "null"
      ],
      "format": "int32"
    }
  }
}

I'm not sure if this was an intentional change, but it seems to me like having "default": null would be more expected, especially given that the example used the #[schemars(default = "Self::default")] attribute.

@GREsau
Copy link
Owner

GREsau commented May 23, 2024

Nope this wasn't intentional, thanks for reporting it! It should now be fixed in 0.8.21

@stevenengler
Copy link
Author

Thanks for the quick fix!

Geal added a commit to apollographql/router that referenced this issue May 24, 2024
the generated schema changed due to a fix in schemars: GREsau/schemars#295
dani-garcia pushed a commit to bitwarden/sdk that referenced this issue May 31, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [schemars](https://graham.cool/schemars/)
([source](https://togithub.com/GREsau/schemars)) | dependencies | patch
| `0.8.19` -> `0.8.21` |

---

### Release Notes

<details>
<summary>GREsau/schemars (schemars)</summary>

###
[`v0.8.21`](https://togithub.com/GREsau/schemars/blob/HEAD/CHANGELOG.md#0821---2024-05-23)

[Compare
Source](https://togithub.com/GREsau/schemars/compare/v0.8.20...v0.8.21)

##### Fixed:

- Fix `null` default not being set on generated schemas
([GREsau/schemars#295
/
[GREsau/schemars#296)

###
[`v0.8.20`](https://togithub.com/GREsau/schemars/blob/HEAD/CHANGELOG.md#0820---2024-05-18)

[Compare
Source](https://togithub.com/GREsau/schemars/compare/v0.8.19...v0.8.20)

##### Fixed:

- Revert unintentional change in behaviour when combining `default` and
`required` attributes
([GREsau/schemars#292)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "every 2nd week starting on the 2 week
of the year before 4am on Monday" (UTC), Automerge - At any time (no
schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/bitwarden/sdk).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNjguMTAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4zNjguMTAiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbXX0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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.

2 participants