Dotnet swagger serializes Dictionary with long key wrong

12 views Asked by At

The following model:

public record Test
{
    public Dictionary<long, string> Dictoinary { get; set; }
}

used like:

public IActionResult Produce([FromBody]Test request) { ... }

Results in the following json body in swagger UI:

{
  "dictoinary": {
    "additionalProp1": "string",
    "additionalProp2": "string",
    "additionalProp3": "string"
  }
}

It's schema is obviously wrong:

Test {
  dictoinary {
    < * >: string
  }
  nullable: true
}

Promptly such request results in 500 as key additionalProp1 cannot be serialized to a long.

Why does this happen? Can I plug-in somewhere during the serialization process of Swagger/Swashbuckle and correct this?

Note: this also happens for ints and I suspect it will happen for every type that cannot be assigned a value of additionalPropN

0

There are 0 answers