openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

fix(rust): correctly handle anyOf with inline primitives

Open awwyushh opened this issue 3 months ago • 1 comments

Pull Request: Fix Rust generator issue with anyOf field naming conflicts

Description

This PR fixes a bug in the Rust client generator where an anyOf field in a response body that has the same name as a request parameter generates an empty struct instead of the expected type or enum. This issue occurs whether the anyOf contains a single variant or multiple variants.

The fix ensures that:

  • For anyOf fields with a single primitive type variant, the generated Rust struct uses the expected primitive type (e.g., Option<String>), instead of generating an empty struct wrapper.
  • For anyOf fields with multiple variants, the generator produces the expected Rust enum variant types.

This resolves the issue reported in [#22042](https://github.com/OpenAPITools/openapi-generator/issues/22042) and was validated by running the generator against the minimal repro spec from the issue. The output matches the expected Rust struct and enum definitions.


How to verify

  1. Run mvn clean install -DskipTests -pl modules/openapi-generator-cli -am to build the CLI module after applying the patch.
  2. Generate Rust client code using the minimal repro OpenAPI spec provided in the issue.
  3. Confirm that the generated Rust code for the anyOf field no longer produces empty structs but the appropriate primitive type or enum.
  4. Verify that existing tests pass and no regressions occur.

Related issue

Fixes #22042


PR checklist

  • [x] Read the [contribution guidelines](https://github.com/openapitools/openapi-generator/blob/master/CONTRIBUTING.md).

  • [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work.

  • [x] Ran 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
    
  • [x] Committed all changed files, including regenerated samples.

  • [x] Targeted the master branch.

  • [x] Linked the PR to the issue using “Fixes #22042 ”.

  • [x] Mentioned relevant technical committee members for Rust generator review: @frol (2017/07) @farcaller (2017/08) @richardwhiuk (2019/07) @paladinzh (2020/05) @jacob-pro (2022/10) @dsteeley (2025/07)

awwyushh avatar Oct 03 '25 19:10 awwyushh

thanks for the PR

can you please merge https://github.com/OpenAPITools/openapi-generator/pull/21911(which contains some tests for anyOf) into your branch?

wing328 avatar Oct 20 '25 02:10 wing328