Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal fields are converted to optional any #2019

Open
DavidRowthorn opened this issue Jan 31, 2024 · 5 comments
Open

Literal fields are converted to optional any #2019

DavidRowthorn opened this issue Jan 31, 2024 · 5 comments
Assignees

Comments

@DavidRowthorn
Copy link

DavidRowthorn commented Jan 31, 2024

Using version 0.26.0

A class like the following:

class Animal(BaseModel):
    type: Literal["dog"] = "dog"
    name: string

Should generate a typescript class like so:

export type Animal = {
  type: "dog"
  name: string
}

But it generates

export type Animal = {
  type?: any
  name: string
}
@mrlubos
Copy link
Collaborator

mrlubos commented Feb 29, 2024

@DavidRowthorn I'll need to see the spec you're using to generate the client, are you able to provide it? You can also try running your spec against our fork, this issue might be fixed there.

@elibolonur
Copy link

@mrlubos I have got the same issue on my end, tried even with your fork but Literal declarations are being generated as optional. However I am not sure if this is entirely generators fault as you see in the schema and not sure if this is somehow configurable during the schema generation.

{
  "Person":{
    "title":"Person",
    "type":"object",
    "properties":{
      "id":{
        "title":"Id",
        "type":"string"
      },
      "type":{
        "title":"Type",
        "default":"Young",
        "enum":[
          "Young"
        ],
        "type":"string"
      },
      "created":{
        "title":"Created",
        "type":"string",
        "format":"date-time"
      }
    },
    "required":[
      "id",
      "created"
      // type is not added as required here
      ...
      ...
    ]
  }
}

@mrlubos
Copy link
Collaborator

mrlubos commented Mar 11, 2024

@elibolonur do you generate spec from code or is it written manually? If code, are you able to share the snippet responsible for the piece that you shared?

EDIT: the original post is about literals not being treated as such and outputting any, but you're talking about an optional field, i.e. not the same issue

@elibolonur
Copy link

@mrlubos Unfortunately not able to share the full schema. This is the part that is treated as optional, with changed names. I could share this part with some changed names fully as well.

Also yes, it is not exactly the same. However it is probably still related since literal types are translated as optional anyway. Might not be related to the generator either as said.

@mrlubos
Copy link
Collaborator

mrlubos commented Mar 16, 2024

@DavidRowthorn @elibolonur this is fixed in the latest version of @nicolas-chaulet/openapi-typescript-codegen. If you're ending up with an optional field, you will need to mark it as required in your schema and that will resolve that problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants