diff options
author | Olivier Chafik <ochafik@users.noreply.github.com> | 2024-03-22 13:07:44 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 15:07:44 +0200 |
commit | 72114edf068a9b2f54d3b09e9a198f611be397e8 (patch) | |
tree | 0614f917e7ecd6a9c86dbcebd43ce4161dea8997 /common/json-schema-to-grammar.cpp | |
parent | 2f0e81e053b41ca28e73a841e7bdbf9820baaa57 (diff) |
json-schema-to-grammar : fix order of props + non-str const/enum (#6232)
* json: ordered json in server/schema converter to respect orig order
* json: ws nits
* json: support non-string const / enums
Diffstat (limited to 'common/json-schema-to-grammar.cpp')
-rw-r--r-- | common/json-schema-to-grammar.cpp | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp index dc6d30ef..0e468034 100644 --- a/common/json-schema-to-grammar.cpp +++ b/common/json-schema-to-grammar.cpp @@ -9,7 +9,7 @@ #include <unordered_set> #include <vector> -using json = nlohmann::json; +using json = nlohmann::ordered_json; const std::string SPACE_RULE = "\" \"?"; @@ -124,7 +124,7 @@ static std::string replacePattern(const std::string & input, const std::regex & } static std::string format_literal(const std::string & literal) { - std::string escaped = replacePattern(json(literal).dump(), GRAMMAR_LITERAL_ESCAPE_RE, [&](const std::smatch & match) { + std::string escaped = replacePattern(literal, GRAMMAR_LITERAL_ESCAPE_RE, [&](const std::smatch & match) { char c = match.str()[0]; return GRAMMAR_LITERAL_ESCAPES.at(c); }); @@ -137,7 +137,7 @@ private: std::function<json(const std::string &)> _fetch_json; bool _dotall; std::map<std::string, std::string> _rules; - std::unordered_map<std::string, nlohmann::json> _refs; + std::unordered_map<std::string, json> _refs; std::unordered_set<std::string> _refs_being_resolved; std::vector<std::string> _errors; std::vector<std::string> _warnings; @@ -413,7 +413,7 @@ private: std::string prop_rule_name = visit(prop_schema, name + (name.empty() ? "" : "-") + prop_name); prop_kv_rule_names[prop_name] = _add_rule( name + (name.empty() ? "" : "-") + prop_name + "-kv", - format_literal(prop_name) + " space \":\" space " + prop_rule_name + format_literal(json(prop_name).dump()) + " space \":\" space " + prop_rule_name ); if (required.find(prop_name) != required.end()) { required_props.push_back(prop_name); @@ -495,7 +495,7 @@ public: _rules["space"] = SPACE_RULE; } - void resolve_refs(nlohmann::json & schema, const std::string & url) { + void resolve_refs(json & schema, const std::string & url) { /* * Resolves all $ref fields in the given schema, fetching any remote schemas, * replacing each $ref with absolute reference URL and populates _refs with the @@ -557,11 +557,7 @@ public: } std::string _generate_constant_rule(const json & value) { - if (!value.is_string()) { - _errors.push_back("Only std::string constants are supported, got " + value.dump()); - return ""; - } - return format_literal(value.get<std::string>()); + return format_literal(value.dump()); } std::string visit(const json & schema, const std::string & name) { |