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

Support for associated array query parameters #38

Merged
merged 4 commits into from
May 15, 2023

Conversation

gajddo00
Copy link
Contributor

@gajddo00 gajddo00 commented May 8, 2023

In order to support associated array query parameters (there are no official formal definitions so let's call it that), an ArrayParameter and ArrayEncoding options are introduced.

ArrayParameter

  • new type introduced to add array encoding support
var urlParameters: [String: Any]? { 
     ["filter": ArrayParameter([1, 2, 3], arrayEncoding: .individual)]
}

ArrayEncoding

  • commaSeparated = filter=1,2,3
  • individual = filter=1&filter=2&filter=3
    sometimes referred to as OR AND modes.

@gajddo00 gajddo00 added the enhancement New feature or request label May 8, 2023
@gajddo00 gajddo00 self-assigned this May 8, 2023
@gajddo00 gajddo00 linked an issue May 8, 2023 that may be closed by this pull request
@gajddo00 gajddo00 marked this pull request as ready for review May 9, 2023 06:58
@gajddo00 gajddo00 requested a review from cejanen May 9, 2023 06:59
@cejanen
Copy link
Collaborator

cejanen commented May 11, 2023

In order to support associated array query parameters (there are no official formal definitions so let's call it that), an ArrayParameter and ArrayEncoding options are introduced.

ArrayParameter

  • new type introduced to add array encoding support
var urlParameters: [String: Any]? { 
     ["filter": ArrayParameter([1, 2, 3], arrayEncoding: .individual)]
}

ArrayEncoding

  • commaSeparated = filter=1,2,3
  • individual = filter=1&filter=2&filter=3
    sometimes referred to as OR AND modes.

I tried to find a name for this approach and asked chatgpt 😆
The syntax you're referring to, where the same parameter key is repeated for each value in the array, is known as "multiple values for a single parameter" and it's specified in RFC 3986.

According to the RFC, the query component of a URI can include multiple parameters with the same name by separating each value with an ampersand & character. In other words, the second syntax you mentioned - ?key=1&key=2&key=3 - is a valid way to pass an array of values for a single parameter in an HTTP GET request.

@gajddo00
Copy link
Contributor Author

gajddo00 commented May 12, 2023

Do you have a link to that source? 🤔 Because every article I've read says that RFC 3986 contains no mention of this convention and I also can't find it there.

Source

Copy link
Collaborator

@cejanen cejanen left a comment

Choose a reason for hiding this comment

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

cool
🐫

@gajddo00 gajddo00 merged commit 67fa4b3 into dev May 15, 2023
@cejanen cejanen deleted the feat/associated-array-url-params branch June 12, 2023 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Multiple key in URL, associated array in URL
2 participants