[Rust] Apply the isAnyType param fallback to operations without path params - #24570
Open
emilbonnek wants to merge 1 commit into
Open
[Rust] Apply the isAnyType param fallback to operations without path params#24570emilbonnek wants to merge 1 commit into
emilbonnek wants to merge 1 commit into
Conversation
…params The block that defaults isAnyType path, query and header params to String was nested inside a check for the operation having path params, so it never ran for operations that have none. Those params kept a dataType that already carries the models:: prefix, and the api template prefixes it again, giving models::models::TypeName which does not compile. Fixes OpenAPITools#20141
Contributor
There was a problem hiding this comment.
1 issue found across 24 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java:781">
P2: JSON-content query parameters with an unconstrained schema now become `Option<&str>` rather than `Option<serde_json::Value>` when the operation has no path params. Preserve `serde_json::Value` for `queryIsJsonMimeType` parameters so callers can still supply and serialize arbitrary JSON values.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| // However for path, query, and headers it's unlikely to be JSON so we default to `String`. | ||
| // Note that we keep the default `serde_json::Value` for body parameters. | ||
| for (var param : operation.allParams) { | ||
| if (param.isAnyType && (param.isPathParam || param.isQueryParam || param.isHeaderParam)) { |
Contributor
There was a problem hiding this comment.
P2: JSON-content query parameters with an unconstrained schema now become Option<&str> rather than Option<serde_json::Value> when the operation has no path params. Preserve serde_json::Value for queryIsJsonMimeType parameters so callers can still supply and serialize arbitrary JSON values.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustClientCodegen.java, line 781:
<comment>JSON-content query parameters with an unconstrained schema now become `Option<&str>` rather than `Option<serde_json::Value>` when the operation has no path params. Preserve `serde_json::Value` for `queryIsJsonMimeType` parameters so callers can still supply and serialize arbitrary JSON values.</comment>
<file context>
@@ -774,19 +774,18 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
+ // However for path, query, and headers it's unlikely to be JSON so we default to `String`.
+ // Note that we keep the default `serde_json::Value` for body parameters.
+ for (var param : operation.allParams) {
+ if (param.isAnyType && (param.isPathParam || param.isQueryParam || param.isHeaderParam)) {
+ param.dataType = "String";
+ param.isPrimitiveType = true;
</file context>
Suggested change
| if (param.isAnyType && (param.isPathParam || param.isQueryParam || param.isHeaderParam)) { | |
| if (param.isAnyType && !param.queryIsJsonMimeType && (param.isPathParam || param.isQueryParam || param.isHeaderParam)) { |
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.
Fixes #20141
The rust client generates
Option<models::models::TypeName>for an enum query parameter when the operation has no path parameters. That does not compile:The same parameter on an operation that does have a path parameter generates
Option<&str>and is fine. That difference is the bug.Cause
In
RustClientCodegen.postProcessOperationsWithModels, the loop that defaultsisAnyTypepath, query and header params toString(added in #20631) sits insideif (operation.pathParams.size() > 0). The loop iteratesallParamsand already guards onisPathParam || isQueryParam || isHeaderParam, so the outer condition only suppresses it for operations without path params.When it is suppressed, the param keeps a
dataTypethat already carries themodels::prefix fromAbstractRustCodegen.getTypeDeclaration, andapi.mustacheprefixes it again.This only shows up when the parameter schema is a
$refthe parser does not treat as a bare ref, so it is not markedisString: a$refwrapped inanyOfwithnull, or a$refwith sibling keywords. Both are common in FastAPI output. A bare$refwas already fine, which is why the existingenum-query-paramssample did not catch it.The fix moves the loop out of the
pathParamscheck, which is what #20631 describes doing.Reproducing
{ "openapi": "3.1.0", "info": { "title": "Repro", "version": "1.0.0" }, "paths": { "/widgets/summary": { "get": { "operationId": "getWidgetSummary", "parameters": [ { "name": "kind", "in": "query", "required": false, "schema": { "anyOf": [ { "$ref": "#/components/schemas/WidgetKind" }, { "type": "null" } ], "title": "Kind" } } ], "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/WidgetSummary" } } } } } } } }, "components": { "schemas": { "WidgetKind": { "type": "string", "enum": [ "basic", "premium" ], "title": "WidgetKind" }, "WidgetSummary": { "type": "object", "title": "WidgetSummary", "properties": { "kind": { "$ref": "#/components/schemas/WidgetKind" }, "total": { "type": "integer" } }, "required": [ "kind", "total" ] } } } }Before:
kind: Option<models::models::WidgetKind>,cargo buildfails.After:
kind: Option<&str>,cargo buildpasses.Testing
rust-reqwest-nullable-enum-query-params, a 3.1 spec covering both shapes with and without a path parameter. It does not compile on master and does compile with this change.Rust*Testpasses, 30 tests.PR checklist
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)cc: @frol @farcaller @richardwhiuk @paladinzh @jacob-pro
Summary by cubic
Fixes a Rust codegen bug where enum query params in operations without path params compiled as
models::models::TypeName. We now always defaultisAnyTypepath/query/header params toString, so generated clients compile consistently.isAnyTypefallback for path/query/header params on all operations (moved loop outside the path params check).models::prefix; affected params now emit as strings instead ofmodels::TypeName.reqwestsample covering nullable enum query params (with and without path params); regenerated Rust configs (no changes) and allRust*Testtests pass.Written for commit 23b0ef9. Summary will update on new commits.