Skip to content

Overly Broad Type Generated When Using "const" #10

Closed

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).

Activity

  1. lightsofapollo commented on May 7, 2026

    @lightsofapollo
    Contributor

    hrrm I'll take a look and see what I can come up with

  2. added a commit that references this issue on May 7, 2026
    3e93ad3
  3. atom-mfeldkamp commented on May 8, 2026

    @atom-mfeldkamp
    Author

    Awesome! I like it. It looks like this solved the issue for schemas, but not for when the "const" variable is used directly as an argument for an endpoint/operation. For instance, in "paths":

    "/some/const/path/{the_constant}": {
        "get": {
            "summary": "DoSomeConstOperation",
            "operationId": "SomeConstOperation",
            "parameters": [
                {
                    "name": "the_constant",
                    "in": "path",
                    "schema": {
                        "type": "string",
                        "const": "MyConstantValue"
                    },
                    "required": true,
                },
            ],
            "responses": {
                "200": {
                    "description": "Success",
                    "content": {
                        "application/json": {}
                    }
                }
            }
        }
    }

    Generates this function signature:

    pub async fn some_const_operation(
        &self,
        the_constant: impl AsRef<str>,
    ) -> Result<(), ApiOpError<serde_json::Value>> {
       // ... Stuff
    }
  4. lightsofapollo commented on May 8, 2026

    @lightsofapollo
    Contributor

    Oh interesting- TBH I didn't realize that was an option lol ... Looking now

  5. lightsofapollo commented on May 8, 2026

    @lightsofapollo
    Contributor

    #13 fixes this

  6. atom-mfeldkamp commented on May 8, 2026

    @atom-mfeldkamp
    Author

    Nice! Thank you!

    And I did say the API we're targeting is a little pathological 😅, haha...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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