musement / types-generator

3 stars 1 forks source link

Some required patches don't get applied #5

Closed toomuchdesign closed 3 years ago

toomuchdesign commented 3 years ago

I'm using this library against https://api.musement.com/swagger_3.4.0.json and heavily relying on --patchSource option to patch the original swagger definition and manually set the required props.

I found out that a few entities don't receive the required attributes specified in the patch file:

Patch file

"CartItemMusementGiftbox": {
  "required": ["type", "product"]
},

Output

export type CartItemMusementGiftbox = CartItem & {
  type?: "musement-giftbox",
  product?: Giftbox
} & {};

It seems to happen with types declared as union of other types. I noticed that the declaration shows and extra & {} appended at the and of it. Could it be that the patch gets applied to this empty object type?

zi commented 3 years ago

The schema you have after applying the patch is this:

{
  "CartItemMusementGiftbox": {
    "type": "object",
    "allOf": [
      {
        "$ref": "#/components/schemas/CartItem"
      },
      {
        "properties": {
          "type": {
            "type": "string",
            "enum": ["musement-giftbox"]
          },
          "product": {
            "$ref": "#/components/schemas/Giftbox"
          }
        },
        "type": "object"
      }
    ],
    "required": ["type", "product"]
  }
}

I'm not sure this is a valid OpenApi format,

You could overwrite the whole CartItemMusementGiftbox model with this patch:

{
  "CartItemMusementGiftbox": {
    "allOf": [
      {
        "$ref": "#/components/schemas/CartItem"
      },
      {
        "properties": {
          "type": {
            "type": "string",
            "enum": ["musement-giftbox"]
          },
          "product": {
            "$ref": "#/components/schemas/Giftbox"
          }
        },
        "type": "object",
        "required": ["type", "product"]
      }
    ]
  }
}
toomuchdesign commented 3 years ago

Yep, that was definitely the issue! I didn't realize the patch was actually meant to be merged with the original schema. 👍

Thanks so much 🙌