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

[Bug]: Nullability problems with dictionaries #3022

Closed
ozziepeeps opened this issue Aug 12, 2024 · 0 comments · Fixed by #3023
Closed

[Bug]: Nullability problems with dictionaries #3022

ozziepeeps opened this issue Aug 12, 2024 · 0 comments · Fixed by #3023
Labels
Milestone

Comments

@ozziepeeps
Copy link
Contributor

ozziepeeps commented Aug 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants