fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Return type is set to undefined even though it is set in openapi docs

bertyhell opened this issue · comments

I have reproduced the issue in this code sandbox: https://codesandbox.io/s/openapi-codegen-template-issue-88-forked-v056i6?file=/issue88/issue88Components.ts:1235-1305

It's a fork of the sandbox that was used to reproduce this issue: #88 since the problem is similar, but the code in the sandbox is different.

I have a route that returns ProjectResponse[] objects.

paths:
  /answer:
    get:
      operationId: AnswerController_findUnanswered
      summary: ''
      description: Get unanswered questions
      parameters:
        - name: answeredIds
          required: true
          in: query
          schema:
            type: array
            items:
              type: string
      responses:
        default:
          description: 10 unanswered projects with their questions and answer options
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ProjectResponse'
      tags:
        - answer

But the generated code sets the return type to undefined instead of ProjectResponse[]

/**
 * Get unanswered questions
 */
export const useAnswerControllerFindUnanswered = <TData = undefined>(
  variables: AnswerControllerFindUnansweredVariables,
  options?: Omit<
    reactQuery.UseQueryOptions<
      undefined,
      AnswerControllerFindUnansweredError,
      TData
    >,
    "queryKey" | "queryFn"
  >
) => {
  const { fetcherOptions, queryOptions, queryKeyFn } =
    useIssue88Context(options);
  return reactQuery.useQuery<
    undefined,
    AnswerControllerFindUnansweredError,
    TData
  >(
    queryKeyFn({
      path: "/answer",
      operationId: "answerControllerFindUnanswered",
      variables,
    }),
    ({ signal }) =>
      fetchAnswerControllerFindUnanswered(
        { ...fetcherOptions, ...variables },
        signal
      ),
    {
      ...options,
      ...queryOptions,
    }
  );
};

The correct type is generated, but not used:

export type ProjectResponse = {
  id: string;
  title: string;
  questions: QuestionResponse[];
};

I found the issue.
My openapi docs were generated with the response default instead of the response 200 status.
It seems openapi-codegen only picks up on 200 responses and not on "default".

      responses:
        default: // Make sure this is 200 instead of default -------------------------------------------------------------------
          description: 10 unanswered projects with their questions and answer options
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ProjectResponse'