oazapfts / oazapfts

Generate TypeScript clients to tap into OpenAPI servers
504 stars 81 forks source link

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

Open balzdur opened 1 year ago

balzdur commented 1 year ago

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 commented 1 year ago

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;
};