[java][okhttp-gson] Rename parameters that collide with call method locals

Merged
CP
4d ago
+58-0

Fixes #25566

With -g java --library okhttp-gson, an operation with a parameter named basePath generates a client that does not compile, because {{operationId}}Call declares a local String basePath next to the parameter. The same template also uses localBasePaths (a local) and reads the fields localCustomBaseUrl and localHostIndex. A parameter with one of those names either fails to compile or shadows the field.

The fix renames these four parameter names in JavaClientCodegen.toParamName, for okhttp-gson only, so basePath becomes paramBasePath. This follows the callback to paramCallback rename in AbstractJavaCodegen.toParamName. The wire name stays basePath.

I compared two other routes. Adding the names to the shared reserved-word list is one line, but toVarName uses that list too, so a model property named basePath would become _basePath in every Java generator. Renaming the template locals to a localVar prefix is a few template lines, but String basePath = null; appears 305 times in 51 okhttp-gson samples, so about 2,000 sample lines would change. That figure comes from a count; I did not regenerate it. If you prefer the template rename, I can switch.

Tests: new JavaClientCodegenTest#testOkHttpGsonParametersNamedLikeCallLocalsAreRenamed_issue25566 with a small fixture that has one parameter for each of the four names and a model property named basePath. It asserts the renamed declarations, that each query parameter still sends its original name, and that the model property keeps its name. It fails with the change reverted, and it fails when any one of the four names is dropped.

I generated the fixture with the fixed jar and mvn compile passes. On base, the issue's spec fails with variable basePath is already defined in method listItemsCall. JavaClientCodegenTest passes, and ./bin/generate-samples.sh bin/configs/java-okhttp-gson*.yaml leaves no sample changes.

Not covered: names that are local only under dynamicOperations (operation, paramMap) or performBeanValidation (method, e), and the localVar* family. These are common parameter names or need a per-option check, so I left them for a separate change if you want one.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    I ran ./bin/generate-samples.sh bin/configs/java*.yaml and ./bin/utils/export_docs_generators.sh; neither produced changes. I did not run ./mvnw clean package or the non-Java configs, because the change is in JavaClientCodegen.
  • If your PR is targeting a particular programming language, mention the technical committee members.

Summary by cubic

Fixes #25566. The okhttp-gson generator now renames parameters that collide with local variables in generated call methods, so clients with parameters named basePath, localBasePaths, localCustomBaseUrl, or localHostIndex compile correctly. The wire name stays unchanged, and model properties are unaffected.

  • Renames only in JavaClientCodegen.toParamName for the okhttp-gson library, e.g. basePath becomes paramBasePath.
  • Adds a regression test with a fixture covering all four names plus a model property named basePath; it fails if any rename is dropped or reverted.