dotnet / dotnet/aspnetcore

ApiExplorer provides no reference type nullability info for API response types

Open
#49,971 4 comments 1 reaction 0 assignees View on GitHub
area-mvc feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I'm using ApiExplorer to get info about the endpoints in my application. For parameters (e.g. route/query) and the request body I can take the `ApiParameterDescription` and try to cast it to `IPropertyInfoParameterDescriptor` or `IParameterInfoParameterDescriptor` to get the corresponding `PropertyInfo` or `ParameterInfo`. With these, I can use `NullabilityInfoContext` to get the `NullabilityInfo` for the API parameter. This provides nullability info about reference types when using nullable reference types.

However, for the response types in `ApiDescription.SupportedResponseTypes` I haven't been able to get this information. I need this information to know if the API returns stuff that can be null or not. If an endpoint returns e.g. `Dictionary`, I want to know that the dictionary values can be null.

I could try looking into the `ActionDescriptor` to find the `MethodInfo` and get the return parameter info from there, but I'm not sure if that works for e.g. Minimal API and it definitely won't work if the endpoint has defined response types using `ProducesResponseTypeAttribute`.

### Describe the solution you'd like

To get nullability info, you need to pass one of the following to a `NullabilityInfoContext`:
- `EventInfo`
- `FieldInfo`
- `ParameterInfo`
- `PropertyInfo`

`ApiResponseType` (the type of the elements inside `ApiDescription.SupportedResponseTypes`) needs to contain one of these. Could also be one or multiple subtypes of `ApiResponseType` containing it, in the same way subtypes of `ApiParameterDescription` can be cast to `IPropertyInfoParameterDescriptor` or `IParameterInfoParameterDescriptor` and contain a `PropertyInfo` or `ParameterInfo`.

---

Obviously to get the info, you also need to be able to provide the info. When only using the return type of the action method, this is trivial (as mentioned before, get the return `ParameterInfo` from the `MethodInfo`).

But when using `ProducesResponseTypeAttribute` it's not. Right now the response type is passed to the attribute as a type parameter and that doesn't allow passing nullable information. The attributes `ProducesAttribute`, `ProducesDefaultResponseType` and `ProducesErrorResponseType` probably also need to support this.

### Additional context

_No response_

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.