-
Notifications
You must be signed in to change notification settings - Fork 474
BUG: ODataInputFormatter and ODataOutputFormatter Does Not Override Expected Behavior #1750
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
Comments
Nice discovery there. I'd just like to suggest returning an empty read-only collection instead of null, as handling collections as null is usually a bad practice and can cause NullRefrrenceException on consumers. In this particular case there is no special semantic difference between null and empty (I understand there may be in other contexts though), so it's better to just return empty IMHO. |
@julealgon, totally agree with you. I prefer avoiding #no-nulls #billion-dollar-mistake |
is there any update with regards this issue? is this on the roadmap/scheduled to be fixed; we have ran into this issue when deploying non-odata and odata controllers in the same host and would like to understand if/when this would be fixed. |
@commonsensesoftware I'm not seeing the ODataInputFormatter or ODataOutputFormatter within the MvcOptions ? any additional pointers ? |
@xuzhg I think this is really important and seems like a simple fix |
Is this issue dead? |
Fixed by #2494 |
Uh oh!
There was an error while loading. Please reload this page.
The ODataInputFormatter and ODataOutputFormatter extend the built-in InputFormatter and OutputFormatter respectively, which each implement the IApiResponseTypeMetadataProvider interface used by the API Explorer. The ODataInputFormatter and ODataOutputFormatter break the expected design by allowing a configuration that does not have any supported media types. The OData implementation should be overriding the default behavior so that the base implementation from InputFormatter and OutputFormatter no longer produces exceptions.
This issue was almost certainly never previously detected because OData does not provide any API Explorer extensions.
Assemblies affected
Microsoft.AspNetCore.OData 7.0.1+
Reproduce steps
Call
ODataInputFormatter.GetSupportedContentTypes
orODataOutputFormatter.GetSupportedContentTypes
whenODataInputFormatter.SupportedMediaTypes
orODataOutputFormatter.SupportedMediaTypes
is empty.Expected result
No exception. The result should be
null
or an empty list.Actual result
InvalidOperationException is thrown.
Additional detail
This happens because the ODataInputFormatterFactory creates an ODataInputFormatter and ODataOutputFormatterFactory creates an ODataOutputFormatter which each have no supported media types. This currently only appears to be for the configured formatter to handle raw values. This is the intended design as the media type is not known upfront and can only be detected from the incoming request.
The issue is that the default behavior from the inherited InputFormatter and OutputFormatter base classes require that there be at least one supported media type. Since OData is changing the expected behavior, it should override the GetSupportedContentTypes method in its own formatters.
The suggested implementation for each formatter is:
Impact
Without this change, OData formatters break certain APIs such as the API Explorer. Callers of the GetSupportedContentTypes expect a
null
or empty result, but not an exception.Workarounds
MvcOptions.InputFormatters
and ODataOutputFormatter fromMvcOptions.OutputFormatters
when they do not have any supported media typesMvcOptions.InputFormatters
and ODataOutputFormatter inMvcOptions.OutputFormatters
when they have not supported media types with a subclass that copies all the existing values and overrides GetSupportedContentTypes correctlyThe text was updated successfully, but these errors were encountered: