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

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

About this issue

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

Most upvoted comments

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());

   // ...
}

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).

This might be terrible swagger, but it seem to be valid one?

This is similar to what is generated by Swashbuckle 5, so to change it to something more autorest-friendly I would have to manually override schema generation for all classes with enum properties.