[Elixir] Use string headers
Currently, generated API modules set headers with the following function:
request
|> add_param(:headers, :"Foo-Id", foo_id)
This works well with the default Tesla adapter, but when changing the adapter to Finch, I got errors that Mint was trying to call String.downcase/2 on the atom key causing a FunctionClauseError.
Additionally, Tesla expects headers as a keyword list of binaries (e.g. [{"Foo-Id", "1234"}] not [{"Foo-Id", 1234}]. If a non-string header value is provided, Mint.HTTP1.Request.validate_header_value/2 will throw :invalid_header_value. Again, this was only observed when changing the default adapter, but does follow the spec expected by Tesla.put_headers/2 so it should probably be changed regardless.
This PR does two things:
- Changes the
add_param/4function to use"Foo-Id"rather than:"Foo-Id" - Calls
Kernel.to_string/1on the header value to ensure it is a binary value
PR checklist
- [x] Read the contribution guidelines.
- [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- [x] Run the following to build the project and update samples:
(For Windows users, please run the script in Git BASH) Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./mvnw clean package ./bin/generate-samples.sh ./bin/configs/*.yaml ./bin/utils/export_docs_generators.sh./bin/generate-samples.sh bin/configs/java*. IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - [x] File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - [x] If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@mrmstn
Hi @ntodd,
Oh, I wasn’t aware of that - thank you very much for the PR and for pointing it out! The changes look good to me, although I’d like to run a quick test in the wild just to make sure it won’t cause any issues with the default adapter. If everything checks out, we should be good to merge this PR by tomorrow.
Thanks again!
Hi @ntodd
Your proposed changes look good, but I noticed that this won't affect how optional parameters are handled. Those values are still stored as atoms.
From the implementation perspective, I think it would make more sense to only change the function add_param(request, :headers, key, value)
as this would ensure that the key will always be an atom, until passed on to the actual transport - and this should also work for the optional parameters as headers
What do you think?
@mrmstn I have no problem with this, but I won't have a chance to tackle it for a while. I'd be happy to review and verify, however.
@mrmstn I updated add_param/4 as suggested. Please review and let me know if you have any remaining issues.
Resolving Hex dependencies...
Resolution completed in 0.056s
Unchanged:
dialyxir 1.4.5
earmark_parser 1.4.41
erlex 0.2.7
ex_doc 0.35.1
jason 1.4.4
makeup 1.2.1
makeup_elixir 1.0.0
makeup_erlang 1.0.1
mime 2.0.6
nimble_parsec 1.4.0
tesla 1.13.2
All dependencies are up to date
[INFO]
[INFO] --- exec:1.2.1:exec (test) @ ElixirPetstoreClientTests ---
Running ExUnit with seed: 446436, max_cases: 16
...........
Finished in 0.4 seconds (0.4s async, 0.00s sync)
11 tests, 0 failures
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.035 s
[INFO] Finished at: 2024-12-07T14:57:54+08:00
[INFO] ------------------------------------------------------------------------
[INFO] 4 goals, 4 executed
local tests passed.
thanks for your PR. let's give it a try
@ntodd when you've time next week, can you please PM me via Slack?
https://join.slack.com/t/openapi-generator/shared_invite/zt-2uoef5v0g-XGwo8~2oJ3EoziDSO1CmdQ