Nswag: Q: What are the rules to use nswag to avoid exceptions

Created on 12 Oct 2017  路  19Comments  路  Source: RicoSuter/NSwag

Hello,
I have been using nswag from the command line for some time now. Today, out of a sudden, I started getting this error:

``` System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) at NSwag.CodeGeneration.TypeScript.Templates.RequestBodyTemplate.TransformText() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\RequestBodyTemplate.tt:line 2 at NSwag.CodeGeneration.TypeScript.Templates.AngularClientTemplate.TransformText() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.tt:line 50 at NSwag.CodeGeneration.TypeScript.Templates.AngularClientTemplate.Render() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.Extensions.cs:l ine 18 at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type) in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration\ClientGeneratorBa
se.cs:line 90
at NSwag.Commands.SwaggerToTypeScriptClientCommand.<b__100_0>d.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 265
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerToTypeScriptClientCommand.d__100.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 256
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerToTypeScriptClientCommand.d__99.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 249
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.NSwagDocumentBase.d__28.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\NSwagDocumentBase.cs:line 187
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommandBase.d__5.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommandBase.cs:
line 52
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommandBase.d__4.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommandBase.cs:line 39
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.d__12.MoveNext() in C:\projects\nconsole\src\NConsole\CommandLineProcessor.cs:line 139
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.d__11.MoveNext() in C:\projects\nconsole\src\NConsole\CommandLineProcessor.cs:line 107
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\Data\Projects\NSwag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 59 ---> (Inner Exception #0) System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) at NSwag.CodeGeneration.TypeScript.Templates.RequestBodyTemplate.TransformText() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\RequestBodyTemplate.tt:line 2 at NSwag.CodeGeneration.TypeScript.Templates.AngularClientTemplate.TransformText() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.tt:line 50 at NSwag.CodeGeneration.TypeScript.Templates.AngularClientTemplate.Render() in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.Extensions.cs:l ine 18 at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateFile(SwaggerDocument document, ClientGeneratorOutputType type) in C:\Data\Projects\NSwag\src\NSwag.CodeGeneration\ClientGeneratorBa
se.cs:line 90
at NSwag.Commands.SwaggerToTypeScriptClientCommand.<b__100_0>d.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 265
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerToTypeScriptClientCommand.d__100.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 256
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.SwaggerToTypeScriptClientCommand.d__99.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\SwaggerToTypeScriptClientCommand.cs:line 249
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.NSwagDocumentBase.d__28.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\NSwagDocumentBase.cs:line 187
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommandBase.d__5.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommandBase.cs:
line 52
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NSwag.Commands.Document.ExecuteDocumentCommandBase.d__4.MoveNext() in C:\Data\Projects\NSwag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommandBase.cs:line 39
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.d__12.MoveNext() in C:\projects\nconsole\src\NConsole\CommandLineProcessor.cs:line 139
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NConsole.CommandLineProcessor.d__11.MoveNext() in C:\projects\nconsole\src\NConsole\CommandLineProcessor.cs:line 107<---
child_process.js:524
throw err;
^

Error: Command failed: "D:\Workspace...\node_modules\nswag\bin/binaries/full/nswag.exe" run
at checkExecSyncError (child_process.js:481:13)
at Object.execSync (child_process.js:521:13)
at Object. (D:\Workspace...\node_modules\nswag\bin\nswag.js:27:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:390:7)
````

I looked thoroughly in my code and I cannot find any duplicate method names in my controllers.

Is there another reason for such an exception to happen? What if 2 different method names but with the same input type, does that cause any problem?

Thanks

bug

All 19 comments

What version are you using? Did you just update NSwag? Can you provide a sample where this can be reproduced?

Thanks @RSuter for your feedback.
I am developing an ASP.NET Core (Full .NET) + Angular 2.

Everything was working fine until last night, dunno what happened and I started getting this error.

In the package,json, the version of NSWAG is: "nswag": "10.6.0"

Is there a way to debug the generation and see where it is breaking?

I can pass to you the json file. would that help? How should I pass it?

Thanks

Have you tried to use the latest NSwag version (we are currently at v11.9.2)?
You can send it via email [email protected] if it shouldnt be public... Should be simple to fix (or troubleshoot) as soon as I know where the problem is...

@RSuter
Email sent. Thanks a lot for your swift feedback.

Ok, the problem is "ApiServicesAppCheckDeletionHelperCheckIfEntityUsedPost" which has two Body parameters...

What does it mean to have 2 Body parameters?

Also, what if I don't want to generate TS code for this service, This should be an internal one for internal use, I don't know how it got generated...

A HTTP response can only have one body payload (it's THE payload), so it is not allowed to have two body parameters. How are you generating the Swagger spec?

I understand. Everything was working fine until I added this helper service.

I use this command: "..\node_modules.bin\nswag" run

Based on a config file:
{ "swaggerGenerator": { "fromSwagger": { "url": "http://localhost:22742/swagger/v1/swagger.json", "output": null } }, "codeGenerators": { "swaggerToTypeScriptClient": { "className": "{controller}ServiceProxy", "moduleName": "", "namespace": "", "typeScriptVersion": 1.8, "template": "Angular", "promiseType": "Promise", "dateTimeType": "MomentJS", "nullValue": "Undefined", "generateClientClasses": true, "generateClientInterfaces": false, "generateOptionalParameters": false, "wrapDtoExceptions": false, "useTransformOptionsMethod": false, "useTransformResultMethod": false, "generateDtoTypes": true, "operationGenerationMode": "MultipleClientsFromPathSegments", "markOptionalProperties": false, "generateCloneMethod": false, "typeStyle": "Class", "extensionCode": "service.extensions.ts", "generateDefaultValues": true, "excludedTypeNames": [], "handleReferences": false, "generateConstructorInterface": true, "importRequiredTypes": true, "baseUrlTokenName": "API_BASE_URL", "output": "../src/shared/service-proxies/service-proxies.ts" }, "swaggerToCSharpClient": { "generateClientClasses": true, "generateClientInterfaces": false, "generateDtoTypes": true, "injectHttpClient": false, "disposeHttpClient": true, "generateExceptionClasses": true, "exceptionClass": "SwaggerException", "wrapDtoExceptions": true, "useHttpClientCreationMethod": false, "httpClientType": "System.Net.Http.HttpClient", "useHttpRequestMessageCreationMethod": false, "useBaseUrl": true, "generateSyncMethods": false, "clientClassAccessModifier": "public", "generateContractsOutput": false, "className": "{controller}Client", "namespace": "MyNamespace", "generateOptionalParameters": false, "requiredPropertiesMustBeDefined": true, "dateType": "System.DateTime", "dateTimeType": "System.DateTime", "timeType": "System.TimeSpan", "timeSpanType": "System.TimeSpan", "arrayType": "System.Collections.ObjectModel.ObservableCollection", "dictionaryType": "System.Collections.Generic.Dictionary", "classStyle": "Inpc", "operationGenerationMode": "MultipleClientsFromOperationId", "generateDefaultValues": true, "generateDataAnnotations": true, "excludedTypeNames": [], "wrapResponses": false, "generateResponseClasses": true, "responseClass": "SwaggerResponse", "handleReferences": false, "generateImmutableArrayProperties": false, "generateImmutableDictionaryProperties": false, "output": null }, "swaggerToCSharpController": { "className": "{controller}", "namespace": "MyNamespace", "additionalNamespaceUsages": [ "System.Web.Http" ], "generateOptionalParameters": false, "requiredPropertiesMustBeDefined": true, "dateType": "System.DateTime", "dateTimeType": "System.DateTime", "timeType": "System.TimeSpan", "timeSpanType": "System.TimeSpan", "arrayType": "System.Collections.Generic.IEnumerable", "dictionaryType": "System.Collections.Generic.Dictionary", "classStyle": "Inpc", "operationGenerationMode": "MultipleClientsFromOperationId", "generateDefaultValues": true, "generateDataAnnotations": true, "excludedTypeNames": [], "wrapResponses": false, "generateResponseClasses": true, "responseClass": "SwaggerResponse", "handleReferences": false, "generateImmutableArrayProperties": false, "generateImmutableDictionaryProperties": false, "output": null } } }

Yes, the problem is the spec which is coming from "http://localhost:22742/swagger/v1/swagger.json", how is this generated?

The problem here: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields-4

There can be one "body" parameter at most.

OK. the Swagger is configured in the Startup class of the ASP.NET Core (.NET full).

Is that what you are asking for?

services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Title = "App API", Version = "v1" }); options.DocInclusionPredicate((docName, description) => true); });

// Enable middleware to serve generated Swagger as a JSON endpoint app.UseSwagger(); // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.) app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "App API V1"); }); //URL: /swagger

I see, you are using Swashbuckle to generate the swagger spec (and not NSwag). Somehow Swashbuckle generates two body params. You have to check its documentation or try NSwag鈥檚 swagger middleware (https://github.com/RSuter/NSwag/wiki/Middlewares)

Great. That was used in the framework I am using. I will see if I can switch. Thanks a lot for your time and quick response.

Would I be able to keep on using same client tool correct? The above middleware is just for json generation.

What client tool ?

the nswag one that I run from inside command line to generate the TypeScript file.

I went to the referenced Url above, the middleware has the same Api like the one I am using "UseSwagger()". isn't it the same one?

I think the url is the same (you canconfigure it) - the produced spec may differ (eg only one body param) but should be the same...

Thanks a lot. I will give it a try.

Closed because it is a problem with Swashbuckle.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rh78 picture rh78  路  3Comments

PabloInNz picture PabloInNz  路  4Comments

p0wertiger picture p0wertiger  路  3Comments

RawsomeGH picture RawsomeGH  路  4Comments

Peter554 picture Peter554  路  3Comments