fix(client): emit typed enum for const/enum on parameter schemas#13
Merged
Conversation
PR #12 fixed string `const` inside schema components, but the parameter pipeline is a separate code path: a path/query parameter declared with `{ "type": "string", "const": "X" }` (or `enum: [...]`) on its inline schema still generated `impl AsRef<str>`, letting callers pass any value even though only one (or a fixed set) is valid. Extend `ParameterInfo` with `enum_values` and detect the string-enum case in `analyze_parameter`. The client generator emits a synthetic inline enum (`<OperationId><ParamName>`) with `Display`, `AsRef<str>`, and an `as_str()` helper, plus serde rename attrs. The existing path/query templating uses `format!("{}", v)` / `v.to_string()` for non-`String` rust_types, so Display makes it work without call-site changes. The same code path also fixes explicit `enum: [...]` on parameter schemas, which was previously generated as `impl AsRef<str>` too. `enum_values` uses `skip_serializing_if = "Option::is_none"` so existing parameter snapshots are unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to issue #10 (comment): PR #12 fixed string
constinside schema components, but the parameter pipeline is a separate code path. A path/query parameter declared with{ "type": "string", "const": "X" }(orenum: [...]) on its inline schema still generatedimpl AsRef<str>, letting callers pass any value even though only one (or a fixed set) is valid.ParameterInfogainsenum_values: Option<Vec<String>>(withskip_serializing_if, so existing snapshots are unchanged).analyze_parameterdetects string-enum/const on the inline parameter schema and produces a synthetic enum type name (<OperationId><ParamName>).Display,AsRef<str>, anas_str()helper, and serde rename attrs — placed before theimpl HttpClientblock.format!("{}", v)/v.to_string()for non-Stringrust_types) picks up the new type viaDisplay, so no call-site changes were needed.enum: [...]on parameter schemas, which was previously emitted asimpl AsRef<str>too.Before / after
For the issue commenter's spec:
{ "name": "the_constant", "in": "path", "schema": { "type": "string", "const": "MyConstantValue" } }Before:
After:
Test plan
cargo buildcargo test— full suite, 0 failures, no snapshot churncargo fmt --checkcargo clippy --lib -- -D warningstests/const_only_parameter_test.rscovers:const-only path parameter → single-variant enumenum: [...]path parameter → multi-variant enum (bonus fix)const-only optional query parameter →Option<Enum>DisplayandAsRef<str>impls are emitted🤖 Generated with Claude Code