azure-functions-openapi-extension icon indicating copy to clipboard operation
azure-functions-openapi-extension copied to clipboard

System.ArgumentException: An item with the same key has already been added. Key: xyz

Open arashcomsense opened this issue 3 years ago • 12 comments

I've created this sample repo to demonstrate the exception outlined in the subject line. The details of the issue have been described in the README.md file of that repo.

Would you please share a solution because renaming public MyRequest Request { get; set; } in both MySubmittedData and YourSubmittedData classes is not feasible? Doing so will lead to other complexities and break the code's architecture (outside of this repo).

arashcomsense avatar Apr 14 '21 19:04 arashcomsense

@arashcomsense Thanks for the issue!

It's because both MySubmittedData.MyResponse and YourSubmittedData.MyResponse are considered as the same one.

I'll have a look, but no ETA at this time. If you are in time challenge, change the naming on your end can sort out the issue, if you can.

justinyoo avatar Apr 15 '21 01:04 justinyoo

@justinyoo Unfortunately we cannot change the naming due to the reasons I already shared. We prefer a fix for this issue and for that reason we are excluding those definitions from our Open Api definitions. Please keep me posted once a fix is in place.

arashcomsense avatar Apr 15 '21 11:04 arashcomsense

Related to: https://github.com/aliencube/AzureFunctions.Extensions/issues/133

justinyoo avatar Apr 15 '21 23:04 justinyoo

@justinyoo Probably it makes sense to accelerate addressing this issue.

arashcomsense avatar Apr 16 '21 11:04 arashcomsense

@justinyoo any updates on this issue?

arashcomsense avatar May 04 '21 11:05 arashcomsense

Seems like what is needed is a configuration setting and functionality similar to CustomSchemaIds as implemented here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#customize-schema-ids.

mark-comeau avatar May 05 '21 19:05 mark-comeau

Same behaviour with nested generic types. Here a test case

`

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Threading.Tasks;

namespace FunctionApp1
{
    public class Model1
    {
        [Required]
        public string RequiredProperty { get; set; }

        [JsonRequired]
        public string JsonRequiredProperty { get; set; }

        [JsonProperty(Required = Required.Always)]
        public string JsonPropertyRequiredAlwaysProperty { get; set; }
    }

    public class Model2
    {
        public string Property { get; set; }
    }

    public class Page<T>
    {
        public int TotalRecords { get; set; }

        public IEnumerable<T> Records { get; set; }
    }

    public class OkResponse<T>
    {
        public T Result { get; set; }
    }

    public static class Function2
    {
        [FunctionName(nameof(Function2.Run))]
        [OpenApiOperation(nameof(Function2.Run))]
        [OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", bodyType: typeof(OkResponse<Page<Model1>>))]
        public static Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
            ILogger log)
            => throw new NotImplementedException();

        [FunctionName(nameof(Function2.Run2))]
        [OpenApiOperation(nameof(Function2.Run2))]
        [OpenApiResponseWithBody(HttpStatusCode.OK, "application/json", bodyType: typeof(OkResponse<Page<Model2>>))]
        public static Task<IActionResult> Run2(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
            ILogger log)
            => throw new NotImplementedException();
    }
}

`

MrWolfPST avatar Sep 07 '21 17:09 MrWolfPST

This may be related #356

RealGoodAnthony avatar Feb 07 '22 15:02 RealGoodAnthony

This issue with nested generic types is blocking us from adopting the library in place of Swashbuckle, please can you provide an update on a fix for this?

absolutebandit avatar Mar 28 '22 10:03 absolutebandit

We are seeing this same issue when we updated our package from 1.0.0 to 1.3.0, What is the release date for the fix?

j03INTHECLOUD avatar Apr 11 '22 19:04 j03INTHECLOUD

We're seeing this issue using 1.3.0 as well, but in the OpenApiRequestBody,

Just adding [OpenApiRequestBody(contentType: MediaTypeNames.Application.Json, bodyType: typeof(EtlRequest))] causes this error to be displayed when attempting to view the Swagger UI.

I have done a Find All in our entire solution and we only have 1 type with that name, and double checked that I didn't duplicate the attribute, which I have not.

For now have rolled back to 1.1.0 and it's working.

willyt150 avatar May 06 '22 20:05 willyt150

I have the same issue with nested generic types. Downgrading to 1.0.0 doesn't fix it for me either.

Having these combined fails for me with the error message: An item with the same key has already been added. Key: Response_list

  • [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: MediaTypeNames.Application.Json, bodyType: typeof(Response<List<Version>>))]
  • [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: MediaTypeNames.Application.Json, bodyType: typeof(Response<List<Endpoint>>))]

I worked around the issue by changing the body types to Response<Version[]> and Response<Endpoint[]>

tijmenamsing avatar May 19 '22 15:05 tijmenamsing

#610 takes care of this issue. The fix will be released in the next v2-preview release.

justinyoo avatar Sep 19 '23 11:09 justinyoo