Skip to content

Commit

Permalink
[cpprest] Add support for nested vectors (#7820)
Browse files Browse the repository at this point in the history
* Small fixes to prevent crash when empty json body is provided.

* cpprest : Add toJson and toHttpContent array support.

* petstore : Run script.

* cpprest : Fix toHttpContent function header.

* petstore : Run script.

* cpprest : Add support for primitive response without enclosing item.

* cpprest : Fix spaces.

* cpprest : Fix build if bodyParam is optional.

* cpprest : Fix vector of vector param.

* Small updates to use utf encoding
Add * operator for ^required
  • Loading branch information
etherealjoy authored and wing328 committed Mar 20, 2018
1 parent 3b7230b commit 73bd24d
Show file tree
Hide file tree
Showing 40 changed files with 155 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
{{/isContainer}}
{{/required}}
{
{{#isContainer}}
{{#isQueryParam}}
queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}});
{{/isQueryParam}}
{{#isHeaderParam}}
headerParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}});
{{/isHeaderParam}}
{{#isFormParam}}
{{^isFile}}
formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToArrayString<{{items.datatype}}>({{^required}}*{{/required}}{{paramName}});
{{/isFile}}
{{/isFormParam}}
{{/isContainer}}
{{^isContainer}}
{{#isQueryParam}}
queryParams[utility::conversions::to_string_t("{{baseName}}")] = ApiClient::parameterToString({{^required}}*{{/required}}{{paramName}});
{{/isQueryParam}}
Expand All @@ -153,7 +139,6 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
formParams[ utility::conversions::to_string_t("{{baseName}}") ] = ApiClient::parameterToString({{^required}}*{{/required}}{{paramName}});
{{/isFile}}
{{/isFormParam}}
{{/isContainer}}
}
{{/isPathParam}}
{{/isBodyParam}}
Expand Down Expand Up @@ -187,8 +172,9 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
json = web::json::value::array(jsonArray);
}
{{/isListContainer}}
{{^isListContainer}}
json = ModelBase::toJson({{paramName}});
{{^isListContainer}}{{#required}}json = ModelBase::toJson({{paramName}});
{{/required}}{{^required}}if ({{paramName}})
json = ModelBase::toJson(*{{paramName}});{{/required}}
{{/isListContainer}}
{{/isPrimitiveType}}
Expand Down Expand Up @@ -217,15 +203,11 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
{{/items.isDateTime}}{{/items.isString}}{{/items.isPrimitiveType}}
}
multipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), web::json::value::array(jsonArray), utility::conversions::to_string_t("application/json")));
}
{{/isListContainer}}
{{^isListContainer}}
{{#isString}}multipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}}));
{{/isString}}
{{^isString}}
if({{paramName}}.get())
}{{/isListContainer}}
{{^isListContainer}}{{#isString}}multipart->add(ModelBase::toHttpContent(utility::conversions::to_string_t("{{paramName}}"), {{paramName}}));
{{/isString}}{{^isString}}if({{^required}}{{paramName}} && (*{{paramName}}){{/required}}{{#required}}{{paramName}}{{/required}}.get())
{
{{paramName}}->toMultipart(multipart, utility::conversions::to_string_t("{{paramName}}"));
{{^required}}(*{{/required}}{{paramName}}{{^required}}){{/required}}->toMultipart(multipart, utility::conversions::to_string_t("{{paramName}}"));
}
{{/isString}}
{{/isListContainer}}
Expand Down Expand Up @@ -345,8 +327,9 @@ pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/r
result[item.first] = itemObj;
{{/vendorExtensions.x-codegen-response.items.isString}}{{/vendorExtensions.x-codegen-response.items.isPrimitiveType}}
}
{{/isMapContainer}}{{^isMapContainer}}{{#vendorExtensions.x-codegen-response.isPrimitiveType}}result = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(json);
{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.isString}}result = ModelBase::stringFromJson(json);
{{/isMapContainer}}{{^isMapContainer}}{{#vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.items.datatype}}result = ModelBase::{{vendorExtensions.x-codegen-response.items.datatype}}FromJson(json);
{{/vendorExtensions.x-codegen-response.items.datatype}}{{^vendorExtensions.x-codegen-response.items.datatype}}result = ModelBase::{{vendorExtensions.x-codegen-response.datatype}}FromJson(json);
{{/vendorExtensions.x-codegen-response.items.datatype}}{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{^vendorExtensions.x-codegen-response.isPrimitiveType}}{{#vendorExtensions.x-codegen-response.isString}}result = ModelBase::stringFromJson(json);
{{/vendorExtensions.x-codegen-response.isString}}{{^vendorExtensions.x-codegen-response.isString}}result->fromJson(json);{{/vendorExtensions.x-codegen-response.isString}}{{/vendorExtensions.x-codegen-response.isPrimitiveType}}{{/isMapContainer}}{{/isListContainer}}
}{{#vendorExtensions.x-codegen-response.isString}}
else if(responseHttpContentType == utility::conversions::to_string_t("text/plain"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,8 @@ public:
static utility::string_t parameterToString(int64_t value);
static utility::string_t parameterToString(float value);
static utility::string_t parameterToString(const utility::datetime &value);
template<class T>
static utility::string_t parameterToArrayString(std::vector<T> value)
{
utility::stringstream_t ss;
for( size_t i = 0; i < value.size(); i++)
{
if( i > 0) ss << utility::conversions::to_string_t(", ");
ss << ApiClient::parameterToString(value[i]);
}

return ss.str();
}
static utility::string_t parameterToString(const std::vector<T>& value);
pplx::task<web::http::http_response> callApi(
const utility::string_t& path,
Expand All @@ -71,6 +59,20 @@ protected:
std::shared_ptr<ApiConfiguration> m_Configuration;
};

template<class T>
utility::string_t ApiClient::parameterToString(const std::vector<T>& value)
{
utility::stringstream_t ss;
for( size_t i = 0; i < value.size(); i++)
{
if( i > 0) ss << utility::conversions::to_string_t(", ");
ss << ApiClient::parameterToString(value[i]);
}

return ss.str();
}

{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <cpprest/details/basic_types.h>
#include <cpprest/json.h>

#include <vector>

{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}
Expand All @@ -41,6 +43,8 @@ public:
static web::json::value toJson( int64_t value );
static web::json::value toJson( double value );
static web::json::value toJson( bool value );
template<class T>
static web::json::value toJson(const std::vector<T>& value);
static int64_t int64_tFromJson(web::json::value& val);
static int32_t int32_tFromJson(web::json::value& val);
Expand All @@ -58,6 +62,8 @@ public:
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, int64_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
template <class T>
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
static int64_t int64_tFromHttpContent(std::shared_ptr<HttpContent> val);
static int32_t int32_tFromHttpContent(std::shared_ptr<HttpContent> val);
Expand All @@ -74,6 +80,27 @@ public:
static std::shared_ptr<std::istream> fromBase64( const utility::string_t& encoded );
};

template<class T>
web::json::value ModelBase::toJson(const std::vector<T>& value) {
std::vector<web::json::value> ret;
for (auto& x : value) {
ret.push_back(toJson(x));
}

return web::json::value::array(ret);
}

template <class T>
std::shared_ptr<HttpContent> ModelBase::toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType ) {
web::json::value json_array = ModelBase::toJson(value);
std::shared_ptr<HttpContent> content( new HttpContent );
content->setName( name );
content->setContentDisposition( utility::conversions::to_string_t("form-data") );
content->setContentType( contentType );
content->setData( std::shared_ptr<std::istream>( new std::stringstream( utility::conversions::to_utf8string(json_array.serialize()) ) ) );
return content;
}

{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/.swagger-codegen/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.0-SNAPSHOT
2.4.0-SNAPSHOT
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
30 changes: 16 additions & 14 deletions samples/client/petstore/cpprest/ApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down Expand Up @@ -52,20 +52,8 @@ class ApiClient
static utility::string_t parameterToString(int64_t value);
static utility::string_t parameterToString(float value);
static utility::string_t parameterToString(const utility::datetime &value);

template<class T>
static utility::string_t parameterToArrayString(std::vector<T> value)
{
utility::stringstream_t ss;

for( size_t i = 0; i < value.size(); i++)
{
if( i > 0) ss << utility::conversions::to_string_t(", ");
ss << ApiClient::parameterToString(value[i]);
}

return ss.str();
}
static utility::string_t parameterToString(const std::vector<T>& value);

pplx::task<web::http::http_response> callApi(
const utility::string_t& path,
Expand All @@ -83,6 +71,20 @@ class ApiClient
std::shared_ptr<ApiConfiguration> m_Configuration;
};

template<class T>
utility::string_t ApiClient::parameterToString(const std::vector<T>& value)
{
utility::stringstream_t ss;

for( size_t i = 0; i < value.size(); i++)
{
if( i > 0) ss << utility::conversions::to_string_t(", ");
ss << ApiClient::parameterToString(value[i]);
}

return ss.str();
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ApiConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ApiConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ApiException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ApiException.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/HttpContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/HttpContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/IHttpBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/JsonBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/JsonBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/ModelBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
29 changes: 28 additions & 1 deletion samples/client/petstore/cpprest/ModelBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand All @@ -26,6 +26,8 @@
#include <cpprest/details/basic_types.h>
#include <cpprest/json.h>

#include <vector>

namespace io {
namespace swagger {
namespace client {
Expand Down Expand Up @@ -53,6 +55,8 @@ class ModelBase
static web::json::value toJson( int64_t value );
static web::json::value toJson( double value );
static web::json::value toJson( bool value );
template<class T>
static web::json::value toJson(const std::vector<T>& value);

static int64_t int64_tFromJson(web::json::value& val);
static int32_t int32_tFromJson(web::json::value& val);
Expand All @@ -70,6 +74,8 @@ class ModelBase
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, int64_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
template <class T>
static std::shared_ptr<HttpContent> toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );

static int64_t int64_tFromHttpContent(std::shared_ptr<HttpContent> val);
static int32_t int32_tFromHttpContent(std::shared_ptr<HttpContent> val);
Expand All @@ -86,6 +92,27 @@ class ModelBase
static std::shared_ptr<std::istream> fromBase64( const utility::string_t& encoded );
};

template<class T>
web::json::value ModelBase::toJson(const std::vector<T>& value) {
std::vector<web::json::value> ret;
for (auto& x : value) {
ret.push_back(toJson(x));
}

return web::json::value::array(ret);
}

template <class T>
std::shared_ptr<HttpContent> ModelBase::toHttpContent( const utility::string_t& name, const std::vector<T>& value, const utility::string_t& contentType ) {
web::json::value json_array = ModelBase::toJson(value);
std::shared_ptr<HttpContent> content( new HttpContent );
content->setName( name );
content->setContentDisposition( utility::conversions::to_string_t("form-data") );
content->setContentType( contentType );
content->setData( std::shared_ptr<std::istream>( new std::stringstream( utility::conversions::to_utf8string(json_array.serialize()) ) ) );
return content;
}

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/cpprest/MultipartFormData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* OpenAPI spec version: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by the swagger code generator 2.3.0-SNAPSHOT.
* NOTE: This class is auto generated by the swagger code generator 2.4.0-SNAPSHOT.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
Expand Down
Loading

0 comments on commit 73bd24d

Please sign in to comment.