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: allow space after enum/const
  • Loading branch information
ochafik committed Jun 9, 2024
commit 87d506f523709f56605faa8d998abf536df6073d
4 changes: 2 additions & 2 deletions common/json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,13 @@ class SchemaConverter {
}
return _add_rule(rule_name, _generate_union_rule(name, schema_types));
} else if (schema.contains("const")) {
return _add_rule(rule_name, _generate_constant_rule(schema["const"]));
return _add_rule(rule_name, _generate_constant_rule(schema["const"]) + " space");
} else if (schema.contains("enum")) {
std::vector<std::string> enum_values;
for (const auto & v : schema["enum"]) {
enum_values.push_back(_generate_constant_rule(v));
}
return _add_rule(rule_name, join(enum_values.begin(), enum_values.end(), " | "));
return _add_rule(rule_name, "(" + join(enum_values.begin(), enum_values.end(), " | ") + ") space");
} else if ((schema_type.is_null() || schema_type == "object")
&& (schema.contains("properties") ||
(schema.contains("additionalProperties") && schema["additionalProperties"] != true))) {
Expand Down
4 changes: 2 additions & 2 deletions examples/json_schema_to_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ def visit(self, schema, name):
return self._add_rule(rule_name, self._generate_union_rule(name, [{'type': t} for t in schema_type]))

elif 'const' in schema:
return self._add_rule(rule_name, self._generate_constant_rule(schema['const']))
return self._add_rule(rule_name, self._generate_constant_rule(schema['const']) + ' space')

elif 'enum' in schema:
rule = ' | '.join((self._generate_constant_rule(v) for v in schema['enum']))
rule = '(' + ' | '.join((self._generate_constant_rule(v) for v in schema['enum'])) + ') space'
return self._add_rule(rule_name, rule)

elif schema_type in (None, 'object') and \
Expand Down
4 changes: 2 additions & 2 deletions examples/server/public/json-schema-to-grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ export class SchemaConverter {
} else if (Array.isArray(schemaType)) {
return this._addRule(ruleName, this._generateUnionRule(name, schemaType.map(t => ({ type: t }))));
} else if ('const' in schema) {
return this._addRule(ruleName, this._generateConstantRule(schema.const));
return this._addRule(ruleName, this._generateConstantRule(schema.const) + ' space');
} else if ('enum' in schema) {
const rule = schema.enum.map(v => this._generateConstantRule(v)).join(' | ');
const rule = '(' + schema.enum.map(v => this._generateConstantRule(v)).join(' | ') + ') space';
return this._addRule(ruleName, rule);
} else if ((schemaType === undefined || schemaType === 'object') &&
('properties' in schema ||
Expand Down
6 changes: 3 additions & 3 deletions tests/test-json-schema-to-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"const": "foo"
})""",
R"""(
root ::= "\"foo\""
root ::= "\"foo\"" space
space ::= " "?
)"""
});
Expand All @@ -259,7 +259,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"const": 123
})""",
R"""(
root ::= "123"
root ::= "123" space
space ::= " "?
)"""
});
Expand All @@ -271,7 +271,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"enum": ["red", "amber", "green", null, 42, ["foo"]]
})""",
R"""(
root ::= "\"red\"" | "\"amber\"" | "\"green\"" | "null" | "42" | "[\"foo\"]"
root ::= ("\"red\"" | "\"amber\"" | "\"green\"" | "null" | "42" | "[\"foo\"]") space
space ::= " "?
)"""
});
Expand Down
Loading