Skip to content

Excluding null to match skip_serializing_if = "Option::is_none" #491

@lgarron

Description

@lgarron

I have some code like this:

pub(crate) struct PostVersionInfo {
    // …
    #[serde(skip_serializing_if = "Option::is_none")]
    magnitude: Option<VersionBumpMagnitudeType>,
}

This outputs a schema containing:

{
    //
    "magnitude": {
      "anyOf": [
        {
          "$ref": "#/$defs/VersionBumpMagnitudeType"
        },
        {
          "type": "null"
        }
      ]
    }
}

This is not valid, as null is specifically not a possibility (and validation should fail if null is specified as the value).

It seems there are a few related issues about this, but I can't quite figure out if this is supposed to be supported. It would be nice if this either:

  • happened automatically when skip_serializing_if = "Option::is_none" is specified, or
  • had a simple attribute to exclude null as a valid field value in the output JSON.

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