Azure / Azure/azure-functions-openapi-extension
An item with the same key has already been added.
- Dominant language
- C#
- Stars
- 388
- Forks
- 202
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the issue**
I was receiving this error when trying to view the API's swagger.json. The actual error was:
**An item with the same key has already been added. Key: 200**
This lead me to believe that there was a duplicate `OpenApiResponseWithBody` attribute within a Func, which turned out to be the case. I'm not sure if this should be classified as a bug since it's _operator error_, but I thought I'd call it out.
**To Reproduce**
Steps to reproduce the behavior:
Decorate an HttpTrigger func with duplicate `OpenApiResponseWithBody`/`statusCode` attributes:
```
public class Func
{
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "app/json",
bodyType: typeof(SomeResponse),
Summary = "Some Response",
Description = "This returns the created response.")]
[OpenApiResponseWithBody(
statusCode: HttpStatusCode.OK,
contentType: "app/json",
bodyType: typeof(SomeResponse),
Summary = "Some Response",
Description = "This returns the created response")]
public async Task RunAsync(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = ResourceUri)] HttpRequest req,
ILogger log)
{
}
}
```
**Expected behavior**
I expected the request to render the swagger.json file, but instead received a 500 response.
```
An item with the same key has already been added. Key: 200
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.DocumentHelper.GetOpenApiResponses(MethodInfo element, NamingStrategy namingStrategy, VisitorCollection collection, OpenApiVersionType version)
at Microsoft.Azure.WebJobs.Extensions.OpenApi.Document.Build(Assembly assembly, OpenApiVersionType version)
at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctions.RenderSwaggerDocument(OpenApiHttpTriggerContext openApiContext, HttpRequest req, String extension, ExecutionContext ctx, ILogger log)
```
**Environment (please complete the following information, if applicable):**
- OS: Windows
- Browser chrome
- Version 1.4.0
**Additional context**
The obvious fix is: _**Don't duplicate OpenApiWithResponseBody statusCode attributes**_! However, it would be nice if this generated a compile error or some other sort of warning.
Contributor guide
Assessment
This issue has not been assessed yet.