Skip to content

Commit b813c67

Browse files
mauriciovasquezbernalfrisso
authored andcommitted
polycubed: fix issue with lists and default elements (#118)
The logic to set default elements in a list was broken as it was trying to fill the elements as if the list were a container. This commit reworks the logic to fill default elements, in a list they are filled only if there are children in the request. Signed-off-by: Mauricio Vasquez B <mauriciovasquezbernal@gmail.com>
1 parent 9bb2ce7 commit b813c67

3 files changed

Lines changed: 10 additions & 28 deletions

File tree

src/polycubed/src/server/Resources/Body/ListResource.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ const Response ListResource::ReadWhole(const std::string &cube_name,
8585
return ParentResource::ReadValue(cube_name, keys);
8686
}
8787

88-
void ListResource::SetDefaultIfMissing(nlohmann::json &body) const {
89-
// keys default values must be ignored (RFC7950#7.8.2)
90-
std::set<std::string> key_names;
91-
for (const auto &key : keys_) {
92-
key_names.emplace(key.Name());
93-
}
94-
95-
for (const auto &child : children_) {
96-
// Set default only for configuration nodes. During initialization
97-
// all non-keys can be defaulted, otherwise only the ones that are not
98-
// marked as init-only-config.
99-
if (key_names.count(child->Name()) == 0 && child->IsConfiguration() &&
100-
!child->IsInitOnlyConfig()) {
101-
child->SetDefaultIfMissing(body[child->Name()]);
102-
if (body[child->Name()].empty()) {
103-
body.erase(child->Name());
104-
}
105-
}
106-
}
107-
}
108-
10988
void ListResource::FillKeys(nlohmann::json &body, const ListKeyValues &keys) {
11089
// TODO: is there a more efficient implementation of this?
11190
for (auto &key : keys_) {
@@ -167,9 +146,6 @@ std::vector<Response> ListResource::BodyValidateMultiple(
167146
}
168147

169148
for (auto &elem : jbody) {
170-
if (initialization) {
171-
SetDefaultIfMissing(elem);
172-
}
173149
auto body = BodyValidate(cube_name, keys, elem, initialization);
174150
errors.reserve(errors.size() + body.size());
175151
std::copy(std::begin(body), std::end(body), std::back_inserter(errors));

src/polycubed/src/server/Resources/Body/ListResource.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ class ListResource : public virtual ParentResource {
4545

4646
bool IsMandatory() const override;
4747

48-
void SetDefaultIfMissing(nlohmann::json &body) const final;
49-
5048
/*
5149
* This function takes the keys (parsed from the url in "keys") and save
5250
* them in the body of the request

src/polycubed/src/server/Resources/Body/ParentResource.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,21 @@ bool ParentResource::IsConfiguration() const {
157157

158158
void ParentResource::SetDefaultIfMissing(nlohmann::json &body) const {
159159
for (const auto &child : children_) {
160+
// keys default values must be ignored (RFC7950#7.8.2)
161+
if (child->IsKey()) {
162+
continue;
163+
}
164+
160165
auto &child_body = body[child->Name()];
161-
// Lists can't be defaulted. The only possible this is if a list
166+
// Lists can't be defaulted. The only possible case is if a list
162167
// (as json array) is provided in request body. In this situation
163168
// each element of the array can be defaulted.
164169
if (std::dynamic_pointer_cast<ListResource>(child) != nullptr) {
165-
if (!child_body.empty()) {
170+
if (child_body.is_array()) {
166171
for (auto &entry : child_body) {
172+
if (child->IsKey()) {
173+
continue;
174+
}
167175
child->SetDefaultIfMissing(entry);
168176
}
169177
}

0 commit comments

Comments
 (0)