oxidecomputer / progenitor

An OpenAPI client generator
426 stars 55 forks source link

`M` and `m` are normalized to ident `m` #821

Open drahnr opened 1 month ago

drahnr commented 1 month ago

An externally provided openapi spec file defines

    foo:
      type: object
      properties:
        m:
          type: boolean
          description: small m
        M:
          type: boolean
          description: biiiig M

which generates

    #[derive(Clone, Debug, Deserialize, Serialize)]
    pub struct Foo {
        #[serde(rename = "M")]
        pub m: bool,
        pub m: bool,
    }

which won't compile. The root here is the normalization of the identifiers to be lowercase.

ahl commented 1 month ago

Oof. Yeah, there's a reason this is issue 1! https://github.com/oxidecomputer/typify/issues/1

What could would you ideally like to see generated?

drahnr commented 1 month ago

I think I'd want to see something like a configurable prefix for disambiguation, or some structural suffix mapping i.e. {orig}_1 or similar. All of them suffer from deferring the collision one level away but doesn't fix the root cause systematically

In a perfect world the upstream would amend the spec, for the particular vendor, that won't happen unfortunately 😕

drahnr commented 1 month ago

Thinking about this some more: What I really want, is a closure (FnMut of some sort) to be passable for ident normalization, which gives a context of some sort if it's going to be a field name, a function name, or struct name.

ahl commented 1 month ago

Setting aside the mechanism: what code would you like to see generated in this case?

drahnr commented 1 month ago

Setting aside the mechanism: what code would you like to see generated in this case?

    #[derive(Clone, Debug, Deserialize, Serialize)]
    pub struct Foo {
        #[serde(rename = "M")]
        pub m__disambiguation_case_1: bool,
        pub m: bool,
    }

Given that depending on the context, any suffix will risk a collision, that was what motivated the user defined closure for normalization (with a sane default).