RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

Invalid generated method names (with brackets) #1776

Open semso-software opened 5 years ago

semso-software commented 5 years ago

Hello @RSuter

I was trying generate a c# client class from a swagger json file. The tool generates the client nicely but it won't build because of brackets in the method names.

System.Threading.Tasks.Task<ApiResponse> OrdersAddedOnOn}Async(System.DateTime added_on,

My nswag file:

"codeGenerators": {
    "swaggerToCSharpClient": {
      "clientBaseClass": null,
      "configurationClass": null,
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateDtoTypes": true,
      "injectHttpClient": true,
      "disposeHttpClient": true,
      "protectedMethods": [],
      "generateExceptionClasses": true,
      "exceptionClass": "SwaggerException",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": false,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": false,
      "useBaseUrl": true,
      "generateBaseUrlProperty": true,
      "generateSyncMethods": false,
      "exposeJsonSerializerSettings": false,
      "clientClassAccessModifier": "public",
      "typeAccessModifier": "public",
      "generateContractsOutput": false,
      "contractsNamespace": null,
      "contractsOutputFilePath": null,
      "parameterDateTimeFormat": "s",
      "generateUpdateJsonSerializerSettingsMethod": true,
      "serializeTypeInformation": false,
      "queryNullValue": "",
      "className": "{controller}Client",
      "operationGenerationMode": "SingleClientFromPathSegments",
      "additionalNamespaceUsages": [],
      "additionalContractNamespaceUsages": [],
      "generateOptionalParameters": true,
      "generateJsonMethods": true,
      "enforceFlagEnums": false,
      "parameterArrayType": "System.Collections.Generic.IEnumerable",
      "parameterDictionaryType": "System.Collections.Generic.IDictionary",
      "responseArrayType": "System.Collections.ObjectModel.ObservableCollection",
      "responseDictionaryType": "System.Collections.Generic.Dictionary",
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "namespace": "MyNamespace",
      "requiredPropertiesMustBeDefined": true,
      "dateType": "System.DateTime",
      "jsonConverters": null,
      "dateTimeType": "System.DateTime",
      "timeType": "System.TimeSpan",
      "timeSpanType": "System.TimeSpan",
      "arrayType": "System.Collections.ObjectModel.ObservableCollection",
      "arrayInstanceType": null,
      "dictionaryType": "System.Collections.Generic.Dictionary",
      "dictionaryInstanceType": null,
      "arrayBaseType": "System.Collections.ObjectModel.ObservableCollection",
      "dictionaryBaseType": "System.Collections.Generic.Dictionary",
      "classStyle": "Inpc",
      "generateDefaultValues": true,
      "generateDataAnnotations": false,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateImmutableArrayProperties": true,
      "generateImmutableDictionaryProperties": true,
      "jsonSerializerSettingsTransformationMethod": null,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "../../../Projecten/temp/OpenCartClient/Client.cs"
    }

The json Sorry cannot post the json body because it's to long, here by the URI http://api.opencart-api.com/demo/admin/index.json

Am i missing an parameter or is the provided json invalid?

RicoSuter commented 5 years ago

The problem is your

  "operationGenerationMode": "SingleClientFromPathSegments",

https://github.com/RSuter/NSwag/blob/c2c3ad24fd1ef4e434a4a2fdc7c224c0c6f61771/src/NSwag.CodeGeneration/OperationNameGenerators/SingleClientFromPathSegmentsOperationNameGenerator.cs

Not sure whether one of the available modes works good with your spec. Otherwise you'd need to implement your own...

Available modes: https://github.com/RSuter/NSwag/blob/c2c3ad24fd1ef4e434a4a2fdc7c224c0c6f61771/src/NSwag.Commands/Commands/CodeGeneration/OperationGenerationMode.cs

RicoSuter commented 5 years ago

The spec seems to be fine.

semso-software commented 5 years ago

Thank you for looking into this. Is there any documentation on writing our own implementation?

RicoSuter commented 5 years ago

Just implement the interface to your liking and assign an instance in the settings property