Skip to content

Request body { x?: null; } mishandled (regression from 0.16.0) #2717

@andrew-stratyfy

Description

@andrew-stratyfy

openapi-fetch version

0.17.0

Description

The attached reduced testcase compiles in 0.16.0 but fails in 0.17.0 with the following error:

Type error: Type '{ x?: null | undefined; }' is not assignable to type '{} & { x?: undefined; }'.
  Type '{ x?: null | undefined; }' is not assignable to type '{ x?: undefined; }'.
    Types of property 'x' are incompatible.
      Type 'null | undefined' is not assignable to type 'undefined'.
        Type 'null' is not assignable to type 'undefined'.

  42 | export const demo = async (body: components['schemas']['T']) => {
  43 |   const api = createClient<paths>();
> 44 |   return api.POST('/', { body });
     |                          ^
  45 | };
  46 |

I realize openapi-fetch is in maintenance mode, but it would be great if there's time to fix this or revert the troublesome code, since I'm currently stuck pinning to 0.16.0.

(You might ask why you'd want a body like this, but it makes more sense in the original code, where the type is more like { x?: null; y: Y } | { x: X; y?: null }.)

Thanks for all your work!

Reproduction

Minimal reproducer:

import createClient from 'openapi-fetch';

// Stripped down from a real openapi-typescript export
export interface paths {
  '/': {
    post: operations['demo'];
  };
}
export interface components {
  schemas: {
    T: {
      x?: null;
    };
  }
}
export interface operations {
  demo: {
    parameters: {
      query?: never;
      header?: never;
      path?: never;
      cookie?: never;
    };
    requestBody: {
      content: {
        'application/json': components['schemas']['T'];
      };
    };
    responses: {
      200: {
        headers: {
          [name: string]: unknown;
        };
        content: {
          'application/json': unknown;
        };
      };
    };
  };
}

export const demo = async (body: components['schemas']['T']) => {
  const api = createClient<paths>();
  return api.POST('/', { body });
};

Expected result

Compiles correctly. Note that the original code does something similar to the test case, where I pull the parameter's type straight from the schema, so I was very surprised when it didn't agree with itself. 😁

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions