8000 Add support for url form encoding collections by MariusVolkhart · Pull Request #584 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for url form encoding collections #584

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

Merged
merged 4 commits into from
Dec 3, 2018
Merged

Add support for url form encoding collections #584

merged 4 commits into from
Dec 3, 2018

Conversation

MariusVolkhart
Copy link
Contributor

When form encoding objects that have a property that is an IEnumerable type, use the QueryAttribute and it's CollectionFormat to determine how to encode the object.

In general, this feature could be fleshed out much more deeply. Future work could include respecting custom formats and delimiters specified on the QueryAttribute, as well as support for collections stored in dictionaries that are form encoded.

 When form encoding objects that have a property that is an IEnumerable type, use the QueryAttribute and it's CollectionFormat to determine how to encode the object.

 In general, this feature could be fleshed out much more deeply. Future work could include respecting custom formats and delimiters specified on the QueryAttribute, as well as support for collections stored in dictionaries that are form encoded.

var formattedValues = enumerable
.Cast<object>()
.Select(v => settings.FormUrlEncodedParameterFormatter.Format(v, null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still value providing the attribute format string? Could these be combined? For example, for a specific double formatting in addition to the collection format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure before how the format strings fit into the picture. The attribute format string should definitely be respected

/// <remarks>Performs field renaming and value formatting as specified in <see cref="QueryAttribute"/>s and
/// <see cref="RefitSettings.FormUrlEncodedParameterFormatter"/>. Note that this is not a true dictionary, rather, a
/// form of MultiMap. A given key may appear multiple times with the same or different values.</remarks>
class FormValueDictionary : IEnumerable<KeyValuePair<string, string>>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is having this be called a Dictionary confusing? Should I rename it? Outside of FormValueDictionaryTests there is only 1 line that would be affected by a rename.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be renamed as it's an internal class

// see if there's a query attribute
var attrib = property.GetCustomAttribute<QueryAttribute>(true);

Add(GetFieldNameForProperty(property), settings.FormUrlEncodedParameterFormatter.Format(value, attrib?.Format));
if (value is IEnumerable enumerable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please put braces on the next line

/// same key, but will not be considered. Returns <c>null</c> if no matching entry is found.
/// </summary>
public string this[string key]
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this currently used? Is there a danger in losing in this path that needs to be addressed? In other words, should this index 8000 er be removed and coded around a different way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing tests use it. I think there is a larger than necessary risk of future misuse, and the tests can be satisfied comfortably with a LINQ expression. Fixed in c63b62c

- Braces on new lines
- Rename FormValueDictionary -> FormValueMultimap
- Remove FormValueMultimap indexer. This had a larger than necessary risk of being misused and introducing bugs. LINQ extensions can more expressively verify functionality in the tests.
@clairernovotny clairernovotny merged commit 3fce69b into reactiveui:master Dec 3, 2018
@MariusVolkhart
Copy link
Contributor Author

Thanks for this library and for taking the time to review the CL!

@ahmedalejo
Copy link
ahmedalejo commented Feb 22, 2019

Hi @MariusVolkhart first of all thanks for the PR.

I was wandering if this PR fortunately addresses the following issue as well:

[QueryParameters] Are broken for classes with IEnumerable properties #587

i took a quick look into the PR code and i couldn´t assert that it would work for non-decorated query parameter class definitions. I only use refit attributes on the Refit interface, and not on my POCOs/domain objects since they are shared with other libraries not referencing Refit.

@ahmedalejo
Copy link
ahmedalejo commented Feb 22, 2019

Here is an extract of the QueryParameter class:

public class PostQueryOptions
{
     public PostQueryOptions()
     {
          KeywordIds = new int[0];
          MaxResults = 100;
     }

     [DataMember("tags", Order = 1)]
     public int[] KeywordIds { get; set; }

     [DataMember("limit", Order = 2)]
     public int MaxResults { get; set; }

     public string SearchPhrase { get; set; }
}

@lock lock bot locked and limited conversation to collaborators Jun 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0