Skip to content

Handle array-typed header parameters and CLI flags - #1464

Open
Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:param-array-headers-and-cli
Open

Indian-boult wants to merge 1 commit into
oxidecomputer:mainfrom
Indian-boult:param-array-headers-and-cli

Conversation

@Indian-boult

Copy link
Copy Markdown

Fixes #822, and handles the array case of #1170.

Two kinds of generated code fail to compile when a parameter's schema is an array.

For the new sample_openapi/param-arrays.json, the positional, builder and tagged builder clients and the CLI fail with 10 errors on main.

OpenAPI 3.0.3 section 4.7.12 allows only the simple style for header parameters, and its examples serialize the array ["blue","black","brown"] as blue,black,brown. The client now joins the items of an array header with commas into one field line, and RFC 9110 section 5.3 treats that as equivalent to one field line per item for a list header. #822 suggested appending each item as its own line. I went with the spec's form, because a server that reads one header value then sees every item, but switching to one line per item is a small change if you prefer it. The httpmock extension matches an array header against the same joined string. Scalar headers keep to_string().

In the CLI an array parameter is now a flag that can be repeated. Each occurrence goes through the item type's parser, so an enum item still gets PossibleValuesParser, and the consumer collects get_many into the parameter's type. This follows the sketch in #1170. Maps and untagged enums such as GetThingOrThingsId in buildomat stay as they are, since #1170 says those need more design.

Array query parameters in httpmock still call to_string(). That is #1260, which #1261 handles, so I left those arms alone, and param_arrays_httpmock is commented out in the output crate the way buildomat_httpmock is.

Tests

  • progenitor/tests/build_param_arrays.rs compiles the positional and both builder clients for the new spec, so CI now builds a client with array headers. It fails to compile on main with the six header errors.
  • The new expectorate outputs show the generated code. No existing output changes.
  • Outside the repo, I compiled the generated positional, builder, tagged builder and CLI modules against reqwest 0.13, clap 4.6 and httpmock 0.8, and ran them against an httpmock server whose expectations are written by hand. Both clients send x-labels: a,b, x-shard-ids: 1,2 and one kinds= pair per item. The CLI accepts --kinds widget --kinds gadget and rejects --kinds sprocket.

cargo fmt --check with nightly, the build and test steps on stable and 1.97, the trybuild tests and the no-default-features build pass.

A header parameter whose schema is an array made the generated client call
`to_string()` on a `Vec`, which does not compile. This is oxidecomputer#822. The CLI passed
the whole `Vec` type to `clap::value_parser!`, which does not compile either,
as oxidecomputer#1170 describes.

The client now sends an array header in the simple style that OpenAPI 3.0.3
gives header parameters, with the items joined by commas, and the httpmock
extension matches an array header against the same joined value. The CLI makes
an array parameter a flag that may be repeated, parses each occurrence with
the item type's parser, and collects the values. Scalar parameters generate
the same code as before.

sample_openapi/param-arrays.json has array query and header parameters,
including one whose items are an enum. On main the client and CLI generated
from it fail to compile with 10 errors. build_param_arrays.rs compiles the
generated clients in CI. Array query parameters in httpmock are oxidecomputer#1260, which
oxidecomputer#1261 handles, so param_arrays_httpmock stays out of the output crate like
buildomat_httpmock.

Fixes oxidecomputer#822
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.

Compilation failure for header values that are arrays

1 participant