-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
[API Proposal]: Add BinaryData.ContentType #87068
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationIt's often useful to know the format of the binary payload stored in BinaryData. It's also often possible for APIs returning BinaryData to know the format. For example, BinaryData bytes = blob.Download(); // blob metadata often specifies the format of the bytes stored in blob ... when the model is being serialized (to JSON) we would like to know if the bytes represent JSON or unknown binary. If JSON, we would serialize it as a JSON value. If unknown binary, we would Base64 encode it and serialize it as a string. API ProposalAdd the following members to BinaryDatapublic class BinaryData {
// new members
public string ContentType { get; } // MIME type, e.g. "application/json"
public BinaryData WithContentType(string contentType);
// new overloads of existing constructors adding contentType parameter
public BinaryData(ReadOnlyMemory<byte> data, string contentType);
public BinaryData(byte[] data, string contentType);
// new overloads of existing factory methods adding contentType parameter
public static BinaryData FromBytes(byte[] data, string contentType);
public static BinaryData FromStream(Stream stream, string contentType);
public static Task<BinaryData> FromStreamAsync(Stream stream, string contentType);
} Existing constructors and factory methods would initialize the ContentType property as follows
API UsageBinaryData bytes = blob.Download(); // blob metadata often specifies the format of the bytes stored in blob
var model = new Model();
model.Foo = bytes;
var json = model.ToJson(); // this serializes the model to JSON so we can send it in an HTTP request. BinaryData bytes = BinaryData.FromStream(stream, MediaTypeNames.Text.Plain);
blob.Upload(bytes); // sets the blob content type to text/plain Alternative DesignsNo response RisksNo response
|
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationIt's often useful to know the format of the binary payload stored in BinaryData. It's also often possible for APIs returning BinaryData to know the format. For example, BinaryData bytes = blob.Download(); // blob metadata often specifies the format of the bytes stored in blob
var model = new Model();
model.Foo = bytes;
var json = model.ToJson(); // this serializes the model to JSON so we can send it in an HTTP request. ... when the model is being serialized (to JSON) we would like to know if the bytes represent JSON or unknown binary. If JSON, we would serialize it as a JSON value. If unknown binary, we would Base64 encode it and serialize it as a string. API ProposalAdd the following members to BinaryDatapublic class BinaryData {
// new members
public string ContentType { get; } // MIME type, e.g. "application/json"
public BinaryData WithContentType(string contentType);
// new overloads of existing constructors adding contentType parameter
public BinaryData(ReadOnlyMemory<byte> data, string contentType);
public BinaryData(byte[] data, string contentType);
// new overloads of existing factory methods adding contentType parameter
public static BinaryData FromBytes(byte[] data, string contentType);
public static BinaryData FromStream(Stream stream, string contentType);
public static Task<BinaryData> FromStreamAsync(Stream stream, string contentType);
} Existing constructors and factory methods would initialize the ContentType property as follows
API UsageBinaryData bytes = blob.Download(); // blob metadata often specifies the format of the bytes stored in blob
var model = new Model();
model.Foo = bytes;
var json = model.ToJson(); // this serializes the model to JSON so we can send it in an HTTP request. BinaryData bytes = BinaryData.FromStream(stream, MediaTypeNames.Text.Plain);
blob.Upload(bytes); // sets the blob content type to text/plain Alternative DesignsNo response RisksNo response
|
public class BinaryData {
// new members
public string? MediaType { get; } // MIME type, e.g. "application/json"
public BinaryData WithMediaType(string? mediaType);
// new overloads of existing constructors adding mediaType parameter
public BinaryData(ReadOnlyMemory<byte> data, string? mediaType);
public BinaryData(byte[] data, string? mediaType);
public BinaryData(string data, string? mediaType);
// new overloads of existing factory methods adding mediaType parameter
public static BinaryData FromBytes(byte[] data, string? mediaType);
public static BinaryData FromStream(Stream stream, string? mediaType);
public static Task<BinaryData> FromStreamAsync(Stream stream, string? mediaType);
public static BinaryData FromString(string data, string? mediaType);
} |
Making this nullable is the right decision. When we are talking about blob storage, there is also another behavior to keep in mind, when the blob storage is use as static website or the container is made pu lic and blobs are accessed as web resource i.e. using a browser, then the Metadata is used to set the content type of the response. And as far as I know the blob storage when used as static web site does some inferring when the content type is not set when it is returning the blob. So having the option to not set it for blob storage makes sense. That being said, right now it is at least two calls when uploading blobs to set the content type, so having an option to set it while uploading makes it much easier. |
I think you forget to add the // new overloads of existing constructors adding mediaType parameter
public BinaryData(ReadOnlyMemory<byte> data, string? mediaType); // It is added
// new overloads of existing factory methods adding mediaType parameter
public static BinaryData FromBytes(ReadOnlyMemory<byte> data, string? mediaType); // Should it be added also? |
I want to implement this API. Can I be assigned? |
I don't think there is really a distinction between Media Type and Content Type, other than the ContentType relating to the Content-Type header. The ContentType contains Media Type values. Even the spec provides examples where the charset parameter is specified for "MediaTypes" - https://www.rfc-editor.org/rfc/rfc6838#section-4.2.1 |
/cc @bartonjs @GrabYourPitchforks wondering if we can reconsider naming the property to ContentType instead of MediaType. |
We have a complex ContentType class that contains MediaType property inside https://learn.microsoft.com/dotnet/api/system.net.mime.contenttype . So we have only the MediaType property here, not the ContentType class. |
Interesting, I don't see any mention in the spec that MediaType should not contain any parameters. https://www.rfc-editor.org/rfc/rfc6838#section-4.2.1 |
|
My question is: Should I think the issue should be reviewed to add the |
Regarding serialization, the serializer could use the data URL format to also store the content type in addition to the data: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs Of course, it could always use the data URL format from now on when serializing, even when the MediaType is null, but the deserializer still needs to be able to recognize the old format. It would be great to implement this to ensure that the type remains roundtrippable when serializing. |
I also feel like it should store the full content type (using either |
@KrzysztofCwalina, @stephentoub. Is this critical for .NET 8, or is it fine to slip to .NET 9? |
For us it would be very useful if it is in net8 |
Yes, we have projects that need that asap. If you are running out of time, we would be willing to put some resources to help finish the PR |
Resolved in #89605 |
Background and motivation
It's often useful to know the format of the binary payload stored in BinaryData. It's also often possible for APIs returning BinaryData to know the format. For example,
... when the model is being serialized (to JSON) we would like to know if the bytes represent JSON or unknown binary. If JSON, we would serialize it as a JSON value. If unknown binary, we would Base64 encode it and serialize it as a string.
API Proposal
Add the following members to BinaryData
Existing constructors and factory methods would initialize the ContentType property as follows
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: