aboutsummaryrefslogtreecommitdiff
path: root/lib/csapi
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-26 20:17:34 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-05-26 20:17:34 +0900
commitc5d06cf378e820ca4290998a5580e87957daf061 (patch)
treed015ee3dc3a0beea2ecb42e47bd052f7d2bfd294 /lib/csapi
parentc03d9b0a595caf8a2d3fbc225625c0e9a083a95c (diff)
downloadlibquotient-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')
-rw-r--r--lib/csapi/gtad.yaml10
-rw-r--r--lib/csapi/{{base}}.cpp.mustache31
-rw-r--r--lib/csapi/{{base}}.h.mustache8
3 files changed, 29 insertions, 20 deletions
diff --git a/lib/csapi/gtad.yaml b/lib/csapi/gtad.yaml
index c7cbc73d..cb68ad1b 100644
--- a/lib/csapi/gtad.yaml
+++ b/lib/csapi/gtad.yaml
@@ -27,12 +27,10 @@ analyzer:
number:
- float: float
- //: double
- boolean: { type: bool, initializer: false }
+ boolean: { type: bool, omitted: false }
string:
- byte: &ByteStream
type: QIODevice*
- #initializer: '"{{defaultValue}}"'
- #string?: true
imports: <QtCore/QIODevice>
- binary: *ByteStream
- date:
@@ -114,6 +112,8 @@ analyzer:
imports: '"variant_converters.h"'
schema: # Properties of inline structure definitions
avoidCopy?: true
+ omitted: omitted<{{dataType.name}}>()
+ imports: '"converters.h"'
#operations:
@@ -121,10 +121,12 @@ mustache:
definitions:
_scopeRenderer: "{{scopeCamelCase}}Job::"
_literalQuote: '"'
+ # Default value for omitted
+ omitted: '{}'
maybeCrefType: "{{#avoidCopy?}}const {{/avoidCopy?}}{{dataType.name}}{{#avoidCopy?}}&{{/avoidCopy?}}{{#noCopy?}}&&{{/noCopy?}}"
qualifiedMaybeCrefType:
"{{#avoidCopy?}}const {{/avoidCopy?}}{{dataType.qualifiedName}}{{#avoidCopy?}}&{{/avoidCopy?}}{{#noCopy?}}&&{{/noCopy?}}"
- initializeDefaultValue: "{{#defaultValue}}{{>initializer}}{{/defaultValue}}{{^defaultValue}}{}{{/defaultValue}}"
+ initializeDefaultValue: "{{#defaultValue}}{{#initializer}}{{>initializer}}{{/initializer}}{{/defaultValue}}{{^defaultValue}}{{>omitted}}{{/defaultValue}}"
joinedParamDecl: '{{>maybeCrefType}} {{paramName}}{{^required?}} = {{>initializeDefaultValue}}{{/required?}}{{#@join}}, {{/@join}}'
joinedParamDef: '{{>maybeCrefType}} {{paramName}}{{#@join}}, {{/@join}}'
passQueryParams: '{{#queryParams}}{{paramName}}{{#@join}}, {{/@join}}{{/queryParams}}'
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}}
diff --git a/lib/csapi/{{base}}.h.mustache b/lib/csapi/{{base}}.h.mustache
index 2eb2c656..6f08e871 100644
--- a/lib/csapi/{{base}}.h.mustache
+++ b/lib/csapi/{{base}}.h.mustache
@@ -14,7 +14,9 @@ namespace QMatrixClient
struct {{name}}{{#parents?}} : {{#parents}}{{name}}{{#@join}}, {{/@join}}{{/parents}}{{/parents?}}
{
{{#vars}} {{dataType.name}} {{nameCamelCase}};
-{{/vars}} };
+{{/vars}}{{#in?}}
+ bool omitted;
+{{/in?}} };
{{#in?}}
QJsonObject toJson(const {{name}}& pod);
{{/in?}}{{#out?}}
@@ -33,7 +35,9 @@ namespace QMatrixClient
struct {{name}}{{#parents?}} : {{#parents}}{{name}}{{#@join}}, {{/@join}}{{/parents}}{{/parents?}}
{
{{#vars}} {{dataType.name}} {{nameCamelCase}};
-{{/vars}} };
+{{/vars}}{{#in?}}
+ bool omitted;
+{{/in?}} };
{{/ model}}
// Construction/destruction
{{/ models}}