Skip to content

ApiExplorer provides no reference type nullability info for API response types #49971

Open
@RdJNL

Description

@RdJNL

Is there an existing issue for this?

  • 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<int, string?>, 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions