RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.63k stars 1.23k forks source link

Create "nonsensical"-style Dictionary types in TypeScript #2189

Open r-k-b opened 5 years ago

r-k-b commented 5 years ago

For a C# type like IDictionary<int, string>, NSwag CLI produces the TypeScript type:

{ [key: string] : string; }

However, the serializer we're using produces an array of objects, with the type

{ Key: number, Value: string }[]

(aka DataContractJsonSerializer's "nonsensical" dictionary serialization)

Is it feasible to have NSwag CLI output types like the latter?


Related?

RicoSuter commented 5 years ago

You'd need to provide a modified JsonSchemaGenerator (in NSwag v13 an own implementation of OpenApiSchemaGenerator) with a special implementation of GenerateDictionary:

https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/JsonSchemaGenerator.cs#L569

I'll make the GenerateDictionary method protected virtual...

RicoSuter commented 5 years ago

This will be in NSwag v13 (see preview packages) - but first we need to update the previews with the new NJS version.