Azure / Azure/azure-functions-openapi-extension

OpenApiResponseWithBody and aliased Dictionary type triggers error: Index was outside the bounds of the array

Open
#313 12 comments 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
388
Forks
202
PR merge metrics
No merged PRs in 30d

Description

**Describe the issue**

See minimal repro at https://github.com/johnnyreilly/azure-functions-openapi-extension-error-repro

Consider the following:

```cs
public class Alias : System.Collections.Generic.Dictionary {
}

public partial class IsBroken {
public Alias? Attributes { get; set; } = default!;
}

public partial class IsWorking {
public System.Collections.Generic.Dictionary? Attributes { get; set; } = default!;
}
```

`IsWorking` and `IsBroken` are effectively the same. But they trigger different behaviour when used in concert with `OpenApiResponseWithBody`. Here is `IsBroken`:

```
[OpenApiOperation(operationId: "greeting", Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiParameter("name", Type = typeof(string), In = ParameterLocation.Query, Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(IsBroken), Summary = "The response", Description = "This returns the response")]

[FunctionName("NetCoreApp31HttpTrigger")]
public async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "greetings")] HttpRequest req,
ILogger log)
{
await Task.Yield();
log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

return new OkObjectResult($"hi {name}");
}
```

When you request the `swagger.json` you receive:

```
[2021-11-10T17:39:55.760Z] swagger.json was requested.
[2021-11-10T17:39:55.848Z] Index was outside the bounds of the array.
```

However `IsWorking` ... works.

```
[OpenApiOperation(operationId: "greeting", Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiParameter("name", Type = typeof(string), In = ParameterLocation.Query, Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(IsWorking), Summary = "The response", Description = "This returns the response")]

[FunctionName("NetCoreApp31HttpTrigger")]
public async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "greetings")] HttpRequest req,
ILogger log)
{
await Task.Yield();
log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["name"];

return new OkObjectResult($"hi {name}");
}
```

**To Reproduce**

There's a reproduction repo here: https://github.com/johnnyreilly/azure-functions-openapi-extension-error-repro

**Expected behavior**

`IsBroken` should have the same behaviour as `IsWorking` - it should not trigger the "Index was outside the bounds of the array" error

**Screenshots**

N/A

**Environment (please complete the following information, if applicable):**

N/A

**Additional context**

Having tested historic versions of the package, it looks like it broke between `0.8.1-preview` and `0.9.0-preview`. Rolling back to `0.8.1-preview` makes things work.

I wonder if https://github.com/Azure/azure-functions-openapi-extension/pull/247 might be the cause?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.