Skip to content

Commit f00b0b9

Browse files
author
ochafik
committed
json: ordered json in server/schema converter to respect orig order
1 parent be07a03 commit f00b0b9

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

common/json-schema-to-grammar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <unordered_set>
1010
#include <vector>
1111

12-
using json = nlohmann::json;
12+
using json = nlohmann::ordered_json;
1313

1414
const std::string SPACE_RULE = "\" \"?";
1515

@@ -137,7 +137,7 @@ class SchemaConverter {
137137
std::function<json(const std::string &)> _fetch_json;
138138
bool _dotall;
139139
std::map<std::string, std::string> _rules;
140-
std::unordered_map<std::string, nlohmann::json> _refs;
140+
std::unordered_map<std::string, json> _refs;
141141
std::unordered_set<std::string> _refs_being_resolved;
142142
std::vector<std::string> _errors;
143143
std::vector<std::string> _warnings;
@@ -495,7 +495,7 @@ class SchemaConverter {
495495
_rules["space"] = SPACE_RULE;
496496
}
497497

498-
void resolve_refs(nlohmann::json & schema, const std::string & url) {
498+
void resolve_refs(json & schema, const std::string & url) {
499499
/*
500500
* Resolves all $ref fields in the given schema, fetching any remote schemas,
501501
* replacing each $ref with absolute reference URL and populates _refs with the

common/json-schema-to-grammar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#pragma once
22
#include "json.hpp"
33

4-
std::string json_schema_to_grammar(const nlohmann::json& schema);
4+
std::string json_schema_to_grammar(const nlohmann::ordered_json& schema);

examples/server/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <signal.h>
3131
#include <memory>
3232

33-
using json = nlohmann::json;
33+
using json = nlohmann::ordered_json;
3434

3535
bool server_verbose = false;
3636
bool server_log_json = true;

examples/server/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define DEFAULT_OAICOMPAT_MODEL "gpt-3.5-turbo-0613"
1414

15-
using json = nlohmann::json;
15+
using json = nlohmann::ordered_json;
1616

1717
// https://community.openai.com/t/openai-chat-list-of-error-codes-and-types/357791/11
1818
enum error_type {

tests/test-json-schema-to-grammar.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -378,28 +378,27 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
378378

379379
test({
380380
SUCCESS,
381-
"required props",
381+
"required props in original order",
382382
R"""({
383383
"type": "object",
384384
"properties": {
385-
"a": {
386-
"type": "string"
387-
},
388-
"b": {
389-
"type": "string"
390-
}
385+
"b": {"type": "string"},
386+
"c": {"type": "string"},
387+
"a": {"type": "string"}
391388
},
392389
"required": [
393390
"a",
394-
"b"
391+
"b",
392+
"c"
395393
],
396394
"additionalProperties": false,
397395
"definitions": {}
398396
})""",
399397
R"""(
400398
a-kv ::= "\"a\"" space ":" space string
401399
b-kv ::= "\"b\"" space ":" space string
402-
root ::= "{" space a-kv "," space b-kv "}" space
400+
c-kv ::= "\"c\"" space ":" space string
401+
root ::= "{" space b-kv "," space c-kv "," space a-kv "}" space
403402
space ::= " "?
404403
string ::= "\"" (
405404
[^"\\] |
@@ -458,13 +457,13 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
458457

459458
test({
460459
SUCCESS,
461-
"required + optional props",
460+
"required + optional props each in original order",
462461
R"""({
463462
"properties": {
464-
"a": {"type": "string"},
465463
"b": {"type": "string"},
466-
"c": {"type": "string"},
467-
"d": {"type": "string"}
464+
"a": {"type": "string"},
465+
"d": {"type": "string"},
466+
"c": {"type": "string"}
468467
},
469468
"required": ["a", "b"],
470469
"additionalProperties": false
@@ -473,9 +472,9 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
473472
a-kv ::= "\"a\"" space ":" space string
474473
b-kv ::= "\"b\"" space ":" space string
475474
c-kv ::= "\"c\"" space ":" space string
476-
c-rest ::= ( "," space d-kv )?
477475
d-kv ::= "\"d\"" space ":" space string
478-
root ::= "{" space a-kv "," space b-kv ( "," space ( c-kv c-rest | d-kv ) )? "}" space
476+
d-rest ::= ( "," space c-kv )?
477+
root ::= "{" space b-kv "," space a-kv ( "," space ( d-kv d-rest | c-kv ) )? "}" space
479478
space ::= " "?
480479
string ::= "\"" (
481480
[^"\\] |
@@ -796,7 +795,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
796795
int main() {
797796
test_all("C++", [](const TestCase & tc) {
798797
try {
799-
tc.verify(json_schema_to_grammar(nlohmann::json::parse(tc.schema)));
798+
tc.verify(json_schema_to_grammar(nlohmann::ordered_json::parse(tc.schema)));
800799
tc.verify_status(SUCCESS);
801800
} catch (const std::runtime_error & ex) {
802801
fprintf(stderr, "Error: %s\n", ex.what());

0 commit comments

Comments
 (0)