[elixir] Fix cookie parameters, header serialization, optional body comma, and undeclared 2xx responses

Merged
NE
2mo ago
+1081-92

Follow-up to #24840, rebased onto master now that it has landed. Fixes remaining bugs in the Elixir client generator; most affect the default Tesla templates, and the last commit carries a header-serialization fix for the req library that was pushed a few minutes after #24840 was merged and so missed that PR.

  • Cookie parameters were not generated. in: cookie parameters produced add_param(, ...) (invalid Elixir) when required and an empty map value when optional. They now map to :cookie / :cookie_exploded and are rendered into a single cookie header, appending to a caller-supplied one (an empty existing header is treated as absent). Values follow the parameter's explode flag: non-exploded arrays comma-join and objects flatten to k1,v1,k2,v2; exploded ones become separate cookie pairs. ParameterFeature.Cookie is no longer excluded, now that the default library supports it.
  • Array and map header parameters were corrupted. They went through to_string/1, which treats a list as a charlist, so [3, 4, 5] was sent as the bytes "\x03\x04\x05". Header serialization is now chosen from the parameter's declared type and explode flag at generation time, so a scalar always goes through to_string/1 (a charlist stays a string) and only declared collections are serialized as such. Per style: simple: arrays comma-join regardless of explode; objects flatten to k1,v1,k2,v2, or k1=v1,k2=v2 when exploded. Applied to both the Tesla and req templates.
  • Missing comma after an optional body. With prependFormOrBodyParameters and another optional parameter, the optional_params map lacked a comma after :body => :body and did not compile.
  • Undeclared 2xx responses were reported as errors. Operations declaring only error responses (petstore deleteOrder: 400 and 404) returned {:error, env} on a successful delete. When no mapping matches, a 2xx now returns {:ok, env}; other unmapped statuses still return {:error, env}.
  • Sample test titles said jason_decode/2; the function under test is json_decode/2.

Verification

  • mvn test -Dtest='Elixir*': 7 tests pass.
  • samples/client/petstore/elixir: mix compile --warnings-as-errors, mix test (33 tests against swaggerapi/petstore, including a new request_builder_test.exs), mix dialyzer all pass.
  • samples/client/petstore/elixir-req: same, mix test 44 tests, mix dialyzer passes.
  • Both samples regenerate with no diff (Samples up-to-date clean), and docs/generators/elixir.md is regenerated.
  • Cookie and comma paths are not exercised by the petstore spec, so they were additionally checked by generating and compiling ad-hoc specs with required/optional cookie parameters and with prependFormOrBodyParameters: true.

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/elixir*.yaml, ./bin/utils/export_docs_generators.sh.
  • File the PR against the correct branch: master.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @wing328 @mrmstn