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

Improve parameter encoding #1044

Merged
merged 1 commit into from
Oct 24, 2017
Merged

Improve parameter encoding #1044

merged 1 commit into from
Oct 24, 2017

Conversation

ob-stripe
Copy link
Contributor

r? @brandur-stripe
cc @stripe/api-libraries

Fixes #1042.

This PR adds general improvements to parameter encoding:

  • dictionary keys are now properly URL-encoded

  • all dictionaries are now automatically correctly encoded (got rid of the special-casing based on parameter names in DictionaryPlugin)

  • all arrays are now automatically correctly encoded (got rid of the special-casing based on prefixing the parameter names with array:)

  • added some tests

if (!type.GetTypeInfo().IsGenericType) return false;
if (type.GetTypeInfo().GetGenericTypeDefinition() != typeof(Dictionary<,>)) return false;

// Ensure that key and value types are both 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.

There's probably a way to be able to encode any Dictionary<object, object> by calling ToString() on keys and values, but I couldn't manage with my meager .NET reflection skills, so I'm just throwing an exception if the key and/or value type is not string.

It's not a big deal as all dictionaries in the library are <string, string> right now and the explicit exception should prevent contributors from mistakenly using other types.

Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly, I think some explicitness is better here than more reflection magic :)

+1 on the ArgumentExceptions.

@brandur-stripe
Copy link
Contributor

Great work as usual OB :) That cleanup around arrays is top notch.

@brandur-stripe brandur-stripe merged commit 6ed8a46 into master Oct 24, 2017
@brandur-stripe brandur-stripe deleted the ob-better-encoding branch October 24, 2017 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metadata keys with spaces and special characters
2 participants