-
Notifications
You must be signed in to change notification settings - Fork 51
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
MEDIUM FIX: Enable creation of multipart-form with non-string types #132
Comments
@Catalin-Andronie MultipartFormDataContent only supports limited data types. They are low level and expect "other" types to be stringified ready for transport. Normally this sort of posting would be handled by the other Post methods in RESTFulSense that supports serialization. The intent of this new method is not to bypass the older methods. Am I missing something in your use case? |
@BrianLParker I am with you on the part regarding the supported types of MultipartFormDataContent. The thing is that RESTFulSense multipart-form implementation assumes that all properties are of string type, and that's because we cast the property value to a string. internal partial class FormOrchestrationService : IFormOrchestrationService
{
private void AddStringContents(FormModel formModel)
{
...
string value = (string)this.valueService.RetrievePropertyValue(formModel.Object, property);
...
}
} What we can do instead is to remove the cast and make use of internal partial class FormOrchestrationService : IFormOrchestrationService
{
private void AddStringContents(FormModel formModel)
{
...
string value = this.valueService.RetrievePropertyValue(formModel.Object, property).ToString();
...
}
} The later approach is more robust and allow for complex data types, like for example I may have a type public class Person
{
[RESTFulStringContent(name: "firstName")]
public string FirstName { get; set; }
[RESTFulStringContent(name: "age")]
public int Age { get; set; }
[RESTFulStringContent(name: "salary")]
public Money Salary { get; set; }
}
public class Money
{
public Money(decimal amount, string currency)
{
...
}
public override string ToString()
=> $"{amount}{currency}";
}
async Task MakeRequestAsync()
{
var person = new Person
{
FirstName = "Jane Smith",
Age = 20,
Salary = new Money(200M, "EUR")
};
var httpClient = SetupHttpClient();
var apiClient = new RESTFulApiFactoryClient(httpClient);
var response = await apiClient.PostFormAsync<Person, string>("/users", person);
} I will create a test for the above example to make sure there are not corner cases to it. @BrianLParker what's your input on the use of |
Please follow the naming conventions of The Standard Team. Naming standards for Issues, Pull Requests, Commits, and Branching help us all follow along in the trail of code. |
I've checked the Category List but I do not see anything related to a new @glhays please let me know what that naming should be. |
Yeah I know sometimes it is hard to put a title on something where it may cross over. I think the MINER FIX is appropriate. |
@BrianLParker Please check the next PR which illustrates the problem and also fixes it: #133 |
Currently when trying to construct a multipart-form for a model which has properties of another type except for string type, the next exception is thrown:
Unable to cast object of type 'System.Decimal' to type 'System.String'
.Exception thrown:
The text was updated successfully, but these errors were encountered: