Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions laya-java/fixtures/structured.json
Original file line number Diff line number Diff line change
Expand Up @@ -3204,7 +3204,7 @@
}
}
},
"error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0"
"error": "properties.x: unsupported schema {'anyOf': 'abc'}"
},
{
"case": "truthy-non-array-anyof-with-a-oneof-to-shadow",
Expand All @@ -3226,7 +3226,7 @@
}
}
},
"error": "properties.x: only 'Optional[...]' unions (one non-null branch) are supported, got 0"
"error": "properties.x: unsupported schema {'anyOf': {'a': 1}, 'oneOf': [{'type': 'boolean'}, {'type': 'null'}]}"
},
{
"case": "top-level-is-null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,32 +436,23 @@ private static Field field(String path, String name, Object raw, Map<String, Obj
// A FALSY `anyOf` falls through to `oneOf`, because the reference spells this
// `prop.get("anyOf") or prop.get("oneOf")`. Falsy is Python's, not "not a list":
// an empty list, an empty object, an empty string, zero and false are all falsy
// there and all fall through, while a NON-EMPTY string or object is truthy and does
// NOT -- it is iterated, yields no mapping branch, and lands on the `got 0` refusal
// below. Reproducing only the empty-list case sent a truthy `{"anyOf": "abc"}`
// through to `oneOf` and, with no `oneOf` to find, out to "unsupported schema".
// there and all fall through. What survives is handled only when it is an array: the
// reference guards with `isinstance(union, list)`, so a truthy `{"anyOf": "abc"}`, a
// truthy `{"anyOf": {...}}`, a `{"anyOf": 5}` or a `{"anyOf": true}` is neither
// iterated nor unwrapped -- it falls past this block to the terminal "unsupported
// schema" below. Iterating them used to reach the `got 0` refusal, which blamed a
// union arity the request never had, and a non-iterable leaked a TypeError.
Object union = prop.get("anyOf");
if (!truthy(union)) {
union = prop.get("oneOf");
}
if (union != null) {
if (union instanceof List) {
List<Map<String, Object>> branches = new ArrayList<>();
if (union instanceof List) {
for (Object candidate : (List<?>) union) {
if (candidate instanceof Map
&& !"null".equals(((Map<?, ?>) candidate).get("type"))) {
branches.add((Map<String, Object>) candidate);
}
for (Object candidate : (List<?>) union) {
if (candidate instanceof Map
&& !"null".equals(((Map<?, ?>) candidate).get("type"))) {
branches.add((Map<String, Object>) candidate);
}
} else if (!(union instanceof String) && !(union instanceof Map)) {
// A string iterates its characters and an object its keys, so both reach the
// refusal below with zero branches, which is what the reference raises. A
// number or a boolean is not iterable at all: the reference raises TypeError
// there, and this is the one recorded exception to the
// message-for-message rule -- see the class note.
throw new SchemaException(path + ": '"
+ (truthy(prop.get("anyOf")) ? "anyOf" : "oneOf")
+ "' must be an array of branches, got " + pythonType(union));
}
if (branches.size() != 1) {
throw new SchemaException(path + ": only 'Optional[...]' unions (one non-null"
Expand Down
8 changes: 7 additions & 1 deletion laya/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@ def _field(path: str, name: str, prop: Dict[str, Any], root: Optional[Dict[str,
branch.update((k, v) for k, v in prop.items() if k != "allOf")
return _field(path, name, branch, root, seen)
union = prop.get("anyOf") or prop.get("oneOf")
if union is not None:
# The `allOf` branch above guards its wrapper's shape before iterating it; an `anyOf`/`oneOf`
# that is not an array (a JSON number, a boolean -- `bool` subclasses `int` and so is neither
# `None` nor iterable the way a list is) used to fall straight into the comprehension below
# and leak `TypeError: 'int' object is not iterable`. A non-array union is not a recognised
# optional wrapper, so it is left to the terminal "unsupported schema" rejection, which names
# the path exactly like the non-array `enum` case does.
if isinstance(union, list):
branches = [b for b in union if isinstance(b, dict) and b.get("type") != "null"]
if len(branches) != 1:
raise SchemaError(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,25 @@ def _bad(schema):
except SchemaError as exc:
check("reject/non-array enum names the field and type", str(exc),
"properties.x: 'enum' must be an array of values, got str")
# The same guard the `allOf` branch already applies to its wrapper, `anyOf`/`oneOf` did not: a
# non-array union leaked `TypeError: 'int' object is not iterable` (and a `bool`, which subclasses
# `int`, leaked `TypeError: 'bool' object is not iterable`) instead of the path-naming SchemaError
# the module raises for every other malformed property. Both must now reject as an unsupported schema.
for nonarray in (3, 1.5, True, False):
check_raises("reject/non-array union %s %r" % ("anyOf", nonarray), SchemaError,
_bad({"type": "object", "properties": {"x": {"anyOf": nonarray}}}))
check_raises("reject/non-array union %s %r" % ("oneOf", nonarray), SchemaError,
_bad({"type": "object", "properties": {"x": {"oneOf": nonarray}}}))
try:
questions_from_json_schema({"type": "object", "properties": {"x": {"anyOf": 3}}})
except SchemaError as exc:
check("reject/non-array union names the field and shows the schema", str(exc),
"properties.x: unsupported schema {'anyOf': 3}")
# Positive control: a real one-non-null-branch union still unwraps (the guard is not over-strict).
check("accept/optional anyOf still maps",
questions_from_json_schema(
{"type": "object", "properties": {"x": {"anyOf": [{"type": "boolean"}, {"type": "null"}]}}}
)["x"]["type"], "noul")
check_raises("reject/non-object root", SchemaError, _bad({"type": "array"}))
check_raises("reject/empty properties", SchemaError, _bad({"type": "object", "properties": {}}))

Expand Down
Loading