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

[openapi-typescript] Incorrect type generated for nullable objects #1821

Closed
1 of 2 tasks
JohanAlteruna opened this issue Aug 5, 2024 · 3 comments · Fixed by #2059 · May be fixed by #1959
Closed
1 of 2 tasks

[openapi-typescript] Incorrect type generated for nullable objects #1821

JohanAlteruna opened this issue Aug 5, 2024 · 3 comments · Fixed by #2059 · May be fixed by #1959
Assignees
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!

Comments

@JohanAlteruna
Copy link

JohanAlteruna commented Aug 5, 2024

Thanks everyone for the new updates of openapi-typescript! I have found an issue which I believe might be related to the new major version (7).

Description

In version 7.3.0 and OpenAPI version 3.0.3, combining type: object and nullable: true does not produce the expected union type.

Reproduction

Run the CLI with default options on the following schema:

openapi: "3.0.3"
info: { title: "Test", version: "0" }
servers:
  - url: "example.com"
components:
  schemas:
    obj1:
      type: object
      nullable: true
paths: {}

Expected result

schemas: {
  obj1: Record<string, never> | null;
};

Actual Result

schemas: {
  obj1: Record<string, never>;
};

Checklist

The example given above works fine with type: string and nullable: true. It also works in openapi-typescript version 6.7.6.

@JohanAlteruna JohanAlteruna added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Aug 5, 2024
@JohanAlteruna
Copy link
Author

JohanAlteruna commented Aug 5, 2024

As a side note, I also think that Record<string, never> should be replaced with a more inclusive type for arbitrary objects. For example, there are use cases in which API consumers and/or implementations are allowed to send objects with arbitrary properties (and arbitrary properties are allowed by default in JSON schema AFAIK). In such cases, Record<string, never> will prevent all legal values except for the empty object ({}). I would suggest that the type Record<string, unknown> is used instead.

@JohanAlteruna JohanAlteruna changed the title Incorrect type generated for nullable objects [openapi-typescript] Incorrect type generated for nullable objects Aug 6, 2024
@drwpow
Copy link
Contributor

drwpow commented Aug 14, 2024

You’re right I don’t think we’re testing for this. This should be an easy fix and we’d welcome a PR on it from anyone 🙂

As a side note, I also think that Record<string, never> should be replaced with a more inclusive type for arbitrary objects.

We’ve had this suggestion before. But this library’s whole purpose is to help you catch where your runtime code deviates from your declared schema. As documented, if you declare an empty object in your schema, TS is going to flag this for you as a potential bug. Perhaps you’re autogenerating these schemas and there was a bug! It’s being overly strict for your benefit, to alert you of where your schema is incomplete, which is also at the boundary of where TS can’t typecheck for you anymore. Appreciate the suggestion, and we’ll always look for newer and better ways to do things. But in situations like this, we’ll always err on the side of being too strict rather than too loose.

@drwpow drwpow added PRs welcome PRs are welcome to solve this issue! good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project labels Aug 14, 2024
@gduliscouet-ubitransport
Copy link
Contributor

I found a similar issue: the nullable is not taken into account on a type: object using a ref (even if the object has properties and even when using the new syntax type: [object, "null"]).

Reproduction

openapi: "3.0.3"
info: { title: "Test", version: "0" }
servers:
  - url: "example.com"
components:
  schemas:
    obj1Ref:
      properties:
        id:
          type: string
    obj1:
      type: object
      nullable: true
      $ref: '#/components/schemas/obj1Ref'
paths: {}

Expected result

obj1: components["schemas"]["obj1Ref"] | null;

Actual Result

obj1: components["schemas"]["obj1Ref"];

Maybe I, or one of my colleagues, can try to open a PR for both cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Straightforward problem, solvable for first-time contributors without deep knowledge of the project openapi-ts Relevant to the openapi-typescript library PRs welcome PRs are welcome to solve this issue!
Projects
4 participants