Skip to content

Conversation

fujikky
Copy link
Contributor

@fujikky fujikky commented Sep 4, 2025

Problem

When OpenAPI specifications use oneOf in response schemas, the TypeScript codegen creates union types but fails to generate the corresponding {operationId}Response type aliases. This inconsistency makes it difficult to use these response types in API mocking libraries and other tooling that rely on consistent named types.

Solution

Extended the shouldExtractNode function to recognize ts.isUnionTypeNode(node) as a complex type that should be extracted into a named type alias. This ensures that responses defined with oneOf generate proper {operationId}Response types, maintaining consistency with other response types.

Example

OpenAPI spec with oneOf response:

paths:
  /user/{userId}:
    get:
      operationId: getUser
      parameters:
        - name: userId
          in: path
          required: true
          schema:
            type: string
      responses:
        "200":
          description: User found
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/User'
                  - $ref: '#/components/schemas/AdminUser'

Before: Only inline union type generated

export type GetUserPathParams = {
  userId: string;
};

export type GetUserError = Fetcher.ErrorWrapper<undefined>;

export type GetUserVariables = {
  pathParams: GetUserPathParams;
} & GithubContext["fetcherOptions"];

export const fetchGetUser = (
  variables: GetUserVariables,
  signal?: AbortSignal
) =>
  githubFetch<
    Schemas.User | Schemas.AdminUser,
    GetUserError,
    undefined,
    {},
    {},
    GetUserPathParams
  >({ url: "/user/{userId}", method: "get", ...variables, signal });

After: GetUserResponse type alias generated

export type GetUserPathParams = {
  userId: string;
};

export type GetUserError = Fetcher.ErrorWrapper<undefined>;

export type GetUserResponse = Schemas.User | Schemas.AdminUser;

export type GetUserVariables = {
  pathParams: GetUserPathParams;
} & GithubContext["fetcherOptions"];

export const fetchGetUser = (
  variables: GetUserVariables,
  signal?: AbortSignal
) =>
  githubFetch<
    GetUserResponse,
    GetUserError,
    undefined,
    {},
    {},
    GetUserPathParams
  >({ url: "/user/{userId}", method: "get", ...variables, signal });

This change ensures that developers can consistently reference response types in their API mocking and testing code, regardless of whether the OpenAPI specification uses simple schemas or oneOf union types.

@fujikky fujikky marked this pull request as ready for review September 4, 2025 12:25
Copy link
Owner

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@fabien0102 fabien0102 merged commit c555865 into fabien0102:main Sep 8, 2025
1 check passed
@github-actions github-actions bot mentioned this pull request Sep 8, 2025
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

Successfully merging this pull request may close these issues.

2 participants