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

Behavior change in 0.8.19 with default, required, and skip_serializing_if #292

Closed
ahl opened this issue May 13, 2024 · 3 comments · Fixed by #293
Closed

Behavior change in 0.8.19 with default, required, and skip_serializing_if #292

ahl opened this issue May 13, 2024 · 3 comments · Fixed by #293

Comments

@ahl
Copy link
Contributor

ahl commented May 13, 2024

We use schemars for our OpenAPI document generation. We have a type with an optional property. With the OpenAPI generation this comes out as nullable:

#[derive(JsonSchema)]
pub struct WithNullable {
    pub value: Option<String>,
}
{
  "type": "object",
  "properties": {
    "value": {
      "type": "string",
      "nullable": true
    }
  }
}

We don't want the nullable in the spec and found prior to 0.8.19 that this combination got what we wanted:

#[derive(JsonSchema)]
pub struct WithoutNullable {
    #[schemars(default, required)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
}

Which produces this in 0.8.17

{
  "type": "object",
  "properties": {
    "value": {
      "type": "string"
    }
  }
}

However, in 0.8.19 we see this:

{
  "type": "object",
  "required": [
    "value"
  ],
  "properties": {
    "value": {
      "type": "string"
    }
  }
}

Is this behavior change from 0.8.17 to 0.8.19 intentional?

@GREsau
Copy link
Owner

GREsau commented May 13, 2024

This is an interesting case, and frankly I'm not 100% sure what the "correct" behaviour here is with this complicated combination of default/required/skip_serializing_if/Option<>.

In any case, I think this does what you want, and works in all versions:

#[derive(JsonSchema)]
pub struct WithoutNullable {
    #[schemars(default, with = "String")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
}

Does that solve your problem?

@ahl
Copy link
Contributor Author

ahl commented May 14, 2024

Sure! Or we can hand roll a JsonSchema impl. I should have been clearer: this behavior changed in a patch release. If that's intended / ok: please close this issue. If it's unintended then perhaps reinstate the behavior and/or change it in a minor release?

@GREsau
Copy link
Owner

GREsau commented May 18, 2024

Thanks for reporting this, I've now released 0.8.20 which restores the previous behaviour

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
2 participants