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

Client generation in NSwagStudio exceptions #2545

Closed
tndata opened this issue Nov 24, 2019 · 8 comments · Fixed by #3209
Closed

Client generation in NSwagStudio exceptions #2545

tndata opened this issue Nov 24, 2019 · 8 comments · Fixed by #3209
Milestone

Comments

@tndata
Copy link

tndata commented Nov 24, 2019

When I try to generate a C# .NET Core3 client using the NSwagStudio (13.1.6.0) the following OpenAPI document generates exception

https://leksaker.starweb.se/api/v2/openapi.json

I have imported it into SwaggerHub without any errors.

I have also saved it as Yaml here: http://download.edument.se/openapi.yaml (from swaggerhub and http://online.swagger.io/)

Any hint on how I can debug this?

The exception I get is:

System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Sequence contains more than one matching element

@haiduc32
Copy link
Contributor

Same problem here. Traced the source of the exception to referencing the same response in multiple places, in your case that's: 400":{"$ref":"#/components/responses/BadRequest"},
BadReqeuest is referenced multiple times.
Doesn't make any sense why it behaves that way. Waiting for someone to look at this issue.

@RicoSuter
Copy link
Owner

Do we have a stacktrace or something?

@haiduc32
Copy link
Contributor

Stack trace from running NSwagStudio (latest version) with the document provided in the first post:

System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element

Runtime: Default
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate)
at NSwag.OpenApiResponse.IsBinary(OpenApiOperation operation)
at NSwag.CodeGeneration.Models.ResponseModelBase.get_CheckChunkedStatusCode()
--- End of inner exception stack trace ---
at DotLiquid.Context.HandleError(Exception ex)
at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result) at DotLiquid.Document.Render(Context context, TextWriter result) at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters) at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters) at DotLiquid.Template.Render(RenderParameters parameters) at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render() ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source, Func2 predicate) at NSwag.OpenApiResponse.IsBinary(OpenApiOperation operation) at NSwag.CodeGeneration.Models.ResponseModelBase.get_CheckChunkedStatusCode() --- End of inner exception stack trace --- at DotLiquid.Context.HandleError(Exception ex) at DotLiquid.Block.RenderAll(List1 list, Context context, TextWriter result)
at DotLiquid.Document.Render(Context context, TextWriter result)
at DotLiquid.Template.RenderInternal(TextWriter result, RenderParameters parameters)
at DotLiquid.Template.Render(TextWriter writer, RenderParameters parameters)
at DotLiquid.Template.Render(RenderParameters parameters)
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
--- End of inner exception stack trace ---
at NJsonSchema.CodeGeneration.DefaultTemplateFactory.LiquidTemplate.Render()
at NSwag.CodeGeneration.CSharp.CSharpClientGenerator.d__8.MoveNext()
at System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 collection)
at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateAllClientTypes() at NSwag.CodeGeneration.ClientGeneratorBase3.GenerateFile(ClientGeneratorOutputType outputType)
at NSwag.Commands.CodeGeneration.SwaggerToCSharpClientCommand.<b__83_0>d.MoveNext()
--- 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.CodeGeneration.SwaggerToCSharpClientCommand.d__83.MoveNext()
--- 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.CodeGeneration.SwaggerToCSharpClientCommand.d__82.MoveNext()
--- 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.NSwagDocument.d__8.MoveNext()
--- 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.ExecuteDocumentCommand.d__9.MoveNext()
--- 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.ExecuteDocumentCommand.d__8.MoveNext()
--- 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()
--- 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()
--- 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.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args)

@i-sinister
Copy link

i-sinister commented Feb 12, 2020

NSwagStudio 13.2.2.0 fails to generate client for following spec:

openapi: '3.0.0'
paths:
  /bugs:
    get:
      summary: Returns list on NSwagStudio bugs
      responses:
        '200':
          $ref: '#/components/responses/NSwagStudioBug'
        '400':
          $ref: '#/components/responses/NSwagStudioBug'
components:
  responses:
    NSwagStudioBug:
      description: List of NSwagStudio bugs
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: '#/components/schemas/NSwagStudioBug'
  schemas:
    NSwagStudioBug:
      description: Describes NSwagStudio bug
      type: object
      required:
        - description
      properties:
        description:
          description: Bug description
          type: string
      example:
        description: https://github.com/RicoSuter/NSwag/issues/2545

Error as in previous comment. The reason is using the same multiple response for different codes.

@RicoSuter RicoSuter added this to the vNext milestone Feb 12, 2020
@haiduc32
Copy link
Contributor

haiduc32 commented Dec 8, 2020

I've hit this bug again today (forgot about it) and I can see I already commented..

Is anyone working on it, or can I take a shot at fixing it?

@jeremyVignelles
Copy link
Collaborator

If there's no open PR, you can assume nobody is working on it.
Thanks 👍

@RicoSuter
Copy link
Owner

image

So the same response object is in resolved multiple times in the ActualResponses?

@haiduc32
Copy link
Contributor

Apologies for the late reply.
Just for the record - yes, that is correct, Operation.ActualResponses will contain one item for each response (different http codes) but with the same r.Value if your responses have the same "$ref". Which is correct (IMHO). So the solution was to change the code to use the "Any" Linq method, as to the best of my understanding this bit of code just checks that the current response is not 204. I changed it to check that there is at least one response that is not 204, so that the response can be binary.

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

Successfully merging a pull request may close this issue.

5 participants