Factor out query param serialization into classes #683
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The query parameter serialization logic in the
RequestFactory
class is complicated and difficult to test well. In addition, it does not consistently escape query parameter names and values (depends on whether you're using OAS 2 vs OAS 3).Solution
Split out the parameter serialization logic into classes where logic can be reused and more easily unit tested.
Notes
This change was made with the intention of being 100% compatible with the previous implementation. The only exception is that OAS 2 query parameters are now escaped with
CGI.escape
when previously they were not.If the change is an issue, I can tweak my changes to preserve the previous escaping behaviour.
File Structure
I added a new
query_serializers
folder with the following structure:collections
folder contains serializers that work on arrays.specifications
folder contains serializers that adhere to specifications like OAS 2 or OAS 3I'm definitely open to changing class names and/or moving files around!
The changes I made are compatible with:
Related Issues
Links to any related issues.
#621 fixed query parameter encoding for OAS 3, but that was never back-ported to OAS 2 serialization.
Checklist
Added documentation to README.mdN/AAdded example of using the enhancement into test-appN/ASteps to Test
The existing specs were left to show that I didn't break compatibility with the original implementation. I added unit tests for each added class to ensure 100% coverage.
Motivation
While I truly believe this code is more maintainable, I didn't make this change without selfish motivations. 😄
I currently have a fork of this gem where I have made changes to support a custom query parameter format. By splitting the logic out into classes, I can change my application to monkey patch specific classes to support my custom format without needing to maintain a fork.