Skip to content

Overly Broad Type Generated When Using "const" #10

Description

@atom-mfeldkamp

Got a little nit in the type generation.

When I have a type in the OpenAPI schema that uses "const" like this:

"ConstModifier": {
    "type": "object",
    "properties": {
        "someConstant": {
            "type": "string",
            "const": "TheOnlyValidValue"
        },
    },
}

I get the following rust code:

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ConstModifier {
    #[serde(rename = "someConstant", skip_serializing_if = "Option::is_none")]
    pub some_constant: Option<String>,
}

The good news is that this works, in that I can call my API this way, but I'd expect it to be more tightly constrained. Rust doesn't have literal values the way Python or TS does and I'm not sure how other Rust libraries handle this... I imagine the better thing would be to emit an enum with only one option, so I'd end up constructing it something like:

let myModifier = ConstModifier {
   some_constant: Some(ConstModifierSomeConstantType::TheOnlyValidValue),
};

It certainly makes sense in our case (where the constant is a placeholder for expanding into an enum).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions