2023-02-26

Nswag adds null check for nullable/optional parameters

I have basically same issue as this (details here on Github) but with C# client - [FromForm] SomeObject x on controller has some nullable (optional) parameters and generated client generated by Nswag has null checks in place like this:

public virtual async System.Threading.Tasks.Task<Attachment> UploadAsync(int? idProject = null, int? idTicket = null...
...
if (idProject == null) throw new System.ArgumentNullException("idProject");
else
{
    content_.Add(new System.Net.Http.StringContent(ConvertToString(idProject, System.Globalization.CultureInfo.InvariantCulture)), "IdProject");
}
...

Both original model (from API project) and generated one in client project have those fields as nullable and function call accepts nullable values.

JSON schema from swagger looks like this:

"/Attachment/Upload": {
  "post": {
    "tags": [
      "Attachment"
    ],
    "requestBody": {
      "content": {
        "multipart/form-data": {
          "schema": {
            "required": [
              "Name"
            ],
            "type": "object",
            "properties": {
              "IdProject": {
                "type": "integer",
                "format": "int32"
              },
              "IdTicket": {
                "type": "integer",
                "format": "int32"
              },...

I've tried setting "queryNullValue": "" in openApiToCSharpClient but it does not help. How to disable those checks in generated client? I must use [FromForm] since I'm sending both file(s) and some additional data with them.

Nswag file (generator settings only):

"openApiToCSharpClient": {
  "generateClientInterfaces": true,
  "GenerateClientClasses": true,
  "useBaseUrl": false,
  "namespace": "xxxxxxxx.APIClient",
  "className": "{controller}Client",
  "operationGenerationMode": "MultipleClientsFromFirstTagAndOperationName",
  "jsonLibrary": "SystemTextJson",
  "generateDtoTypes": true,
  "disposeHttpClient": true,
  "injectHttpClient": true,
  "httpClientType": "System.Net.Http.HttpClient",
  "UseHttpRequestMessageCreationMethod": false,
  "generateBaseUrlProperty": false,
  "generateOptionalParameters": true,
  "parameterArrayType": "System.Collections.Generic.IReadOnlyList",
  "responseArrayType": "System.Collections.Generic.IReadOnlyList",
  "generateOptionalPropertiesAsNullable": true,
  "generateNullableReferenceTypes": true,
  "output": "Client.g.cs",
  "generateExceptionClasses": true,
  "dateType": "System.DateTime",
  "dateTimeType": "System.DateTime",
  "queryNullValue": "",
  "additionalNamespaceUsages": [
    "global::APIClient"
  ]
}


No comments:

Post a Comment