Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json: fix additionalProperties, allow space after enum/const #7840

Merged
merged 21 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
12a061c
json: default additionalProperty to true
ochafik Jun 9, 2024
8785a6e
json: don't force additional props after normal properties!
ochafik Jun 9, 2024
87d506f
json: allow space after enum/const
ochafik Jun 9, 2024
43f74e0
json: update pydantic example to set additionalProperties: false
ochafik Jun 9, 2024
adca9af
json: prevent additional props to redefine a typed prop
ochafik Jun 10, 2024
6743438
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 11, 2024
8b47473
port not_strings to python, add trailing space
ochafik Jun 11, 2024
4e63756
fix not_strings & port to js+py
ochafik Jun 11, 2024
02e2634
Update json-schema-to-grammar.cpp
ochafik Jun 11, 2024
0e48ea8
fix _not_strings for substring overlaps
ochafik Jun 11, 2024
322d611
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 12, 2024
6c859ee
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 22, 2024
2f1a087
json: fix additionalProperties default, uncomment tests
ochafik Jun 22, 2024
5c2d3fa
json: add integ. test case for additionalProperties
ochafik Jun 22, 2024
f714d7f
json: nit: simplify condition
ochafik Jun 22, 2024
3c64db1
reformat grammar integ tests w/ R"""()""" strings where there's escapes
ochafik Jun 23, 2024
65680f9
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 23, 2024
35bbac1
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 24, 2024
c6df6ce
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 25, 2024
fba3a86
Merge remote-tracking branch 'origin/master' into json-additional
ochafik Jun 25, 2024
23beed2
update # tokens in server test: consts can now have trailing space
ochafik Jun 25, 2024
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
Prev Previous commit
Next Next commit
json: don't force additional props after normal properties!
  • Loading branch information
ochafik committed Jun 9, 2024
commit 8785a6eea011996a788b884a672f582dba55b737
12 changes: 4 additions & 8 deletions common/json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,11 @@ class SchemaConverter {
}
std::string k = ks[0];
std::string kv_rule_name = prop_kv_rule_names[k];
if (k == "*") {
res = _add_rule(
name + (name.empty() ? "" : "-") + "additional-kvs",
kv_rule_name + " ( \",\" space " + kv_rule_name + " )*"
);
} else if (first_is_optional) {
res = "( \",\" space " + kv_rule_name + " )?";
std::string comma_ref = "( \",\" space " + kv_rule_name + " )";
if (first_is_optional) {
res = comma_ref + (k == "*" ? "*" : "?");
} else {
res = kv_rule_name;
res = kv_rule_name + (k == "*" ? " " + comma_ref + "*" : "");
}
if (ks.size() > 1) {
res += " " + _add_rule(
Expand Down
12 changes: 4 additions & 8 deletions examples/json_schema_to_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,11 @@ def _build_object_rule(self, properties: List[Tuple[str, Any]], required: Set[st
def get_recursive_refs(ks, first_is_optional):
[k, *rest] = ks
kv_rule_name = prop_kv_rule_names[k]
if k == '*':
res = self._add_rule(
f'{name}{"-" if name else ""}additional-kvs',
f'{kv_rule_name} ( "," space ' + kv_rule_name + ' )*'
)
elif first_is_optional:
res = f'( "," space {kv_rule_name} )?'
comma_ref = f'( "," space {kv_rule_name} )'
if first_is_optional:
res = comma_ref + ('*' if k == '*' else '?')
else:
res = kv_rule_name
res = kv_rule_name + (' ' + comma_ref + "*" if k == '*' else '')
if len(rest) > 0:
res += ' ' + self._add_rule(
f'{name}{"-" if name else ""}{k}-rest',
Expand Down
12 changes: 4 additions & 8 deletions examples/server/public/json-schema-to-grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -506,15 +506,11 @@ export class SchemaConverter {
const [k, ...rest] = ks;
const kvRuleName = propKvRuleNames[k];
let res;
if (k === '*') {
res = this._addRule(
`${name ?? ''}${name ? '-' : ''}additional-kvs`,
`${kvRuleName} ( "," space ` + kvRuleName + ` )*`
)
} else if (firstIsOptional) {
res = `( "," space ${kvRuleName} )?`;
const commaRef = `( "," space ${kvRuleName} )`;
if (firstIsOptional) {
res = commaRef + (k === '*' ? '*' : '?');
} else {
res = kvRuleName;
res = kvRuleName + (k === '*' ? ' ' + commaRef + '*' : '');
}
if (rest.length > 0) {
res += ' ' + this._addRule(
Expand Down
27 changes: 10 additions & 17 deletions tests/test-json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,12 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
})""",
R"""(
additional-kv ::= string ":" space additional-value
additional-kvs ::= additional-kv ( "," space additional-kv )*
additional-value ::= "[" space (number ("," space number)*)? "]" space
char ::= [^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
root ::= "{" space (additional-kvs )? "}" space
root ::= "{" space (additional-kv ( "," space additional-kv )* )? "}" space
space ::= " "?
string ::= "\"" char* "\"" space
)"""
Expand Down Expand Up @@ -636,12 +635,11 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
R"""(
a-kv ::= "\"a\"" space ":" space number
additional-kv ::= string ":" space string
additional-kvs ::= additional-kv ( "," space additional-kv )*
char ::= [^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
root ::= "{" space a-kv ( "," space ( additional-kvs ) )? "}" space
root ::= "{" space a-kv ( "," space ( additional-kv ( "," space additional-kv )* ) )? "}" space
space ::= " "?
string ::= "\"" char* "\"" space
)"""
Expand All @@ -659,14 +657,13 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
})""",
R"""(
a-kv ::= "\"a\"" space ":" space number
a-rest ::= additional-kvs
a-rest ::= ( "," space additional-kv )*
additional-kv ::= string ":" space number
additional-kvs ::= additional-kv ( "," space additional-kv )*
char ::= [^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
root ::= "{" space (a-kv a-rest | additional-kvs )? "}" space
root ::= "{" space (a-kv a-rest | additional-kv ( "," space additional-kv )* )? "}" space
space ::= " "?
string ::= "\"" char* "\"" space
)"""
Expand All @@ -687,14 +684,13 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
R"""(
a-kv ::= "\"a\"" space ":" space number
additional-kv ::= string ":" space number
additional-kvs ::= additional-kv ( "," space additional-kv )*
b-kv ::= "\"b\"" space ":" space number
b-rest ::= additional-kvs
b-rest ::= ( "," space additional-kv )*
char ::= [^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
integral-part ::= [0] | [1-9] [0-9]{0,15}
number ::= ("-"? integral-part) ("." decimal-part)? ([eE] [-+]? integral-part)? space
root ::= "{" space a-kv ( "," space ( b-kv b-rest | additional-kvs ) )? "}" space
root ::= "{" space a-kv ( "," space ( b-kv b-rest | additional-kv ( "," space additional-kv )* ) )? "}" space
space ::= " "?
string ::= "\"" char* "\"" space
)"""
Expand Down Expand Up @@ -752,20 +748,18 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
alternative-0 ::= foo
alternative-1 ::= bar
array ::= "[" space ( value ("," space value)* )? "]" space
bar ::= "{" space (bar-b-kv bar-b-rest | bar-additional-kvs )? "}" space
bar ::= "{" space (bar-b-kv bar-b-rest | bar-additional-kv ( "," space bar-additional-kv )* )? "}" space
bar-additional-kv ::= string ":" space bar-additional-value
bar-additional-kvs ::= bar-additional-kv ( "," space bar-additional-kv )*
bar-additional-value ::= object
bar-b-kv ::= "\"b\"" space ":" space number
bar-b-rest ::= bar-additional-kvs
bar-b-rest ::= ( "," space bar-additional-kv )*
boolean ::= ("true" | "false") space
char ::= [^"\\] | "\\" (["\\/bfnrt] | "u" [0-9a-fA-F]{4})
decimal-part ::= [0-9]{1,16}
foo ::= "{" space (foo-a-kv foo-a-rest | foo-additional-kvs )? "}" space
foo ::= "{" space (foo-a-kv foo-a-rest | foo-additional-kv ( "," space foo-additional-kv )* )? "}" space
foo-a-kv ::= "\"a\"" space ":" space number
foo-a-rest ::= foo-additional-kvs
foo-a-rest ::= ( "," space foo-additional-kv )*
foo-additional-kv ::= string ":" space foo-additional-value
foo-additional-kvs ::= foo-additional-kv ( "," space foo-additional-kv )*
foo-additional-value ::= object
integral-part ::= [0] | [1-9] [0-9]{0,15}
null ::= "null" space
Expand All @@ -775,7 +769,6 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
space ::= " "?
string ::= "\"" char* "\"" space
value ::= object | array | string | number | boolean | null

)"""
});

Expand Down