Search code examples
angulartypescriptswaggernswag

Nested Array of objects - item.toJSON is not a function


When using NSwag with my angular project, throws an error, when trying to send the data, if the object has an nested array of object as here:

export interface IJobAdDto {
    mainJobAd: JobAddDetailsDto;
    differentLanguageJobAds: JobAddDetailsDto[] | undefined;}

enter image description here

But if i instead "cast it" to a new object of the type it should already be, it works: enter image description here

The question is, how do i get NSwag to make this change, or how can i extend this method/class so it will not overwrite it each time it is generated?

I have generated the client code from with NSwag(13.9.4.0).

Error: enter image description here


Solution

  • One way to do it is to seperate the DTO from the client, so it is not overwritting every single time.

    Put the DTO in the service class that is defined by NSwag: enter image description here

    And then "Exclude the type names"(dtos) that you dont want NSwag to generate: enter image description here

    and then whatever changes you need to make, can be made in there. The downside to this, is that if the model changes, you need to remember to update the extention file.