Skip to content
Open
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
10 changes: 10 additions & 0 deletions common/json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,16 @@ class common_schema_converter {
}

std::string visit(const json & schema, const std::string & name) {
// An empty union ("anyOf": [], "oneOf": [] or "type": []) has no alternatives to build a rule from; it used to
// produce an empty rule that the grammar parser rejects, failing the whole request. Schema generators do emit
// it, so drop the keyword and convert the rest of the schema.
for (const char * key : {"oneOf", "anyOf", "type"}) {
if (schema.is_object() && schema.contains(key) && schema.at(key).is_array() && schema.at(key).empty()) {
json rest = schema;
rest.erase(key);
return visit(rest, name);
}
}
json schema_type = schema.contains("type") ? schema["type"] : json();
std::string schema_format = schema.contains("format") ? schema["format"].get<std::string>() : "";
std::string rule_name = is_reserved_name(name) ? name + "-" : name.empty() ? "root" : name;
Expand Down
5 changes: 5 additions & 0 deletions examples/json_schema_to_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ def _generate_constant_rule(self, value):
return self._format_literal(json.dumps(value))

def visit(self, schema, name):
# An empty union ("anyOf": [], "oneOf": [] or "type": []) has no alternatives to build a rule from; drop the
# keyword and convert the rest of the schema (same as the C++ converter).
for key in ('oneOf', 'anyOf', 'type'):
if isinstance(schema, dict) and isinstance(schema.get(key), list) and not schema[key]:
return self.visit({k: v for k, v in schema.items() if k != key}, name)
schema_type = schema.get('type')
schema_format = schema.get('format')
rule_name = name + '-' if name in RESERVED_NAMES else name or 'root'
Expand Down
103 changes: 103 additions & 0 deletions tests/test-json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,109 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
)"""
});

test({
SUCCESS,
"empty anyOf",
R"""({
"anyOf": []
})""",
R"""(
array ::= "[" space ( value ("," space value)* )? space "]"
boolean ::= ("true" | "false")
char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
null ::= "null"
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)?
object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? space "}"
root ::= object
space ::= | " " | "\n"{1,2} [ \t]{0,20}
string ::= "\"" char* "\""
value ::= object | array | string | number | boolean | null
)"""
});

test({
SUCCESS,
"empty oneOf",
R"""({
"oneOf": []
})""",
R"""(
array ::= "[" space ( value ("," space value)* )? space "]"
boolean ::= ("true" | "false")
char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
null ::= "null"
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)?
object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? space "}"
root ::= object
space ::= | " " | "\n"{1,2} [ \t]{0,20}
string ::= "\"" char* "\""
value ::= object | array | string | number | boolean | null
)"""
});

test({
SUCCESS,
"empty type array",
R"""({
"type": [],
"properties": {
"a": {
"type": "string"
}
}
})""",
R"""(
a-kv ::= "\"a\"" space ":" space string
char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
root ::= "{" space (a-kv )? space "}"
space ::= | " " | "\n"{1,2} [ \t]{0,20}
string ::= "\"" char* "\""
)"""
});

test({
SUCCESS,
"empty unions in properties",
R"""({
"type": "object",
"properties": {
"x": {
"anyOf": []
},
"y": {
"type": [],
"enum": [
"a",
"b"
]
}
}
})""",
R"""(
array ::= "[" space ( value ("," space value)* )? space "]"
boolean ::= ("true" | "false")
char ::= [^"\\\x7F\x00-\x1F] | [\\] (["\\bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
null ::= "null"
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)?
object ::= "{" space ( string ":" space value ("," space string ":" space value)* )? space "}"
root ::= "{" space (x-kv x-rest | y-kv )? space "}"
space ::= | " " | "\n"{1,2} [ \t]{0,20}
string ::= "\"" char* "\""
value ::= object | array | string | number | boolean | null
x ::= object
x-kv ::= "\"x\"" space ":" space x
x-rest ::= ( "," space y-kv )?
y ::= ("\"a\"" | "\"b\"")
y-kv ::= "\"y\"" space ":" space y
)"""
});

test({
SUCCESS,
"anyOf $ref",
Expand Down