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

Referenced nullable object types are not generated as possibly null #2030

Open
raphael-verdier opened this issue Feb 6, 2024 · 4 comments · Fixed by hey-api/openapi-ts#33
Assignees

Comments

@raphael-verdier
Copy link

Describe the bug

Referenced nullable objects are not correctly generated as either an object or null.

Considering those two schemas that should result in the same typing:

NullableObject:
  type: object
  nullable: true
  description: An object that can be null.
  properties:
    testProperty:
      type: string

CoreObjectBug:
  type: object
  properties:
    referenceA:
      $ref: '#/NullableObject'


CoreObjectWorkaround:
  type: object
  properties:
    referenceA:
      type: object
      nullable: true
      description: An object that can be null.
      properties:
        testProperty:
          type: string

The generated type for NullableObject is the following, which is wrong:

export type NullableObject = {
    testProperty?: string;
};

The generated type for CoreObjectWorkaround is the following, which is correct:

export type CoreObjectWorkaround = {
    /**
     * An object that can be null.
     */
    referenceA?: {
        testProperty?: string;
    } | null;
};

This bug is isolated to referenced object types as I use referenced atomic nullable types that don't have this bug.

@raphael-verdier raphael-verdier changed the title Nullable objects referenced tyes are not generated as nullable Referenced nullable object types are not generated as possibly null Feb 6, 2024
@mrlubos
Copy link
Collaborator

mrlubos commented Feb 11, 2024

@raphael-verdier if you're able to submit a failing test case to our fork, I might have a look at this for you

@raphael-verdier
Copy link
Author

@mrlubos If I have some time I will try to do it but I never looked at the source code

@mrlubos
Copy link
Collaborator

mrlubos commented Feb 16, 2024

@raphael-verdier can you try generating your client with our fork? It's possible it may even work, I haven't tested this scenario myself.

For failing test case, here's an example. I'd need a schema in test/spec/v3.json (like you have in the issue description), and then the expected types in the snapshot. If the generator produces incorrect code, that snapshot should fail during tests, you can leave that as a draft pull request, and I'd take it from there

@mrlubos
Copy link
Collaborator

mrlubos commented Mar 4, 2024

@raphael-verdier this is fixed in @nicolas-chaulet/openapi-typescript-codegen, give it a try!

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 a pull request may close this issue.

3 participants