Base solution for your next web application
Open Closed

AddSwaggerGen options cause a duplicate key error #7490


User avatar
0
ivanosw1 created

Hi, I've upgraded from 6.9.1 to 7.1.0. The new option added to SwaggerGen in startup causes a duplicate key error: options.OperationFilter<SwaggerOperationFilter>();

System.ArgumentException: An item with the same key has already been added. Key: x-schema at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) at Uno.Framework.Web.Swagger.SwaggerOperationFilter.Apply(Operation operation, OperationFilterContext context) in C:\Uno\Git\uno-ins\framework\src\Uno.Framework.Web.Core\Swagger\SwaggerOperationFilter.cs:line 32 at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry)...

Removed the new option all works fine. What is the purpose of this new code?

This is my full configuration for swagger:

services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc("v1", new Info { Title = "Framework API", Version = "v1" });
                    options.DocInclusionPredicate((docName, description) => true);
                    options.UseReferencedDefinitionsForEnums();
                    options.ParameterFilter<SwaggerEnumParameterFilter>();
                    options.SchemaFilter<SwaggerEnumSchemaFilter>();
                    options.OperationFilter<SwaggerOperationIdFilter>();
                    //SOFTWAREUNO - Questo filtro causa un errore di duplicazione di chiave
                    //options.OperationFilter<SwaggerOperationFilter>();
                    options.CustomDefaultSchemaIdSelector();

                    //Note: This is just for showing Authorize button on the UI. 
                    //Authorize button's behaviour is handled in wwwroot/swagger/ui/index.html
                    options.AddSecurityDefinition("Bearer", new BasicAuthScheme());

                    //SOFTWAREUNO
                    options.OrderActionsBy(o => o.RelativePath);
                    options.CustomSchemaIds(o => o.FullName);
                    foreach (var file in Directory.GetFiles(_hostingEnvironment.ContentRootPath, "*.docs.xml", SearchOption.AllDirectories))
                    {
                        options.IncludeXmlComments(file);
                    }
                    options.DescribeAllEnumsAsStrings();
                });

Thank you Ivano


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    May be related to this https://github.com/aspnetzero/aspnet-zero-core/issues/2458

  • User Avatar
    0
    ivanosw1 created

    No, the code is the same and the problem happens in SwaggerOperationFilter.cs file.

    Removed the new option all works fine. What is the purpose of this new code?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ivanosw1

    It simplyfies Enum definitions and allows Angular client app to use more meaningful Enum names. The line options.CustomSchemaIds(o => o.FullName); can cause the problem in your case as well.

  • User Avatar
    0
    ivanosw1 created

    I'm not an expert of Swagger and the default values works well for me until last update.

    I must use options.CustomSchemaIds(o => o.FullName) because we have some duplicate schemaIds in our dtos otherwise I have the Conflicting schemaIds error.

    If you want to see for enums the text instead of the values, I use options.DescribeAllEnumsAsStrings(); and all enum are showed as text:

    Uno.Foundation.Core.ImageSizeTypestring $ref: #/definitions/Uno.Foundation.Core.ImageSizeType Enum: [ Big, Medium, Small ] <--- 1,2,3

    Also swagger show a dropdown with literal values when you try some rest api. Maybe it is a duplicate functionality?

  • User Avatar
    0
    ismcagdas created
    Support Team

    We had several issue when using CustomSchemaIds similar to https://github.com/aspnetzero/aspnet-zero-core/issues/2205

    Here is the full list https://github.com/aspnetzero/aspnet-zero-core/issues?q=is:issue+CustomSchemaIds+is:closed

    If your solution works for you, you can keep it that way.