[SWIFT4] Dates Are Inconsistently Encoded
Created by: james-rantmedia
Description
The generated code for GET requests that have URL Query Parameters that are dates currently encodes the date to JSON and inserts it as a query parameter.
The problem with this is that it is possible for the encoded date to contain a +
character, which is a valid character in a query parameter, but not as an encoded space, as intended.
My problem example:
I have a date with a timezone component, like 2018-11-13T13:20:14.421+01:00
. Adding this date to a URL's query results in: https://example.com?date=2018-11-13T13:20:14.421+01:00
. The +
is not encoded by URLQueryItem
because it is a valid character.
However, when the server receives the request and decodes the query parameters, the result is 2018-11-13T13:20:14.421 01:00
. The + was decoded to a space. This is unintentional, and the server responds with HTTP 400 Bad Request.
openapi-generator version
3.3.2
Steps to reproduce
- Generate code for Swift4 from an OpenAPI Spec that has a GET request with URL query parameters, one of which is a Date.
- Call the generated request method passing a date that has a positive timezone adjustment, eg
2018-11-13T13:20:14.421+01:00
Related issues/PRs
I will submit a PR to address this issue and update this issue.
Suggest a fix/enhancement
Modify the template for API classes to include a URL encoding step for dates added as query parameters. Eg:
{{#queryParams}}
{{> _param}}{{#isDateTime}}.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: "1234567890T.-")){{/isDateTime}}{{#hasMore}}, {{/hasMore}}
{{/queryParams}}