Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
RdJNL opened this issue Aug 9, 2023 · 4 comments
Open
1 task done
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi

Comments

@RdJNL
Copy link

RdJNL commented Aug 9, 2023

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

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Aug 9, 2023
@RdJNL
Copy link
Author

RdJNL commented Aug 10, 2023

Small addition: instead of a FieldInfo/ParameterInfo/PropertyInfo, the ApiResponseType could also contain a NullabilityInfo directly. In this case it'd make sense to also have the ApiParameterDescription contain a NullabilityInfo.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@RdJNL
Copy link
Author

RdJNL commented Feb 6, 2024

This is not a PR...

@wtgodbe Help please...

@martincostello
Copy link
Member

#53841

@RdJNL
Copy link
Author

RdJNL commented Feb 6, 2024

Oh, fixed quickly, good :)

The label still needs to be removed once the new code is live.

@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

No branches or pull requests

4 participants