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

Missing nullable property for nullable reference type dictionary values #1747

Open
ben-kiplot opened this issue Nov 20, 2024 · 0 comments
Open

Comments

@ben-kiplot
Copy link

NSwag seems unable to correctly generate API clients for dictionary properties with nullable reference type values, such as public Dictionary<string, object?> { get; set; } and I think I've tracked this down to an issue with NJsonSchema not supporting this.

Generating a schema for this class:

public class TestDto
{
    public required Dictionary<string, object?> TestProperty { get; set; };
}

incorrectly generates the following schema which lacks the nullable additional property:

"TestDto": {
  "type": "object",
  "additionalProperties": false,
  "required": [
    "testProperty"
  ],
  "properties": {
    "testProperty": {
      "type": "object",
      "additionalProperties": {}
    }
  }
}

Nullable primitives don't exhibit the same issue however:

public class TestDto
{
    public required Dictionary<string, string?> TestProperty { get; set; };
}

correctly generates the following:

"TestDto": {
  "type": "object",
  "additionalProperties": false,
  "required": [
    "testProperty"
  ],
  "properties": {
    "testProperty": {
      "type": "object",
      "additionalProperties": {
        "type": "string",
        "nullable": true
      }
    }
  }
}

I've made a small change that shows when the schema generation is changed to set the property as nullable the C# code generation does correctly generate a Dictionary<string, object?> as expected. The commit is here: 5f52e9d

This change doesn't result in the exact same code as shown above for nullable primitives though so perhaps this isn't the correct way to fix this.

"NullableObjectDictionary": {
      "type": "object",
      "additionalProperties": {
        "x-nullable": true
      }
    }
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

No branches or pull requests

1 participant