-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- What's the version of OpenAPI Generator used?
- Have you search for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Bounty to sponsor the fix (example)
Description
When an enum is defined along with a struct, that enum is not exported from the crate and so external code using the model cannot access its values.
In the Pet Store example, models/pet.rs defines pub enum Status. However, models/mod.rs only exports the Pet struct, but not the Status enum:
pub use self::pet::Pet;
The pet module is also not exported, so it can't be accessed that way:
mod pet;
openapi-generator version
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar version
4.1.3-SNAPSHOT
OpenAPI declaration file content or url
Command line used for generation
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g rust -o /tmp/petstore
Steps to reproduce
Run the above command line.
Related issues/PRs
#2244 added support for enums.
Suggest a fix
There are two approaches I can think of:
-
Have
models/mod.rsexport the whole module where the struct and enums are defined. For example, models/mod.rs would havepub mod pet;instead of
mod pet;This way the enum could be accessed as
[crate]::models::pet::Status. -
Have
models/mod.rsexport the enum directly. For example, models/mod.rs would havepub use self::pet::{Pet, Status};instead of
pub use self::pet::Pet;This would allow the enum to be accessed as
[crate]::models::Status.However, there is a problem with this approach: the enum names are not necessarily unique (another struct could also have an enum named
Status), so for this to work the enum name would have to be disambiguated, for example by prepending the struct name (soStatuswould becomePetStatus).
/cc @bjgill (submitter of PR that added enum support)