RicoSuter/NSwag

Client code generation in NSwagStudio fails with NullReferenceException

Open

#2,530 opened on Nov 18, 2019

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (6,291 stars) (1,189 forks)batch import
help wantedtype: bug

Description

If specification contains response with defined content which has example but not have schema element code generation fails with an error

System.InvalidOperationException: Error while rendering Liquid template CSharp/Client.Class: 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.

Runtime: NetCore20
   at NSwag.CodeGeneration.Models.OperationModelBase`2.get_UnwrappedResultType() in C:\projects\nswag\src\NSwag.CodeGeneration\Models\OperationModelBase.cs:line 120
   at NSwag.CodeGeneration.CSharp.Models.CSharpOperationModel.get_ResultType() in C:\projects\nswag\src\NSwag.CodeGeneration.CSharp\Models\CSharpOperationModel.cs:line 131
   --- End of inner exception stack trace ---
...

Here is sample of invalid configuration for studio

openapi: 3.0.1
info:
  description: >-
    Test API's
  version: 1.1.2
  title: Test API
   
paths:
  /testPath:
    get:
      operationId: testId
      description: "42"
      responses:
        '200':
          description: Returns some object
          content:
            application/json:
                example: {}

And here is valid configuration (with added schema property)

openapi: 3.0.1
info:
  description: >-
    Test API's
  version: 1.1.2
  title: Test API
   
paths:
  /testPath:
    get:
      operationId: testId
      description: "42"
      responses:
        '200':
          description: Returns some object
          content:
            application/json:
                schema:
                    type: object
                example: {}

Contributor guide