When generating the typescript client through nswag, the output contains the following:
export class GetItResponse implements IGetItResponse {
values!: (It | undefined)[];
constructor(data?: IGetItResponse) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
if (data.values) {
this.values = [];
for (let i = 0; i < data.values.length; i++) {
let item = data.values[i];
this.values[i] = item && !(<any>item).toJSON ? new It(item) : <It>item;
}
}
}
if (!data) {
this.values = [];
}
}
init(_data?: any) {
if (_data) {
if (Array.isArray(_data["values"])) {
this.values = [] as any;
for (let item of _data["values"])
this.values!.push(It.fromJS(item));
}
}
}
static fromJS(data: any): GetItResponse {
data = typeof data === 'object' ? data : {};
let result = new GetItResponse();
result.init(data);
return result;
}
toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
if (Array.isArray(this.values)) {
data["values"] = [];
for (let item of this.values)
data["values"].push(item.toJSON());
}
return data;
}
}
The toJSON function iterates over the values array and call .ToJSON() on each value. However, since the value can be undefined, I get the following error when compiling:
error TS2532: Object is possibly 'undefined'. => data["values"].push(item.toJSON());
I guess the generated toJSON should contain data["values"].push(item?.toJSON()); instead of data["values"].push(item.toJSON());
Hello,
The following OpenAPI definition contains a GetItResponse schema having 1 property "values" which is an array of a nullable object.
When generating the typescript client through nswag, the output contains the following:
The toJSON function iterates over the values array and call .ToJSON() on each value. However, since the value can be undefined, I get the following error when compiling:
I guess the generated toJSON should contain
data["values"].push(item?.toJSON());
instead ofdata["values"].push(item.toJSON());