diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-05-26 20:17:34 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-05-26 20:17:34 +0900 |
commit | c5d06cf378e820ca4290998a5580e87957daf061 (patch) | |
tree | d015ee3dc3a0beea2ecb42e47bd052f7d2bfd294 /lib/csapi/{{base}}.cpp.mustache | |
parent | c03d9b0a595caf8a2d3fbc225625c0e9a083a95c (diff) | |
download | libquotient-c5d06cf378e820ca4290998a5580e87957daf061.tar.gz libquotient-c5d06cf378e820ca4290998a5580e87957daf061.zip |
lib/csapi/: Don't send parameters that were omitted
This is important because payloads that include omitted parameters may be malformed. Closes #208 (an example of such malformed payload).
Diffstat (limited to 'lib/csapi/{{base}}.cpp.mustache')
-rw-r--r-- | lib/csapi/{{base}}.cpp.mustache | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/lib/csapi/{{base}}.cpp.mustache b/lib/csapi/{{base}}.cpp.mustache index 72ea56c9..c6504f42 100644 --- a/lib/csapi/{{base}}.cpp.mustache +++ b/lib/csapi/{{base}}.cpp.mustache @@ -10,18 +10,20 @@ using namespace QMatrixClient; {{#models.model}}{{#in?}} QJsonObject QMatrixClient::toJson(const {{qualifiedName}}& pod) { - QJsonObject o; -{{#vars}} o.insert("{{baseName}}", toJson(pod.{{nameCamelCase}})); -{{/vars}} - return o; + QJsonObject _json; + if (pod.omitted) + return _json; +{{#vars}} + addToJson<{{^required?}}IfNotEmpty{{/required?}}>(_json, "{{baseName}}", pod.{{nameCamelCase}});{{/vars}} + return _json; } {{/in?}}{{#out?}} {{qualifiedName}} FromJson<{{qualifiedName}}>::operator()(const QJsonValue& jv) { - const auto& o = jv.toObject(); + const auto& _json = jv.toObject(); {{qualifiedName}} result; {{#vars}}result.{{nameCamelCase}} = - fromJson<{{dataType.name}}>(o.value("{{baseName}}")); + fromJson<{{dataType.name}}>(_json.value("{{baseName}}")); {{/vars}} return result; } @@ -34,20 +36,22 @@ namespace QMatrixClient {{#model}}{{#in?}} QJsonObject toJson(const {{qualifiedName}}& pod) { - QJsonObject o; -{{#vars}} o.insert("{{baseName}}", toJson(pod.{{nameCamelCase}})); -{{/vars}} - return o; + QJsonObject _json; + if (pod.omitted) + return _json; +{{#vars}} + addToJson<{{^required?}}IfNotEmpty{{/required?}}>(_json, "{{baseName}}", pod.{{nameCamelCase}});{{/vars}} + return _json; } {{/in?}}{{#out?}} template <> struct FromJson<{{qualifiedName}}> { {{qualifiedName}} operator()(const QJsonValue& jv) { - const auto& o = jv.toObject(); + const auto& _json = jv.toObject(); {{qualifiedName}} result; {{#vars}} result.{{nameCamelCase}} = - fromJson<{{dataType.qualifiedName}}>(o.value("{{baseName}}")); + fromJson<{{dataType.qualifiedName}}>(_json.value("{{baseName}}")); {{/vars}} return result; } @@ -90,8 +94,7 @@ QUrl {{camelCaseOperationId}}Job::makeRequestUrl(QUrl baseUrl{{#allParams?}}, {{ }}{{#consumesNonJson?}}{{nameCamelCase}}{{/consumesNonJson? }}{{^consumesNonJson?}}toJson({{nameCamelCase}}){{/consumesNonJson?}}));{{/inlineBody }}{{^inlineBody}} QJsonObject _data;{{#bodyParams}} -{{^required?}}{{#string?}} if (!{{paramName}}.isEmpty()) - {{/string?}}{{/required?}} _data.insert("{{baseName}}", toJson({{paramName}}));{{/bodyParams}} + addToJson<{{^required?}}IfNotEmpty{{/required?}}>(_data, "{{baseName}}", {{paramName}});{{/bodyParams}} setRequestData(_data);{{/inlineBody}} {{/bodyParams?}}{{#producesNonJson?}} setExpectedContentTypes({ {{#produces}}"{{_}}"{{#@join}}, {{/@join}}{{/produces}} }); {{/producesNonJson?}}}{{!<- mind the actual brace}} |