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

[API Proposal]: "Multipart/form-data" serializer #104050

Open
Mr0N opened this issue Jun 26, 2024 · 3 comments
Open

[API Proposal]: "Multipart/form-data" serializer #104050

Mr0N opened this issue Jun 26, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Serialization
Milestone

Comments

@Mr0N
Copy link

Mr0N commented Jun 26, 2024

Background and motivation

I propose to add the "Multipart/form-data" serializer; this is just an example to make it clear what I am suggesting.

API Proposal

using System;
using System.Collections.Concurrent;
using System.Reflection;

using var client = new HttpClient();
//var form = new MultipartFormDataContent();
var obj = new MultipartContentSerialization();
var multiForm = obj.Serialization(new Info() { FirstName = "12345", LastName = "12345", Hash = "hash" });
client.PostAsync("https://google.com/test", multiForm).GetAwaiter().GetResult();

class MultipartContentSerialization
{
    public MultipartFormDataContent Serialization<T>(T obj)
    {
        var form = new MultipartFormDataContent();
        var property = obj.GetType()
           .GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (var item in property)
        {
            object propertyObj = item.GetValue(obj);
            var result = item.GetCustomAttribute<NamePropertyMultiformAttribute>();
            if (propertyObj is string res)
            {
                if (result is null)
                    form.Add(new StringContent(res), item.Name);
                else
                    form.Add(new StringContent(res), result.Name);
            }
            else
            {
                throw new Exception("This type is not support");
            }

        }
        return form;
    }
}
class NamePropertyMultiformAttribute(string name) : Attribute
{
    public string Name => name;
}
class Info
{
    public required string FirstName { set; get; }
    public required string LastName { set; get; }
    [NamePropertyMultiformAttribute("User Id")]
    public required string Hash { set; get; }
}

API Usage

Alternative Designs

No response

Risks

No response

@Mr0N Mr0N added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 26, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 26, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 26, 2024
@colejohnson66
Copy link

Any API that uses reflection is probably not a good one. It’s slow and not trimming/AOT safe.

@Mr0N
Copy link
Author

Mr0N commented Jun 28, 2024

Any API that uses reflection is probably not a good one. It’s slow and not trimming/AOT safe.

Well, I'm not suggesting using reflection for this, it's just an example. I suggest using IL generation and similar approaches. I'm not sure what System.Text.Json uses, but it's quite fast

@colejohnson66
Copy link

In that case, your API proposal is wrongly written. You don't need to provide working code, just the API "shape"; You only need to explain what it does. As is, your proposal reads like reflection would be required.

@vcsjones vcsjones removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 1, 2024
@stephentoub stephentoub added this to the Future milestone Jul 19, 2024
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-Serialization
Projects
None yet
Development

No branches or pull requests

5 participants