# Tests for closedness-related issues in JSON Schema generation.
#
# Issue 4352: closedness lost in disjunctions.
-- test.cue --
package test

#Disj: int | {
	a?: int
}

#Simple: {
	a?: int
	b?: string
}

#NestedDisj: {
	x?: string | {
		y?: int
	}
}

#TwoStructDisj: {a?: int} | {b?: string}

t01?: #Disj
t02?: #Simple
t03?: #NestedDisj
t04?: #TwoStructDisj

-- datatest/testdata.cue --
package datatest
disjInt: {
	data: {t01: 42}
}
disjStruct: {
	data: {t01: {a: 1}}
}
disjStructExtra: {
	data: {t01: {a: 1, extra: true}}
	error: true
}
simple: {
	data: {t02: {a: 1, b: "hello"}}
}
simpleExtra: {
	data: {t02: {a: 1, extra: true}}
	error: true
}
nestedDisjStructExtra: {
	data: {t03: {x: {y: 1, extra: true}}}
	error: true
}
twoStructDisjExtra: {
	data: {t04: {a: 1, extra: true}}
	error: true
}
-- out/generate-v3/schema --
{
	$schema: "https://json-schema.org/draft/2020-12/schema"
	$defs: {
		Disj: {
			anyOf: [{
				type: "integer"
			}, {
				type:                 "object"
				additionalProperties: false
				properties: {
					a: {
						type: "integer"
					}
				}
			}]
		}
		NestedDisj: {
			type:                 "object"
			additionalProperties: false
			properties: {
				x: {
					anyOf: [{
						type: "string"
					}, {
						type:                 "object"
						additionalProperties: false
						properties: {
							y: {
								type: "integer"
							}
						}
					}]
				}
			}
		}
		Simple: {
			type:                 "object"
			additionalProperties: false
			properties: {
				a: {
					type: "integer"
				}
				b: {
					type: "string"
				}
			}
		}
		TwoStructDisj: {
			anyOf: [{
				type:                 "object"
				additionalProperties: false
				properties: {
					a: {
						type: "integer"
					}
				}
			}, {
				type:                 "object"
				additionalProperties: false
				properties: {
					b: {
						type: "string"
					}
				}
			}]
		}
	}
	type: "object"
	properties: {
		t01: {
			$ref: "#/$defs/Disj"
		}
		t02: {
			$ref: "#/$defs/Simple"
		}
		t03: {
			$ref: "#/$defs/NestedDisj"
		}
		t04: {
			$ref: "#/$defs/TwoStructDisj"
		}
	}
}
-- out/generate-v3/disjInt --
-- out/generate-v3/disjStruct --
-- out/generate-v3/disjStructExtra --
disjStructExtra.data.t01: conflicting values {a:1,extra:true} and int (mismatched types struct and int):
    ./datatest/testdata.cue:9:14
    1:86
    1:736
disjStructExtra.data.t01: invalid value {a:1,extra:true} (does not satisfy matchN): 0 matched, expected >=1:
    1:76
    ./datatest/testdata.cue:9:14
    1:736
disjStructExtra.data.t01.extra: field not allowed:
    1:76
    ./datatest/testdata.cue:9:28
-- out/generate-v3/simple --
-- out/generate-v3/simpleExtra --
simpleExtra.data.t02.extra: field not allowed:
    ./datatest/testdata.cue:16:21
-- out/generate-v3/nestedDisjStructExtra --
nestedDisjStructExtra.data.t03.x: conflicting values {y:1,extra:true} and string (mismatched types struct and string):
    ./datatest/testdata.cue:20:18
    1:269
    1:798
nestedDisjStructExtra.data.t03.x: invalid value {y:1,extra:true} (does not satisfy matchN): 0 matched, expected >=1:
    1:269
    ./datatest/testdata.cue:20:18
nestedDisjStructExtra.data.t03.x.extra: field not allowed:
    1:269
    ./datatest/testdata.cue:20:32
-- out/generate-v3/twoStructDisjExtra --
twoStructDisjExtra.data.t04: invalid value {a:1,extra:true} (does not satisfy matchN): 0 matched, expected >=1:
    1:518
    ./datatest/testdata.cue:24:14
    1:834
twoStructDisjExtra.data.t04.a: field not allowed:
    1:518
    ./datatest/testdata.cue:24:18
twoStructDisjExtra.data.t04.extra: field not allowed:
    1:518
    ./datatest/testdata.cue:24:28
