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

Make the generation of unknown types for undefined spec configurable #1116

Open
jmechevarria opened this issue Oct 3, 2024 · 4 comments
Open
Labels
feature 🚀 New feature or request
Milestone

Comments

@jmechevarria
Copy link

Description

This is similar, but not quite the same, to #820.

Spec excerpt:

paths:
  /login:
    post:
      operationId: loginMethod
      responses:
        200:
          description: OK
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/LoginRequest"

Generated TS relating to the response body:

// types.gen.ts
export type LoginMethodResponse = unknown;

In this case, if I don't define anything for the response body, it still gets generated as an unknown type. The name of this type is inferred from the operationId field, eg:

operationId: loginMethod -> LoginMethodResponse

This is unexpected behavior for me, after switching from openapi-typescript-codegen.

Still, it might be by design now, which is why I'm not reporting a bug, but instead asking for the possibility to make it configurable; thus allowing the developer to choose NOT to generate a type at all (even void would be preferable here).

Thanks

@jmechevarria jmechevarria added the feature 🚀 New feature or request label Oct 3, 2024
@mrlubos
Copy link
Member

mrlubos commented Oct 3, 2024

Hey @jmechevarria, what's the impact of this change?

@jmechevarria
Copy link
Author

jmechevarria commented Oct 3, 2024

Hey @mrlubos

  1. Not having unnecessary unknown types around 🍃
  2. Consistency with long-standing working behavior 👴
  3. Keeping your non-paying customers happy 😃

@peppescg
Copy link

peppescg commented Oct 4, 2024

I have the same issue using hey-api/client-fetch, the body type is body?: RequestInit['body'] | Record<string, unknown> | Array<Record<string, unknown>> | Array<unknown> | number; but from my generated type I have body: unknown so a mismmatch.

Argument of type '{ url: string; headers?: RequestInit["headers"] | Record<string, string | number | boolean | (string | number | boolean)[] | null | undefined | unknown>; method?: "CONNECT" | "DELETE" | "GET" | "HEAD" | "OPTIONS" | "PATCH" | "POST" | "PUT" | "TRACE"; ... 22 more ...; body: v1CreateUserRequest; }' is not assignable to parameter of type 'Omit<RequestOptionsBase<ThrowOnError>, "method">'.
  Types of property 'body' are incompatible.
    Type 'unknown' is not assignable to type 'number | Record<string, unknown> | BodyInit | Record<string, unknown>[] | unknown[] | null | undefined'.ts(2345)

I would extend the types in config openapi-ts.config.ts, something like that, where the user can define is own body default type overriding unknown

type EmptyBody = {}
...
requestBody: {
      default: EmptyBody, 
    },

other options could be replace the empty body with RequestInit['body'] | Record<string, unknown> | Array<Record<string, unknown>> | Array<unknown> | number the same used on client-fetch or as last option any if you want to match the axios too,

@mrlubos
Copy link
Member

mrlubos commented Oct 20, 2024

The original issue will be fixed in the new parser. In fact it's fixed already, but more work needs to happen before it's quite ready for you. If you'd like the other issue resolved after that, please open a new ticket.

@mrlubos mrlubos added this to the Parser milestone Oct 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants