Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate return code declaration causes nasty exception #2863

Open
alalloo opened this issue May 31, 2020 · 6 comments
Open

Duplicate return code declaration causes nasty exception #2863

alalloo opened this issue May 31, 2020 · 6 comments

Comments

@alalloo
Copy link

alalloo commented May 31, 2020

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


@RicoSuter
Copy link
Owner

I'd say you should have only one of them and merge the descriptions.

@alalloo
Copy link
Author

alalloo commented 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.

@aviita
Copy link
Contributor

aviita commented Apr 4, 2023

Would also appreciate better error message. Took me also some time before I noticed to read the call stack far enough to notice this was about XML docs. Was searching for actual code changes that could have some duplication like Attributes. Anyways the information was hiding in plain sight in that call stack. Also did not find this issue with google using the error message.

Executing file 'nswag.json' with variables ''...
System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable1 source, Func2 predicate, Boolean& found)
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate)
at NSwag.Generation.Processors.OperationResponseProcessorBase.GetResponseXmlDocsElement(MethodInfo methodInfo, String responseCode) in /_/src/NSwag.Generation/Processors/OperationResponseProcessorBase.cs:line 97

aviita pushed a commit to aviita/NSwag that referenced this issue Apr 4, 2023
Duplicate response code in XML documentation response xml tag causes
somewhat hard to debug exception, which is now made more descriptive.

Did not yet figure out how to make an unit test for this one or if some
other other exception type would be more suitable than
InvalidOperationException.

Bug: Duplicate return code declaration causes nasty exception
id: RicoSuter#2863
@aviita
Copy link
Contributor

aviita commented Apr 4, 2023

Created a PR #4388. Did not test it yet though as I could not get the nswag repo's build to work.

@BenJenkinson
Copy link

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.

RicoSuter pushed a commit that referenced this issue May 2, 2023
Duplicate response code in XML documentation response xml tag causes
somewhat hard to debug exception, which is now made more descriptive.

Did not yet figure out how to make an unit test for this one or if some
other other exception type would be more suitable than
InvalidOperationException.

Bug: Duplicate return code declaration causes nasty exception
id: #2863

Co-authored-by: Antti Viita <[email protected]>
shuruev added a commit to servicetitan/NSwag that referenced this issue Nov 16, 2023
* Update README.md

* Add nswag.cmd (RicoSuter#4255)

* v13.18.1

* v13.18.2

* Remove description from OpenApiResponse when serializing for Swagger v2 (RicoSuter#4314)

* Remove description from OpenApiResponse when serializing for Swagger v2 to prevent error "Structural error at paths.somepath.get.responses.304 should NOT have additional properties additionalProperty: description" [PR].

* Remove description from OpenApiResponse when serializing for Swagger v2 to prevent error "Structural error at paths.somepath.get.responses.304 should NOT have additional properties additionalProperty: description" [PR].

* Add check for IsBindingAllowed so that BindNever attribute works correctly

* Add CS0612 to the File.Header.liquid (RicoSuter#4409)

OpenApi which contains deprecated schemas correctly generates `[Obsolete]` attribute. If the API document does not propagate `deprecate` all the way to path/method resulting c# code produce warning CS0612: '...' is obsolete

* Update Client.Class.liquid (RicoSuter#4402)

Fix Newtonsoft.Json being used instead of System.Text.Json

* Descriptive exception for duplicate XML doc line (RicoSuter#2863) (RicoSuter#4388)

Duplicate response code in XML documentation response xml tag causes
somewhat hard to debug exception, which is now made more descriptive.

Did not yet figure out how to make an unit test for this one or if some
other other exception type would be more suitable than
InvalidOperationException.

Bug: Duplicate return code declaration causes nasty exception
id: RicoSuter#2863

Co-authored-by: Antti Viita <[email protected]>

* Revert PR RicoSuter#4314

* v13.18.3

* Improve NRT support for API parameters

* Required path parameters are not nullable (when NRT is off)

* v13.18.4

* Add enforceNotNull parameter and use for required path parameters

* v13.18.5

* Add File.Footer.liquid (RicoSuter#4411)

* Swagger UI v4.18.3, closes RicoSuter#4311 and RicoSuter#4387 (v13.18.5)

* add axios abort signal (RicoSuter#4282)

* add axios abort signal

* unit test

* Add support for MSBuild transitive dependency flow RicoSuter#4026 (RicoSuter#4338)

* Use GNU tar for cache and install .NET in single step (RicoSuter#4251)

* Unwrap ValueTask<T> return types (RicoSuter#4374)

* Unwrap ValueTask<T> return types, same as Task<T>, unwrap (Value)Task<ActionResult<T>> and unify unwrapping for consistency RicoSuter#4373

* lahma suggestion for performance

* cleanup

* NJsonSchema v10.9.0

* Fix build

* Revert "Fix build"

This reverts commit 21bf926.

* Fix build

* Fix dep

* Add .NET 5 SDK

* Improve build

* v13.19.0

* fix default value docs of SerializeTypeInformation, closes RicoSuter#4480

* Add SwaggerRoutesFactory to SwaggerUi3Settings

* Disable warning CS8604 "Possible null reference argument for parameter" in generated code (RicoSuter#4422)

* Disable warning CS8604 "Possible null reference argument for parameter" in code generation file header template

* Restore warning in file footer

* Update Client.Class.liquid (RicoSuter#4471)

* Update Client.Class.liquid

fix RicoSuter#4466

* Update Client.Class.liquid

---------

Co-authored-by: Rico Suter <[email protected]>

* install .net 3.1 sdk

* v13.20.0

* Set ChocolateyPush source

* Post-merge adjustments

* Updated more Newtonsoft.Json versions, for consistency

---------

Co-authored-by: Rico Suter <[email protected]>
Co-authored-by: Patrick Hofman <[email protected]>
Co-authored-by: Jan Trejbal <[email protected]>
Co-authored-by: Jean-Luc <[email protected]>
Co-authored-by: Antti Viita <[email protected]>
Co-authored-by: Antti Viita <[email protected]>
Co-authored-by: Rico Suter <[email protected]>
Co-authored-by: Lydon Chandra <[email protected]>
Co-authored-by: Daniel Sack <[email protected]>
Co-authored-by: Marko Lahma <[email protected]>
Co-authored-by: Alasdair <[email protected]>
Co-authored-by: Noah Stolk <[email protected]>
Co-authored-by: Alireza Mahdavi <[email protected]>
lahma pushed a commit to lahma/NSwag that referenced this issue Jan 20, 2024
…coSuter#4388)

Duplicate response code in XML documentation response xml tag causes
somewhat hard to debug exception, which is now made more descriptive.

Did not yet figure out how to make an unit test for this one or if some
other other exception type would be more suitable than
InvalidOperationException.

Bug: Duplicate return code declaration causes nasty exception
id: RicoSuter#2863

Co-authored-by: Antti Viita <[email protected]>
@aviita
Copy link
Contributor

aviita commented Jun 6, 2024

Since the PR #4388 has been merged, could this issue be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants