fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If all query operations have all required path parameters TS throws `Property 'path' does not exist on type 'never'`

gustavo-depaula opened this issue · comments

When all get (query) operations have all path parameters required in the OpenAPI Schema, Typescript throws error at apiContext:

       ERROR in ./[...]/apiContext.ts:57:17
       TS2339: Property 'path' does not exist on type 'never'.
           55 |         .filter(Boolean)
           56 |         .map((i) => resolvePathParam(i, operation.variables.pathParams))
         > 57 |     : operation.path.split('/').filter(Boolean);
              |                 ^^^^
           58 |
           59 |   if (hasQueryParams(operation)) {
           60 |     queryKey.push(operation.variables.queryParams);

Because all operations have variables with pathParams, the type QueryOperation always is QueryOperation & { variables: { pathParams: Record<string, string> }; }, thus, operation in the else clause of the ternary expression will be never.

Problematic type definition:

const hasPathParams = (
  operation: QueryOperation
): operation is QueryOperation & {
  variables: { pathParams: Record<string, string> };
} => {
  return Boolean((operation.variables as any).pathParams);
};

Smallest OpenAPI schema reproduction (this breaks):

openapi: 3.1.0
info:
  title: OpenAPI description of Octopus API
  version: 0.0.1
paths:
  /imAQuery/{objectId}:
    get:
      operationId: imAQuery
      parameters:
        - name: objectId
          required: true
          in: path
          schema:
            type: string
      responses:
        '200':
          description: 'OK'

components:
  schemas:
    simpleObject:
      type: object

This passes (no more required):

  /imAQuery/{objectId}:
    get:
      operationId: imAQuery
      parameters:
        - name: objectId
          in: path
          schema:
            type: string
      responses:
        '200':
          description: 'OK'