You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When generating a typescript client with class type style, if the backend returns a response that contains a dictionary with an enum and it is using the JsonStringEnumConverter, the generated ts client has a compile error in the toJson method of the containing object:
Version of NSwag toolchain, computer and .NET runtime used
export class WeatherForecast implements IWeatherForecast {
forecast!: { [key in keyof typeof RandomEnum]?: Weather; };
constructor(data?: IWeatherForecast) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}
init(_data?: any) {
if (_data) {
if (_data["forecast"]) {
this.forecast = {} as any;
for (let key in _data["forecast"]) {
if (_data["forecast"].hasOwnProperty(key))
(<any>this.forecast)![key] = _data["forecast"][key] ? Weather.fromJS(_data["forecast"][key]) : new Weather();
}
}
}
}
static fromJS(data: any): WeatherForecast {
data = typeof data === 'object' ? data : {};
let result = new WeatherForecast();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
if (this.forecast) {
data["forecast"] = {};
for (let key in this.forecast) {
if (this.forecast.hasOwnProperty(key))
(<any>data["forecast"])[key] = this.forecast[key] ? this.forecast[key].toJSON() : <any>undefined; // error occurs here
}
}
return data;
}
}
Expected behavior
The client should generate without errors, presumably by casting the string key into an enum: (<any>data["forecast"])[key] = this.forecast[key as RandomEnum] ? this.forecast[key as RandomEnum]?.toJSON() : <any>undefined;
Additional context
This doesn't happen if using interfaces to generate the client, or if the json string enum converter is not used.
It also doesn't happen if the value type of the dictionary is object, because it casts the entire value into , so it skips the type validation.
The text was updated successfully, but these errors were encountered:
Describe the bug
When generating a typescript client with class type style, if the backend returns a response that contains a dictionary with an enum and it is using the JsonStringEnumConverter, the generated ts client has a compile error in the toJson method of the containing object:
Version of NSwag toolchain, computer and .NET runtime used
Backend: NSwag.AspNetCore 14.3.0, .NET 8.0
NSwagStudio: 14.3.0.0
To Reproduce
Server:
Generated Open API spec:
Typescript client settings:
Snippet with error in the generated client:
Expected behavior
The client should generate without errors, presumably by casting the string key into an enum:
(<any>data["forecast"])[key] = this.forecast[key as RandomEnum] ? this.forecast[key as RandomEnum]?.toJSON() : <any>undefined;
Additional context
This doesn't happen if using interfaces to generate the client, or if the json string enum converter is not used.
It also doesn't happen if the value type of the dictionary is
object
, because it casts the entire value into , so it skips the type validation.The text was updated successfully, but these errors were encountered: