Skip to content

[BUG][Rust] Enums are not accessible outside the crate #4079

@borsboom

Description

@borsboom

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

Pet store example

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:

  1. Have models/mod.rs export the whole module where the struct and enums are defined. For example, models/mod.rs would have

    pub mod pet;
    

    instead of

    mod pet;
    

    This way the enum could be accessed as [crate]::models::pet::Status.

  2. Have models/mod.rs export the enum directly. For example, models/mod.rs would have

    pub 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 (so Status would become PetStatus).

/cc @bjgill (submitter of PR that added enum support)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions