You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feature: Add support for custom formating when flattening complex object to query parameters #1258
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:
the resulting URL looks like this /info?Name=John%20Doe&Date=2021-10-23&CarSize.Key=medium. Notice that CarSize property is flattened as CarSize.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 that CarSize is flattened under name CarSize in URL, so the result looks like /info?Name=John%20Doe&Date=2021-10-23&CarSize=medium
I found out that what I want is achievable by specifying empty format on CarSize property:
publicclassInfoDto{publicstringName{get;set;}[Query(Format="yyyy-M-d")]publicDateTimeDate{get;set;}[Query(Format="")]// added empty format specificationpublicSizeCarSize{get;set;}}
and by adding ToString() implementation to the enumeration:
(without overriding ToString() the value in URL would be the type name aka. the result of inherited ToString() implementation)
Now the URL produced by refit looks like I want - /info?Name=John%20Doe&Date=2021-10-23&CarSize=medium, but I didn't find this behavior documented anywhere - so is this behavior intentional or just a side effect that can go away at any time?
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:
I am running into this except specifically I want to apply my System.Text.Json snake_case naming policy on the query object property parameter names automatically instead of [AliasAs]. When you have tons of objects that all follow the same convention, it's much nicer to define it in one place.
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 UrlParameterFormatter that handled the names of parameters, but this only handles parameter values (as far as I can tell). I'll keep digging to see if I can find a different workaround...
edit: Looks like there's no way to override this behavior in a centralized location 😞
@kamranayub have tried create a custom implementation of DelegatingHandler and register it via AddHttpMessageHandler<>? Then you can try to override SendAsync method and do whatever you want with the request.RequestUri
@kamranayub have tried create a custom implementation of DelegatingHandler and register it via AddHttpMessageHandler<>? Then you can try to override SendAsync method and do whatever you want with the request.RequestUri
Thanks, it's been awhile but I have an API I'd like to convert to Refit where I may need to do that.
Activity
yohny commentedon Oct 25, 2021
I found out that what I want is achievable by specifying empty format on
CarSizeproperty:and by adding
ToString()implementation to the enumeration:(without overriding
ToString()the value in URL would be the type name aka. the result of inheritedToString()implementation)Now the URL produced by refit looks like I want -
/info?Name=John%20Doe&Date=2021-10-23&CarSize=medium, but I didn't find this behavior documented anywhere - so is this behavior intentional or just a side effect that can go away at any time?yohny commentedon Dec 7, 2021
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:
and call the api
the requested URL is
/info?name=John%20Doe&date=2021-12-7&Key=medium- thecarSizeargument is namedKeyinstead of using its parameter namekamranayub commentedon Jan 14, 2022
I am running into this except specifically I want to apply my System.Text.Json
snake_casenaming policy on the query object property parameter names automatically instead of[AliasAs]. When you have tons of objects that all follow the same convention, it's much nicer to define it in one place.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
UrlParameterFormatterthat handled the names of parameters, but this only handles parameter values (as far as I can tell). I'll keep digging to see if I can find a different workaround...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
sguryev commentedon Nov 22, 2023
@kamranayub have tried create a custom implementation of
DelegatingHandlerand register it viaAddHttpMessageHandler<>? Then you can try to overrideSendAsyncmethod and do whatever you want with therequest.RequestUrikamranayub commentedon Nov 23, 2023
Thanks, it's been awhile but I have an API I'd like to convert to Refit where I may need to do that.
tcortega commentedon Jun 4, 2024
@ChrisPulman this should be closed
github-actions commentedon Jul 5, 2024
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.