NSwag generated client makes optional files required
I used NSwag to generate a typescript client which uploads two files, where only one of the two is required and the other is optional. But something seems to go wrong with optional files when generating a client using NSwag.
Below is some code generated by NSwag with the following swagger.json. It is generated for typescript, but I presume it goes wrong when generating the client in other programming languages (have only tested with typescript and .Net). Here you can see the fully generated client with the swagger.json.
/**
* Test upload with one required file and one optional file
* @param file1 (optional)
* @param file2 (optional)
* @return Success
*/
testupload(file1: FileParameter | undefined, file2: FileParameter | undefined): Promise<OptionalFileUploadResponse> {
let url_ = this.baseUrl + "/api/testupload";
url_ = url_.replace(/[?&]$/, "");
const content_ = new FormData();
if (file1 === null || file1 === undefined)
throw new Error("The parameter 'file1' cannot be null.");
else
content_.append("file1", file1.data, file1.fileName ? file1.fileName : "file1");
if (file2 === null || file2 === undefined)
throw new Error("The parameter 'file2' cannot be null.");
else
content_.append("file2", file2.data, file2.fileName ? file2.fileName : "file2");
let options_: RequestInit = {
body: content_,
method: "POST",
headers: {
"Accept": "application/json"
}
};
return this.http.fetch(url_, options_).then((_response: Response) => {
return this.processTestupload(_response);
});
}
Now there are two things that seem to go wrong here:
- file1 is said to be optional in the comments above the function, even though it is set to required in the swagger.json.
- It does a check on file2 whether its null or undefined, and throws an exception if it is. But in the swagger.json I have specified that file2 is optional. And so I would expect that file2 can be undefined here.
Also here is provided the nswagfile.nswag for generating the client.
It think that the issue is in https://github.com/RicoSuter/NSwag/blob/512eb654b1be5714c7923b74d22e2049d56b5622/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestBody.liquid#L54C1-L68C26
at it isn't checking parameter.IsRequired / parameter.IsNullable
Issue is still there with NSwag 14.3. Is there a workaround?