#NameFunc: toUpper
#DescriptionFunc: randomish
#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": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "properties": {
               "a": {
                  "description": "Randomly picked description from a set of size one.",
                  "type": "integer"
               }
            },
            "allOf": [
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "b"
                                 ],
                                 "properties": {
                                    "b": {
                                       "$ref": "#/components/schemas/EMBED_T"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "c"
                                 ],
                                 "properties": {
                                    "c": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "b"
                        ],
                        "properties": {
                           "b": {
                              "$ref": "#/components/schemas/EMBED_T"
                           }
                        }
                     },
                     {
                        "required": [
                           "c"
                        ],
                        "properties": {
                           "c": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "d"
                                 ],
                                 "properties": {
                                    "d": {
                                       "$ref": "#/components/schemas/EMBED_T"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "e"
                                 ],
                                 "properties": {
                                    "e": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "d"
                        ],
                        "properties": {
                           "d": {
                              "$ref": "#/components/schemas/EMBED_T"
                           }
                        }
                     },
                     {
                        "required": [
                           "e"
                        ],
                        "properties": {
                           "e": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     }
                  ]
               }
            ]
         },
         "EMBED_T": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "properties": {
               "b": {
                  "description": "Randomly picked description from a set of size one.",
                  "type": "integer"
               }
            }
         },
         "FOO": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "required": [
               "include",
               "exclude",
               "count"
            ],
            "properties": {
               "include": {
                  "$ref": "#/components/schemas/T"
               },
               "exclude": {
                  "description": "Randomly picked description from a set of size one.",
                  "type": "array",
                  "items": {
                     "$ref": "#/components/schemas/T"
                  }
               },
               "count": {
                  "$ref": "#/components/schemas/MYINT"
               }
            }
         },
         "INCOMPATIBLE": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ],
                        "properties": {
                           "shared": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra1"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    },
                                    "extra1": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "shared",
                                    "extra2"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    },
                                    "extra2": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra1"
                  ],
                  "properties": {
                     "shared": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "integer"
                     },
                     "extra1": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "integer"
                     }
                  }
               },
               {
                  "required": [
                     "shared",
                     "extra2"
                  ],
                  "properties": {
                     "shared": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "integer"
                     },
                     "extra2": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "integer"
                     }
                  }
               }
            ]
         },
         "MYINT": {
            "description": "Randomly picked description from a set of size one.",
            "type": "integer"
         },
         "T": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "properties": {
               "shared": {
                  "description": "Randomly picked description from a set of size one.",
                  "type": "integer"
               },
               "shared2": {
                  "description": "Randomly picked description from a set of size one.",
                  "type": "integer"
               }
            },
            "allOf": [
               {
                  "required": [
                     "shared"
                  ],
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "exact"
                                 ],
                                 "properties": {
                                    "exact": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "string"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "regex"
                                 ],
                                 "properties": {
                                    "regex": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "string"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "exact"
                        ],
                        "properties": {
                           "exact": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "string"
                           }
                        }
                     },
                     {
                        "required": [
                           "regex"
                        ],
                        "properties": {
                           "regex": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "string"
                           }
                        }
                     }
                  ]
               },
               {
                  "oneOf": [
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "count"
                                 ],
                                 "properties": {
                                    "count": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              },
                              {
                                 "required": [
                                    "amount"
                                 ],
                                 "properties": {
                                    "amount": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     },
                     {
                        "required": [
                           "count"
                        ],
                        "properties": {
                           "count": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "required": [
                           "amount"
                        ],
                        "properties": {
                           "amount": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     }
                  ],
                  "required": [
                     "shared2"
                  ]
               }
            ]
         },
         "WITHMAP": {
            "description": "Randomly picked description from a set of size one.",
            "type": "object",
            "oneOf": [
               {
                  "allOf": [
                     {
                        "required": [
                           "shared"
                        ],
                        "properties": {
                           "shared": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "object",
                              "additionalProperties": {
                                 "description": "Randomly picked description from a set of size one.",
                                 "type": "integer"
                              }
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared",
                                    "extra"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "object",
                                       "additionalProperties": {
                                          "description": "Randomly picked description from a set of size one.",
                                          "type": "integer"
                                       }
                                    },
                                    "extra": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "integer"
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "allOf": [
                     {
                        "required": [
                           "shared",
                           "extra"
                        ],
                        "properties": {
                           "shared": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "object",
                              "additionalProperties": {
                                 "description": "Randomly picked description from a set of size one.",
                                 "type": "integer"
                              }
                           },
                           "extra": {
                              "description": "Randomly picked description from a set of size one.",
                              "type": "integer"
                           }
                        }
                     },
                     {
                        "not": {
                           "anyOf": [
                              {
                                 "required": [
                                    "shared"
                                 ],
                                 "properties": {
                                    "shared": {
                                       "description": "Randomly picked description from a set of size one.",
                                       "type": "object",
                                       "additionalProperties": {
                                          "description": "Randomly picked description from a set of size one.",
                                          "type": "integer"
                                       }
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  ]
               },
               {
                  "required": [
                     "shared",
                     "extra"
                  ],
                  "properties": {
                     "shared": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "string"
                     },
                     "extra": {
                        "description": "Randomly picked description from a set of size one.",
                        "type": "integer"
                     }
                  }
               }
            ]
         }
      }
   }
}
