RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.79k stars 1.29k forks source link

NSwagStudio throws an System.ArgumentNullException #3067

Open IngFie opened 4 years ago

IngFie commented 4 years ago

When I try to create a CSharp Client output from this Here Routing API OpenAPI specification, a System.ArgumentNullException is thrown, when I press the button 'Generate Outputs' or 'Gererate Files'.

Attached you find my NSwagStudio file. Routing.zip

RicoSuter commented 4 years ago

Can you provide a stacktrace?

IngFie commented 4 years ago

No problem, here is the stacktrace. Created with NSwagStudio 13.8.1.0

System.ArgumentNullException: Value cannot be null. (Parameter 'key')

Runtime: NetCore31 at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector) at NJsonSchema.JsonPathUtilities.GetJsonPaths(Object rootObject, IEnumerable1 searchedObjects, IContractResolver contractResolver) at NJsonSchema.JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(Object rootObject, Boolean removeExternalReferences, IContractResolver contractResolver) at NJsonSchema.Infrastructure.JsonSchemaSerialization.ToJson(Object obj, SchemaType schemaType, IContractResolver contractResolver, Formatting formatting) at NSwag.OpenApiDocument.ToJson(SchemaType schemaType, Formatting formatting) in C:\projects\nswag\src\NSwag.Core\OpenApiDocument.cs:line 127 at NSwag.OpenApiDocument.ToJson(SchemaType schemaType) in C:\projects\nswag\src\NSwag.Core\OpenApiDocument.cs:line 115 at NSwag.OpenApiDocument.ToJson() in C:\projects\nswag\src\NSwag.Core\OpenApiDocument.cs:line 106 at NSwag.Commands.OutputCommandExtensions.<>c__DisplayClass1_0.b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\OutputCommandExtensions.cs:line 28 at NSwag.Commands.OutputCommandExtensions.TryWriteFileOutputAsync(IOutputCommand command, String path, IConsoleHost host, NewLineBehavior newLineBehavior, Func`1 generator) in C:\projects\nswag\src\NSwag.Commands\Commands\OutputCommandExtensions.cs:line 41 at NSwag.Commands.Generation.FromDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\FromDocumentCommand.cs:line 55 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 commented 4 years ago

We need to repro/fix this in NJS

arturomonge commented 4 years ago

I'm getting this error too. It has to do with referencing examples in the components section. If I remove the references to the examples (while leaving the actual examples in the component sections) the error goes away. For example,

paths:
  /contacts:
    get:
      summary: List all contacts
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ContactList'
              examples:
                Contact List:
                  $ref: '#/components/examples/ContactList'
        '400':
          $ref: '#/components/responses/BadRequest'

This causes the error. If I simply remove this:

              examples:
                Contact List:
                  $ref: '#/components/examples/ContactList'

the error is gone.

I'm attaching a simple test case where this can be reproduced. Not sure if this helps. test-case.zip