-
-
Notifications
You must be signed in to change notification settings - Fork 748
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
feature: Add support for custom formating when flattening complex object to query parameters #1258
Comments
I found out that what I want is achievable by specifying empty format on public class InfoDto
{
public string Name { get; set; }
[Query(Format = "yyyy-M-d")]
public DateTime Date { get; set; }
[Query(Format = "")] // added empty format specification
public Size CarSize { get; set; }
} and by adding public class Size
{
public static Size Small = new Size("small");
public static Size Medium = new Size("medium");
public static Size Large = new Size("large");
public string Key { get; }
private Size(string key)
{
Key = key;
}
// added ToString() implementation
public override string ToString()
{
return Key;
}
} (without overriding Now the URL produced by refit looks like I want - |
I just noticed that the above trick does not work if instead of DTO class the method consumes the values as individual arguments aka. if I change the method signature to: public interface IApi
{
[Get("/info")]
Task<string> GetInfo(string name, [Query(Format = "yyyy-M-d")] DateTime date, [Query(Format = "")] Size carSize);
} and call the api var response = await api.GetInfo("John Doe", DateTime.Now, Size.Medium); the requested URL is |
I am running into this except specifically I want to apply my System.Text.Json For now I can manually specify each overridden alias (or use snake case property names, which feels weird in "native" .NET userland). After all, it's an implementation detail of the API. What I was hoping for was something like edit: Looks like there's no way to override this behavior in a centralized location 😞 refit/Refit/RequestBuilderImplementation.cs Lines 381 to 385 in 61a95de
|
@kamranayub have tried create a custom implementation of |
Thanks, it's been awhile but I have an API I'd like to convert to Refit where I may need to do that. |
@ChrisPulman this should be closed |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
It would be nice if one could specify how objects are flattened when used as parameters to GET method. For example I have a refit API definition as follows:
where the
InfoDto
looks like this:the important thing to notice is that
Size
is not a regularenum
, but instead its a custom class implementation of enumeration pattern (simplified version of https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types) and it looks like this:When calling the APi using refit code below
the resulting URL looks like this
/info?Name=John%20Doe&Date=2021-10-23&CarSize.Key=medium
. Notice thatCarSize
property is flattened asCarSize.Key
in query string.It would be nice if one could specify how the individual properties are flattened, so it would be possible to tell the
CarSize
property what its name should be in URL query parameter. The aim in this case it so achieve thatCarSize
is flattened under nameCarSize
in URL, so the result looks like/info?Name=John%20Doe&Date=2021-10-23&CarSize=medium
The text was updated successfully, but these errors were encountered: