#skip-v3
#skip-v3-noshare
-- in.cue --
// OpenAPI title.

$version: "v1alpha1"

#T: {
	shared: int
}
#T: {} | {
	exact: string
} | {
	regex: string
}
#T: {} | {
	count: int
} | {
	amount: int
}
#T: {
	shared2: int
}

// This should be dedupped.
#T: {} | {
	count: int
} | {
	amount: int
}

#MyInt: int

#Foo: {
	include: #T
	exclude: [...#T]
	count: #MyInt
}

#Incompatible: {
	shared: int
} | {
	shared: int
	extra1: int
} | {
	shared: int
	extra2: int
}

#WithMap: {
	shared: [string]: int
} | {
	shared: [string]: int
	extra: int
} | {
	shared: string // incompatible
	extra:  int
}

#Embed: {
	a?: int

	close({}) |
	close({b: #T}) |
	close({c: int})

	#T: {b?: int}

	close({}) |
	close({d: #T}) |
	close({e: int})

	// TODO: maybe support builtin to write this as
	// oneof({},
	// {b: int},
	// {c: int})
}

-- out/TestGenerateOpenAPI/out.json --
{
   "openapi": "3.0.0",
   "info": {
      "title": "OpenAPI title.",
      "version": "v1alpha1"
   },
   "paths": {},
   "components": {
      "schemas": {
         "Embed": {
            "type": "object",
            "properties": {
               "a": {
                  "type": "integer"
               }
            },
            "allOf": [
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "b"
                                 ],
                                 "properties": {
                                    "b": {
                                       "$ref": "#/components/schemas/Embed.T"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "c"
                                 ],
                                 "properties": {
                                    "c": {
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "b"
                        ],
                        "properties": {
                           "b": {
                              "$ref": "#/components/schemas/Embed.T"
                           }
                        }
                     },
                     {
                        "required": [
                           "c"
                        ],
                        "properties": {
                           "c": {
                              "type": "integer"
                           }
                        }
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "d"
                                 ],
                                 "properties": {
                                    "d": {
                                       "$ref": "#/components/schemas/Embed.T"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "e"
                                 ],
                                 "properties": {
                                    "e": {
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "d"
                        ],
                        "properties": {
                           "d": {
                              "$ref": "#/components/schemas/Embed.T"
                           }
                        }
                     },
                     {
                        "required": [
                           "e"
                        ],
                        "properties": {
                           "e": {
                              "type": "integer"
                           }
                        }
                     }
                  ]
               }
            ]
         },
         "Embed.T": {
            "type": "object",
            "properties": {
               "b": {
                  "type": "integer"
               }
            }
         },
         "Foo": {
            "type": "object",
            "required": [
               "include",
               "exclude",
               "count"
            ],
            "properties": {
               "include": {
                  "$ref": "#/components/schemas/T"
               },
               "exclude": {
                  "type": "array",
                  "items": {
                     "$ref": "#/components/schemas/T"
                  }
               },
               "count": {
                  "$ref": "#/components/schemas/MyInt"
               }
            }
         },
         "Incompatible": {
            "type": "object",
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ],
                        "properties": {
                           "shared": {
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra1"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "type": "integer"
                                    },
                                    "extra1": {
                                       "type": "integer"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "shared",
                                    "extra2"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "type": "integer"
                                    },
                                    "extra2": {
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra1"
                  ],
                  "properties": {
                     "shared": {
                        "type": "integer"
                     },
                     "extra1": {
                        "type": "integer"
                     }
                  }
               },
               {
                  "required": [
                     "shared",
                     "extra2"
                  ],
                  "properties": {
                     "shared": {
                        "type": "integer"
                     },
                     "extra2": {
                        "type": "integer"
                     }
                  }
               }
            ]
         },
         "MyInt": {
            "type": "integer"
         },
         "T": {
            "description": "This should be dedupped.",
            "type": "object",
            "properties": {
               "shared": {
                  "type": "integer"
               },
               "shared2": {
                  "type": "integer"
               }
            },
            "allOf": [
               {
                  "required": [
                     "shared"
                  ],
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "exact"
                                 ],
                                 "properties": {
                                    "exact": {
                                       "type": "string"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "regex"
                                 ],
                                 "properties": {
                                    "regex": {
                                       "type": "string"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "exact"
                        ],
                        "properties": {
                           "exact": {
                              "type": "string"
                           }
                        }
                     },
                     {
                        "required": [
                           "regex"
                        ],
                        "properties": {
                           "regex": {
                              "type": "string"
                           }
                        }
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "count"
                                 ],
                                 "properties": {
                                    "count": {
                                       "type": "integer"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "amount"
                                 ],
                                 "properties": {
                                    "amount": {
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "count"
                        ],
                        "properties": {
                           "count": {
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "required": [
                           "amount"
                        ],
                        "properties": {
                           "amount": {
                              "type": "integer"
                           }
                        }
                     }
                  ],
                  "required": [
                     "shared2"
                  ]
               }
            ]
         },
         "WithMap": {
            "type": "object",
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ],
                        "properties": {
                           "shared": {
                              "type": "object",
                              "additionalProperties": {
                                 "type": "integer"
                              }
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "type": "object",
                                       "additionalProperties": {
                                          "type": "integer"
                                       }
                                    },
                                    "extra": {
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "allOf": [
                     {
                        "required": [
                           "shared",
                           "extra"
                        ],
                        "properties": {
                           "shared": {
                              "type": "object",
                              "additionalProperties": {
                                 "type": "integer"
                              }
                           },
                           "extra": {
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "type": "object",
                                       "additionalProperties": {
                                          "type": "integer"
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra"
                  ],
                  "properties": {
                     "shared": {
                        "type": "string"
                     },
                     "extra": {
                        "type": "integer"
                     }
                  }
               }
            ]
         }
      }
   }
}