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

Json2code: full $ref support #2452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 9 additions & 9 deletions scripts/seastar-json2code.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,22 @@ def add_operation(hfile, ccfile, path, oper):
def get_base_name(param):
return os.path.basename(param)

def get_referenced_type(schema, name):
# to be backward compatible, support "type" as well
type = schema.get("$ref") or schema.get("type")
if type is None:
raise Exception(f"neither '$ref' nor 'type' found in {name}")
return type

def is_model_valid(name, model):
if name in valid_vars:
return ""
properties = getitem(model[name], "properties", name)
for var in properties:
type = getitem(properties[var], "type", name + ":" + var)
type = get_referenced_type(properties[var], name + ":" + var)
if type == "array":
items = getitem(properties[var], "items", name + ":" + var)
try:
type = getitem(items, "type", name + ":" + var + ":items")
except Exception as e:
try:
type = getitem(items, "$ref", name + ":" + var + ":items")
except:
raise e
type = get_referenced_type(items, name + ":" + var + ":items")
if type not in valid_vars:
if type not in model:
raise Exception("Unknown type '" + type + "' in Model '" + name + "'")
Expand Down Expand Up @@ -563,7 +563,7 @@ def indent(s):
fprintln(hfile, create_enum_wrapper(model_name, member_name, member["enum"]))
fprintln(hfile, f" {config.jsonns}::json_element<{member_name}_wrapper> {member_name};\n")
else:
type_name = type_change(member["type"], member)
type_name = type_change(member["type"] if "type" in member else member["$ref"], member)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please remind me why we prefer member["type"] over member["$ref"] here? if we should indeed go the other way around, probably we could reuse get_referenced_type() as well? or you intended to be backward compatible ?

fprintln(hfile, f" {config.jsonns}::{type_name} {member_name};\n")
member_init += f'add(&{member_name}, "{member_name}");\n'
member_assignment += f'{member_name} = e.{member_name};\n'
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
}
],
"models": {
"var2_referred": {
"type": "object",
"properties": {
"var21": {
"type": "string",
"description": "The inner part of the second parameter"
}
}
},
"my_object": {
"id": "my_object",
"description": "Demonstrate an object",
Expand All @@ -63,8 +72,8 @@
"description": "The first parameter in the path"
},
"var2": {
"type": "string",
"description": "The second parameter in the path"
"$ref": "var2_referred",
"description": "The second parameter in the path wrapped in an object"
},
"enum_var": {
"type": "string",
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/json2code_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_path_params(self):
with urllib.request.urlopen(url) as f:
response = json.loads(f.read().decode('utf-8'))
self.assertEqual(response['var1'], f'/{var1}')
self.assertEqual(response['var2'], f'/{var2}')
self.assertEqual(response['var2']['var21'], f'/{var2}')
self.assertEqual(response['enum_var'], query_enum)

def test_bad_enum(self):
Expand All @@ -69,7 +69,7 @@ def test_bad_enum(self):
with urllib.request.urlopen(url) as f:
response = json.loads(f.read().decode('utf-8'))
self.assertEqual(response['var1'], f'/{var1}')
self.assertEqual(response['var2'], f'/{var2}')
self.assertEqual(response['var2']['var21'], f'/{var2}')
self.assertEqual(response['enum_var'], 'Unknown')

def test_missing_path_param(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/rest_api_httpd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ void set_routes(routes& r) {
api_json::hello_world.set(r, [] (const_req req) {
api_json::my_object obj;
obj.var1 = req.param.at("var1");
obj.var2 = req.param.at("var2");
api_json::var2_referred var2_wrapped;
var2_wrapped.var21 = req.param.at("var2");
obj.var2 = var2_wrapped;
api_json::ns_hello_world::query_enum v = api_json::ns_hello_world::str2query_enum(req.query_parameters.at("query_enum"));
// This demonstrate enum conversion
obj.enum_var = v;
Expand Down