openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

anyOf combining literals and objects generates incorrect code

Open danikenan opened this issue 1 year ago • 6 comments

The following schema

"schedule": {
    "anyOf": [
      {
        "const": "manual"
      },
      {
        "$ref": "#/components/schemas/SimpleSchedule"
      },
      {
        "$ref": "#/components/schemas/CronSchedule"
      }
    ],
    "title": "Schedule"
},

produces the following:

schedule: (SimpleSchedule | CronSchedule);

danikenan avatar Feb 12 '24 06:02 danikenan

@danikenan what is the type you'd expect?

mrlubos avatar Feb 13 '24 02:02 mrlubos

@danikenan we just merged a fix for similar issue in our fork, mind having a look if it fixes your problem too?

mrlubos avatar Feb 20 '24 12:02 mrlubos

@mrlubos Doesnt resolve it. The issue is that the tool resolves { "const": "manual" } to any instead of "manual"

kevinand11 avatar Feb 28 '24 12:02 kevinand11

@mrlubos Doesnt resolve it. The issue is that the tool resolves `{

"const": "manual"

}toanyinstead of"manual"`

@kevinand11 I'll try to fix it this weekend

mrlubos avatar Feb 28 '24 12:02 mrlubos

@kevinand11 Please open a pull request with failing test case against our fork. The following spec

"AnyOfConst": {
    "anyOf": [
        {
            "const": "foo"
        },
        {
            "$ref": "#/components/schemas/ModelWithString"
        },
        {
            "$ref": "#/components/schemas/ModelWithArray"
        }
    ]
}

generates the following model

export type AnyOfConst = ("foo" | ModelWithString | ModelWithArray);

so I think this works as expected.

mrlubos avatar Feb 29 '24 00:02 mrlubos

@danikenan this is fixed in @nicolas-chaulet/openapi-typescript-codegen

mrlubos avatar Feb 29 '24 00:02 mrlubos