Search code examples
swaggercode-generationswashbucklenswagnswagstudio

NSwag generate single client class


When using NSwagStudio for generating C# client code (not in file) it generates the way that only one client class and corresponding interface is getting generated with all controllers methods in them. However when trying to do the same thing with NSwag.MSBuild it generates separate partial classes/interfaces for each controller.

Here is the nswag.json:

{
"openApiToCSharpClient": {
  "clientBaseClass": "BillingBaseClient",
  "configurationClass": "ConnectionOptions",
  "generateClientClasses": true,
  "generateClientInterfaces": true,
  "clientBaseInterface": null,
  "injectHttpClient": true,
  "disposeHttpClient": true,
  "protectedMethods": [
  ],
  "generateExceptionClasses": true,
  "exceptionClass": "ApiException",
  "wrapDtoExceptions": true,
  "useHttpClientCreationMethod": false,
  "httpClientType": "System.Net.Http.HttpClient",
  "useHttpRequestMessageCreationMethod": true,
  "useBaseUrl": true,
  "generateBaseUrlProperty": false,
  "generateSyncMethods": false,
  "exposeJsonSerializerSettings": false,
  "clientClassAccessModifier": "public",
  "typeAccessModifier": "public",
  "generateContractsOutput": false,
  "contractsNamespace": null,
  "contractsOutputFilePath": null,
  "parameterDateTimeFormat": "s",
  "parameterDateFormat": "yyyy-MM-dd",
  "generateUpdateJsonSerializerSettingsMethod": true,
  "useRequestAndResponseSerializationSettings": false,
  "serializeTypeInformation": false,
  "queryNullValue": "",
  "className": "BillingClient",
  "operationGenerationMode": "MultipleClientsFromOperationId",
  "additionalNamespaceUsages": [
  ],
  "additionalContractNamespaceUsages": [
  ],
  "generateOptionalParameters": true,
  "generateJsonMethods": true,
  "enforceFlagEnums": false,
  "parameterArrayType": "System.Collections.Generic.IEnumerable",
  "parameterDictionaryType": "System.Collections.Generic.IDictionary",
  "responseArrayType": "System.Collections.Generic.ICollection",
  "responseDictionaryType": "System.Collections.Generic.IDictionary",
  "wrapResponses": false,
  "wrapResponseMethods": [
  ],
  "generateResponseClasses": true,
  "responseClass": "SwaggerResponse",
  "namespace": "ServiceTitan.Billing.Api.Client",
  "requiredPropertiesMustBeDefined": true,
  "dateType": "System.DateTimeOffset",
  "jsonConverters": null,
  "anyType": "object",
  "dateTimeType": "System.DateTimeOffset",
  "timeType": "System.TimeSpan",
  "timeSpanType": "System.TimeSpan",
  "arrayType": "System.Collections.Generic.ICollection",
  "arrayInstanceType": "System.Collections.ObjectModel.Collection",
  "dictionaryType": "System.Collections.Generic.IDictionary",
  "dictionaryInstanceType": "System.Collections.Generic.Dictionary",
  "arrayBaseType": "System.Collections.ObjectModel.Collection",
  "dictionaryBaseType": "System.Collections.Generic.Dictionary",
  "classStyle": "Poco",
  "generateDefaultValues": true,
  "generateDataAnnotations": true,
  "excludedTypeNames": [
  ],
  "excludedParameterNames": [
  ],
  "handleReferences": false,
  "generateImmutableArrayProperties": false,
  "generateImmutableDictionaryProperties": false,
  "jsonSerializerSettingsTransformationMethod": null,
  "inlineNamedArrays": false,
  "inlineNamedDictionaries": false,
  "inlineNamedTuples": true,
  "inlineNamedAny": false,
  "generateDtoTypes": true,
  "generateOptionalPropertiesAsNullable": false,
  "templateDirectory": null,
  "typeNameGeneratorType": null,
  "propertyNameGeneratorType": null,
  "enumNameGeneratorType": null,
  "serviceHost": null,
  "serviceSchemes": null,
  "output": "BillingClient.g.cs"
  }
}

So which option am I missing for telling NSwag to generate single class/interface?


Solution

  • The option you need to change is operationGenerationMode which you have already in your nswag.json file, but you need to change its value to SingleClientFromOperationId if you want to have one interface for multiple controllers. MultipleClientsFromOperationId which is what you currently have set, will generate a class per controller.