#ExpandReferences
#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"
               },
               "d": {
                  "type": "object",
                  "properties": {
                     "b": {
                        "type": "integer"
                     }
                  }
               },
               "e": {
                  "type": "integer"
               },
               "b": {
                  "type": "object",
                  "properties": {
                     "b": {
                        "type": "integer"
                     }
                  }
               },
               "c": {
                  "type": "integer"
               }
            },
            "allOf": [
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "b"
                                 ]
                              },
                              {
                                 "required": [
                                    "c"
                                 ]
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "b"
                        ]
                     },
                     {
                        "required": [
                           "c"
                        ]
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "d"
                                 ]
                              },
                              {
                                 "required": [
                                    "e"
                                 ]
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "d"
                        ]
                     },
                     {
                        "required": [
                           "e"
                        ]
                     }
                  ]
               }
            ]
         },
         "Embed.T": {
            "type": "object",
            "properties": {
               "b": {
                  "type": "integer"
               }
            }
         },
         "Foo": {
            "type": "object",
            "required": [
               "include",
               "exclude",
               "count"
            ],
            "properties": {
               "include": {
                  "type": "object",
                  "properties": {
                     "shared": {
                        "type": "integer"
                     },
                     "shared2": {
                        "type": "integer"
                     },
                     "count": {
                        "type": "integer"
                     },
                     "amount": {
                        "type": "integer"
                     },
                     "exact": {
                        "type": "string"
                     },
                     "regex": {
                        "type": "string"
                     }
                  },
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ],
                        "oneOf": [
                           {
                              "not": {
                                 "anyOf": [
                                    {
                                       "required": [
                                          "exact"
                                       ]
                                    },
                                    {
                                       "required": [
                                          "regex"
                                       ]
                                    }
                                 ]
                              }
                           },
                           {
                              "required": [
                                 "exact"
                              ]
                           },
                           {
                              "required": [
                                 "regex"
                              ]
                           }
                        ]
                     },
                     {
                        "oneOf": [
                           {
                              "not": {
                                 "anyOf": [
                                    {
                                       "required": [
                                          "count"
                                       ]
                                    },
                                    {
                                       "required": [
                                          "amount"
                                       ]
                                    }
                                 ]
                              }
                           },
                           {
                              "required": [
                                 "count"
                              ]
                           },
                           {
                              "required": [
                                 "amount"
                              ]
                           }
                        ],
                        "required": [
                           "shared2"
                        ]
                     }
                  ]
               },
               "exclude": {
                  "type": "array",
                  "items": {
                     "type": "object",
                     "properties": {
                        "shared": {
                           "type": "integer"
                        },
                        "shared2": {
                           "type": "integer"
                        },
                        "count": {
                           "type": "integer"
                        },
                        "amount": {
                           "type": "integer"
                        },
                        "exact": {
                           "type": "string"
                        },
                        "regex": {
                           "type": "string"
                        }
                     },
                     "allOf": [
                        {
                           "required": [
                              "shared"
                           ],
                           "oneOf": [
                              {
                                 "not": {
                                    "anyOf": [
                                       {
                                          "required": [
                                             "exact"
                                          ]
                                       },
                                       {
                                          "required": [
                                             "regex"
                                          ]
                                       }
                                    ]
                                 }
                              },
                              {
                                 "required": [
                                    "exact"
                                 ]
                              },
                              {
                                 "required": [
                                    "regex"
                                 ]
                              }
                           ]
                        },
                        {
                           "oneOf": [
                              {
                                 "not": {
                                    "anyOf": [
                                       {
                                          "required": [
                                             "count"
                                          ]
                                       },
                                       {
                                          "required": [
                                             "amount"
                                          ]
                                       }
                                    ]
                                 }
                              },
                              {
                                 "required": [
                                    "count"
                                 ]
                              },
                              {
                                 "required": [
                                    "amount"
                                 ]
                              }
                           ],
                           "required": [
                              "shared2"
                           ]
                        }
                     ]
                  }
               },
               "count": {
                  "type": "integer"
               }
            }
         },
         "Incompatible": {
            "type": "object",
            "properties": {
               "shared": {
                  "type": "integer"
               },
               "extra1": {
                  "type": "integer"
               },
               "extra2": {
                  "type": "integer"
               }
            },
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ]
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra1"
                                 ]
                              },
                              {
                                 "required": [
                                    "shared",
                                    "extra2"
                                 ]
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra1"
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra2"
                  ]
               }
            ]
         },
         "MyInt": {
            "type": "integer"
         },
         "T": {
            "description": "This should be dedupped.",
            "type": "object",
            "properties": {
               "shared": {
                  "type": "integer"
               },
               "shared2": {
                  "type": "integer"
               },
               "count": {
                  "type": "integer"
               },
               "amount": {
                  "type": "integer"
               },
               "exact": {
                  "type": "string"
               },
               "regex": {
                  "type": "string"
               }
            },
            "allOf": [
               {
                  "required": [
                     "shared"
                  ],
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "exact"
                                 ]
                              },
                              {
                                 "required": [
                                    "regex"
                                 ]
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "exact"
                        ]
                     },
                     {
                        "required": [
                           "regex"
                        ]
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "count"
                                 ]
                              },
                              {
                                 "required": [
                                    "amount"
                                 ]
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "count"
                        ]
                     },
                     {
                        "required": [
                           "amount"
                        ]
                     }
                  ],
                  "required": [
                     "shared2"
                  ]
               }
            ]
         },
         "WithMap": {
            "type": "object",
            "properties": {
               "shared": {},
               "extra": {
                  "type": "integer"
               }
            },
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ]
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra"
                                 ]
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "allOf": [
                     {
                        "required": [
                           "shared",
                           "extra"
                        ]
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared"
                                 ]
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra"
                  ]
               }
            ]
         }
      }
   }
}
