question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate return code declaration causes nasty exception

See original GitHub issue

A controller might have several reasons to return a bad request status and in our project had them declared like below which is handled OK by Swagger. But switching to NSwag was problematic since it caused an exception and it was really hard knowing what NSwag was unhappy about. I had to clone nswag repo and debug it to find what was the problem (which then could trivially be corrected).

A proper error message to user would have saved a couple of hours.

	/// <returns>Updated insured object.</returns>
	/// <response code="400">Bad request, missing information or similar.</response>
	/// <response code="400">Unknown category.</response>
	/// <response code="409">Attempt to update read-only properties.</response>

The exception

System.InvalidOperationException: Sequence contains more than one matching element

Runtime: NetCore31
   at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at NSwag.Generation.Processors.OperationResponseProcessorBase.GetResponseXmlDocsElement(MethodInfo methodInfo, String responseCode) in C:\projects\nswag\src\NSwag.Generation\Processors\OperationResponseProcessorBase.cs:line 92
   at NSwag.Generation.Processors.OperationResponseProcessorBase.UpdateResponseDescription(OperationProcessorContext operationProcessorContext) in C:\projects\nswag\src\NSwag.Generation\Processors\OperationResponseProcessorBase.cs:line 59
   at NSwag.Generation.WebApi.Processors.OperationResponseProcessor.Process(OperationProcessorContext context) in C:\projects\nswag\src\NSwag.Generation.WebApi\Processors\OperationResponseProcessor.cs:line 52
   at NSwag.Generation.WebApi.WebApiOpenApiDocumentGenerator.RunOperationProcessors(OpenApiDocument document, Type controllerType, MethodInfo methodInfo, OpenApiOperationDescription operationDescription, List`1 allOperations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver) in C:\projects\nswag\src\NSwag.Generation.WebApi\WebApiOpenApiDocumentGenerator.cs:line 241
   at NSwag.Generation.WebApi.WebApiOpenApiDocumentGenerator.AddOperationDescriptionsToDocument(OpenApiDocument document, Type controllerType, List`1 operations, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver) in C:\projects\nswag\src\NSwag.Generation.WebApi\WebApiOpenApiDocumentGenerator.cs:line 211
   at NSwag.Generation.WebApi.WebApiOpenApiDocumentGenerator.GenerateForController(OpenApiDocument document, Type controllerType, OpenApiDocumentGenerator swaggerGenerator, OpenApiSchemaResolver schemaResolver) in C:\projects\nswag\src\NSwag.Generation.WebApi\WebApiOpenApiDocumentGenerator.cs:line 199
   at NSwag.Generation.WebApi.WebApiOpenApiDocumentGenerator.GenerateForControllersAsync(IEnumerable`1 controllerTypes) in C:\projects\nswag\src\NSwag.Generation.WebApi\WebApiOpenApiDocumentGenerator.cs:line 88
   at NSwag.Commands.Generation.WebApi.WebApiToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\WebApi\WebApiToOpenApiCommand.cs:line 109
   at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 76
   at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at NSwag.Commands.IsolatedCommandBase`1.RunIsolatedAsync(String configurationFile) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
   at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 47
   at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 280
   at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81
   at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86
   at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32
   at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
   at NConsole.CommandLineProcessor.Process(String[] args, Object input)
   at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56


Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alalloocommented, Oct 5, 2020

Absolutely, and that was the fix I did - this issue is only a suggestion to make the cause of the problem be understandable from the returned error message since it took me a really long time to figure out what the problem was, from diving into nswag source code.

0reactions
BenJenkinsoncommented, Apr 25, 2023

I’ve also run into this problem today.

I agree that a more descriptive error message/console output from NSwag would be really useful in tracking down which endpoint it was choking on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C++ - Arguments for Exceptions over Return Codes
IMHO, the #1 reason to prefer exceptions over return codes is you can't ... bubble that damn retCode to the top if something...
Read more >
Why are exceptions considered better than explicit error ...
Yes, this mean exceptions might sometimes make it harder to write correct code. Writing a bad code with error code is easy, writing...
Read more >
Why does upsert cause a DUPLICATE_VALUE error?
When I upsert two records with identical Name/ID, but otherwise different values, in two separate upsert statement, one record results, as ...
Read more >
Return Code 18 - EXCLUSIVE CONTROL DUPLICATE
... IDAETPGM01 - INTERR Bad return code from $WTO or $SCPMSG · IDAETPGM02 - INTERR Statement marker error · IDAETRUN01 - Non-Ideal program ......
Read more >
Exceptions and Error Handling, C++ FAQ
Although the return code technique is sometimes the most appropriate error handling technique, there are some nasty side effects to adding unnecessary if ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found