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.

KeyNotFoundException: The given key was not present in the dictionary.

See original GitHub issue

Hi Guys,

Today we’ve got the following exception:

AutoRest code generation utility [version: 2.0.4413; node: v12.14.0]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
   Loading AutoRest core      'C:\Users\yahor_si\.autorest\@microsoft.azure_autorest-core@2.0.4413\node_modules\@microsoft.azure\autorest-core\dist' (2.0.4413)
   Loading AutoRest extension '@microsoft.azure/autorest.typescript' (~4.2.0->4.2.4)
   Loading AutoRest extension '@microsoft.azure/autorest.modeler' (2.3.51->2.3.51)
FATAL: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at AutoRest.Modeler.SwaggerModeler.Build(ServiceDefinition serviceDefinition) in c:\publish\autorest.modeler\src\SwaggerModeler.cs:line 142
   at AutoRest.Modeler.Program.<ProcessInternal>d__2.MoveNext() in c:\publish\autorest.modeler\src\Program.cs:line 60
--- 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 NewPlugin.<Process>d__15.MoveNext()
FATAL: typescript/imodeler1 - FAILED
FATAL: Error: Plugin imodeler1 reported failure.
Process() cancelled due to exception : Plugin imodeler1 reported failure.
  Error: Plugin imodeler1 reported failure.

The command line we use looks like this

$SwaggerSpec  = "src/app/Api/TeamGateApiSwaggerSpec.json"
$OutputFolder = "."
$SecureSourceFolder = "src/app/Api/Secure"
$UnsecureSourceFolder = "src/app/Api/Unsecure"

# Generate Client with Auth support
autorest --add-credentials --source-code-folder-path=${SecureSourceFolder} --override-client-name=TeamGateApiSecured   --input-file=${SwaggerSpec} --typescript --output-folder=${OutputFolder}

And swagger spec is attached in the zip TeamGateApiSwaggerSpec.zip

Any ideas on how we could fix it are really welcome! Thanks!

PS: And of course thank you for the cool tool! PPS: Sorry if that is a wrong repo for this issue

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
spareilleuxcommented, Mar 10, 2020

If you use NSwag, you can do something like:

SimplifyAllOfPropertiesProcessor.cs

using System.Collections.Immutable;
using System.Linq;

using NJsonSchema.Generation;

namespace YourNamespace.Swagger
{
    public class SimplifyAllOfPropertiesProcessor : ISchemaProcessor
    {
        public void Process(SchemaProcessorContext context)
        {
            var schema = context.Schema;
            var allOfProperties = 
                schema.Properties
                      .Where(x => x.Value.AllOf != null && x.Value.AllOf.Count == 1)
                      .ToImmutableArray();

            foreach (var allOfProperty in allOfProperties)
            {
                allOfProperty.Value.Reference = allOfProperty.Value.AllOf.First().Reference;
                allOfProperty.Value.AllOf.Clear();
            }
        }
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   services.AddSwaggerDocument(config => config.SchemaProcessors.Add(new SimplifyAllOfPropertiesProcessor());

   // ...
}
1reaction
oldercommented, Mar 3, 2020

Using allOf to refer to one is downright strange (especially since a direct $ref is far simpler, and so much cleaner to read.)

@fearthecowboy Here’s swashbuckle author explains rationale behind this (I resolved my own issue using advice from this thread, just wanted to add this as a reference).

Read more comments on GitHub >

github_iconTop Results From Across the Web

The given key was not present in the dictionary. Which key?
This exception is thrown when you try to index to something that isn't there, for example: Dictionary<String, String> test = new ...
Read more >
KeyNotFoundException: The given key was not present in the ...
This problem tends to happen if you are initially populating the dictionary from a database query (or some other external data source), and ......
Read more >
C# KeyNotFoundException: Key Not Present in Dictionary
KeyNotFoundException. In a C# program, a KeyNotFoundException was thrown. This is likely caused by a lookup done on a key (one that is...
Read more >
The given key was not present in the dictionary. - Unity Forum
Hello, i like to have a way to find out what key was not found and write it to a logfile when i...
Read more >
The given key was not present in the dictionary - CodeProject
Hi, Actually this exception occur when you are accessing Dictionary for Incorrect value. Here i have created one code snippet for ...
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