From 72114edf068a9b2f54d3b09e9a198f611be397e8 Mon Sep 17 00:00:00 2001 From: Olivier Chafik Date: Fri, 22 Mar 2024 13:07:44 +0000 Subject: 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 --- common/json-schema-to-grammar.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'common/json-schema-to-grammar.cpp') 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 #include -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 _fetch_json; bool _dotall; std::map _rules; - std::unordered_map _refs; + std::unordered_map _refs; std::unordered_set _refs_being_resolved; std::vector _errors; std::vector _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()); + return format_literal(value.dump()); } std::string visit(const json & schema, const std::string & name) { -- cgit v1.2.3