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

[Elixir] Use string headers #19759

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ntodd
Copy link
Contributor

@ntodd ntodd commented Oct 2, 2024

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:

  1. Changes the add_param/4 function to use "Foo-Id" rather than :"Foo-Id"
  2. Calls Kernel.to_string/1 on the header value to ensure it is a binary value

PR checklist

  • Read the contribution guidelines.
  • 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.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    (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 ./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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • 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

@mrmstn
Copy link
Contributor

mrmstn commented Oct 4, 2024

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!

@mrmstn
Copy link
Contributor

mrmstn commented Oct 6, 2024

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.
image

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

image

What do you think?

@ntodd
Copy link
Contributor Author

ntodd commented Oct 15, 2024

@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.

@ntodd
Copy link
Contributor Author

ntodd commented Nov 4, 2024

@mrmstn I updated add_param/4 as suggested. Please review and let me know if you have any remaining issues.

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.

2 participants