OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

Stream properties on dynamic EDM model entities no longer appear #2577

Open kmcinnomate opened 3 years ago

kmcinnomate commented 3 years ago

It was previously possible to add stream properties to dynamic EDM model entities. This seems to no longer be possible. The properties are no longer output.

Assemblies affected

Microsoft.AspNet.OData 7.5.6 and up.

Reproduce steps

I have added a stream property to the DynamicEdmModelCreation solution from the ODataSamples project (https://github.com/OData/ODataSamples) using Microsoft.AspNet.OData 7.5.5. This is very similar to the setup we have. You can find the code here: https://github.com/kmcinnomate/ODataSamples/commit/769866e2ed1c21b0239d293fcf76326e27a4434e

Load ODataSamples\WebApiClassic\DynamicEdmModelCreation\DynamicEdmModelCreation.sln and build and run the application.

This will work. Note that both the Products entity set and the single Product entity in the output contain a stream property called Photo.

Now upgrade Microsoft.AspNet.OData to 7.5.6 (or use this commit instead).

Now build and run again.

Expected result

Product entries should contain stream properties.

Actual result

The stream properties have disappeared.

Additional detail

This seems to be related to https://github.com/OData/WebApi/pull/2401. For some reason, stream properties are no longer handled by the ODataSerializerProvider in the ODataResourceSerializer, but instead get special treatment and are sent to CreateStreamProperty that always returns null! Since this is an internal method, I don't have the option to override that and I don't really see what my options are.

To me, it seems like a bug, but of course I might be missing something.

Thanks in advance!

xuzhg commented 3 years ago

@kmcinnomate By design, the stream properties are handled in the full metadata. Can you try to retrieve them using odata.metadata=full?

kmcinnomate commented 3 years ago

@xuzhg Thanks for your response. When adding accept: application/json;odata.metadata=full, I get some seemingly default values (and no mediaContentType) in the linked example, like:

    "Photo@odata.mediaEditLink": "http://localhost:54321/odata/mydatasource/Products(1)/Photo",
    "Photo@odata.mediaReadLink": "http://localhost:54321/odata/mydatasource/Products(1)/Photo"

I would expect the values that I provided in the data source in the example, i.e.

            var stream = new ODataStreamReferenceValue
            {
                ReadLink = new Uri("https://mysite.com/abc.jpg"),
                EditLink = new Uri("https://mysite.com/abc.jpg?upload"),
                ContentType = "image/jpeg",
            };

            entity.TrySetPropertyValue("Photo", stream);

Before upgrading to 7.5.6, this was the result:

    "Photo@odata.mediaEditLink": "https://mysite.com/abc.jpg?upload",
    "Photo@odata.mediaReadLink": "https://mysite.com/abc.jpg",
    "Photo@odata.mediaContentType": "image/jpeg",

Granted, I had to jump through a couple of hoops to get it to work, as you can see in the code, but it was possible. Now I'm not sure it's possible anymore.