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

[Bug] Reuse type and make some properties required #417

Open
balzdur opened this issue May 2, 2023 · 1 comment
Open

[Bug] Reuse type and make some properties required #417

balzdur opened this issue May 2, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@balzdur
Copy link

balzdur commented May 2, 2023

The AST generator seems ignoring allOf usage when it comes to merging definition (and not distinct types)

My use case is well described in this article and address the need to make some optional properties required.

Using the example from the article :

  schemas:
    Address:
      type: object
      properties:
        line1:
          type: string
          example: 123 Main Street
        line2:
          type: string
          example: Suite 301
        city:
          type: string
          example: Austin
    AddressRequired:
      allOf:
        - $ref: "#/components/schemas/Address"
        - required:
          - line1
          - city

Generated :

export type Address = {
  line1?: string;
  line2?: string;
  city?: string;
};
export type AddressRequired = Address & any;

Expected (or something like that):

export type Address = {
  line1?: string;
  line2?: string;
  city?: string;
};
export type AddressRequired = Required<Pick<Address,'line1'|'city'> & Omit<Address,'line1'|'city'>
@Xiphe
Copy link
Collaborator

Xiphe commented May 3, 2023

Hi @balzdur and thanks for reporting!

This is indeed an interesting use-case. From glancing over the code it seems the current implementation expected the allOf prop to be used for merging only.

The implementation currently expects complete types and then create an intersection of those, while what you're demonstrating here could be considered more like create a completely new type.

Implementation-wise I'd try to refactor this to first merge the schemas and then create the typescript interface from that.

The output would then probably just look like this for your example

export type Address = {
  line1?: string;
  line2?: string;
  city?: string;
};
export type AddressRequired = {
  line1: string;
  line2?: string;
  city: string;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants