Skip to content

[Bug]: Nullability problems with dictionaries #3022

@ozziepeeps

Description

@ozziepeeps

Describe the bug

Two issues: Dictionary<string, int>? in a nullable context is incorrectly described with the int value being nullable. Additionally the behavior of Swashbuckle changes if my property type is IDictionary<string, int>? vs Dictionary<string, int>? (e.g. interface vs concrete).

Expected behavior

If I have a property nullableMapNonNullableValue of type Dictionary<string, int>? or IDictionary<string, int>? on a schema, I would expect the int value to be described as NOT nullable in additionalProperties, like so:

{
    "nullableMapNonNullableValue": {
        "type": "object",
        "additionalProperties": {
            "type": "integer",
            "format": "int32"
        },
        "nullable": true
    }
}

Actual behavior

The int value in additionalProperties is incorrectly described as being nullable:

{
    "nullableMapNonNullableValue": {
        "type": "object",
        "additionalProperties": {
            "type": "integer",
            "format": "int32",
            "nullable": true // <-- this is wrong!
        },
        "nullable": true
    }
}

If, however, I change the type of the property to be IDictionary<string, int>? (e.g. interface instead of concrete) the behavior changes and the nullability of the int value is now described correctly in additionalProperties:

{
    "nullableMapNonNullableValue": {
        "type": "object",
        "additionalProperties": {
            "type": "integer",
            "format": "int32"
        },
        "nullable": true
    }
}

Steps to reproduce

I'm submitting a PR with unit tests that fixes both issues.

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

6.7.0

.NET Version

net8.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions