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

Relax requirements for 0-sized data objects in JSON #3170

Merged
merged 3 commits into from
Mar 17, 2023
Merged
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
49 changes: 27 additions & 22 deletions src/stan/io/json/json_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,17 @@ class json_data : public stan::io::var_context {
void validate_dims(const std::string &stage, const std::string &name,
const std::string &base_type,
const std::vector<size_t> &dims_declared) const {
bool is_int_type = base_type == "int";
if (is_int_type) {
if (!contains_i(name)) {
std::stringstream msg;
msg << (contains_r(name) ? "int variable contained non-int values"
: "variable does not exist")
<< "; processing stage=" << stage << "; variable name=" << name
<< "; base type=" << base_type;
throw std::runtime_error(msg.str());
}
} else {
if (!contains_r(name)) {
std::stringstream msg;
msg << "variable does not exist"
<< "; processing stage=" << stage << "; variable name=" << name
<< "; base type=" << base_type;
throw std::runtime_error(msg.str());
}
}

std::vector<size_t> dims = dims_r(name);

// JSON '[ ]' is ambiguous - any multi-dim variable with len 0 dim
size_t num_elements = 1;
for (size_t i = 0; i < dims.size(); ++i) {
num_elements *= dims[i];
if (dims.size() == 0) {
// treat non-existent variables as size-0 objects
num_elements = 0;
} else {
for (size_t i = 0; i < dims.size(); ++i) {
num_elements *= dims[i];
}
}

size_t num_elements_expected = 1;
Expand Down Expand Up @@ -301,6 +286,26 @@ class json_data : public stan::io::var_context {
throw std::runtime_error(msg.str());
}
}

bool is_int_type = base_type == "int";
if (is_int_type) {
if (!contains_i(name)) {
std::stringstream msg;
msg << (contains_r(name) ? "int variable contained non-int values"
: "variable does not exist")
<< "; processing stage=" << stage << "; variable name=" << name
<< "; base type=" << base_type;
throw std::runtime_error(msg.str());
}
} else {
if (!contains_r(name)) {
std::stringstream msg;
msg << "variable does not exist"
<< "; processing stage=" << stage << "; variable name=" << name
<< "; base type=" << base_type;
throw std::runtime_error(msg.str());
}
}
}
};

Expand Down
14 changes: 9 additions & 5 deletions src/test/unit/io/json/json_data_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ TEST(ioJson, jsonData_empty_2D_array_0_0) {
expected_dims.push_back(0);
expected_dims.push_back(0);
test_empty_int_arr(jdata, "foo", expected_vals_i);
try {
jdata.validate_dims("test", "foo", "int", expected_dims);
} catch (const std::exception &e) {
FAIL();
}
EXPECT_NO_THROW(jdata.validate_dims("test", "foo", "int", expected_dims));
EXPECT_NO_THROW(
jdata.validate_dims("test", "foo.2", "double", expected_dims));
}

TEST(ioJson, jsonData_x_3d_y_2d_z_0d) {
Expand Down Expand Up @@ -352,6 +350,12 @@ TEST(ioJson, jsonData_parse_empty_obj) {
EXPECT_EQ(0U, var_names.size());
jdata.names_i(var_names);
EXPECT_EQ(0U, var_names.size());

EXPECT_THROW(
jdata.validate_dims("testing", "should_not_exist", "double", {3, 2}),
std::runtime_error);
EXPECT_NO_THROW(
jdata.validate_dims("testing", "zero_dims", "double", {3, 0, 2}));
}

// R: strings "NaN", "Inf", "-Inf"
Expand Down