Skip to content

Commit

Permalink
[cpp rest-sdk]Fix precision (OpenAPITools#1293)
Browse files Browse the repository at this point in the history
* Convert floating point numbers to string with higher precision
* Update PetStore
  • Loading branch information
whoan authored and etherealjoy committed Nov 1, 2018
1 parent 7820af8 commit 7f5b38a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
#include "MultipartFormData.h"
#include "ModelBase.h"

#include <sstream>
#include <limits>
#include <iomanip>

template <typename T>
utility::string_t toString(const T value)
{
std::ostringstream out;
out << std::setprecision(std::numeric_limits<T>::digits10) << std::fixed << value;
return out.str();
}

{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
Expand Down Expand Up @@ -46,12 +58,12 @@ utility::string_t ApiClient::parameterToString(int32_t value)

utility::string_t ApiClient::parameterToString(float value)
{
return utility::conversions::to_string_t(std::to_string(value));
return utility::conversions::to_string_t(toString(value));
}

utility::string_t ApiClient::parameterToString(double value)
{
return utility::conversions::to_string_t(std::to_string(value));
return utility::conversions::to_string_t(toString(value));
}

utility::string_t ApiClient::parameterToString(const utility::datetime &value)
Expand Down
16 changes: 14 additions & 2 deletions samples/client/petstore/cpp-restsdk/ApiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
#include "MultipartFormData.h"
#include "ModelBase.h"

#include <sstream>
#include <limits>
#include <iomanip>

template <typename T>
utility::string_t toString(const T value)
{
std::ostringstream out;
out << std::setprecision(std::numeric_limits<T>::digits10) << std::fixed << value;
return out.str();
}

namespace org {
namespace openapitools {
namespace client {
Expand Down Expand Up @@ -57,12 +69,12 @@ utility::string_t ApiClient::parameterToString(int32_t value)

utility::string_t ApiClient::parameterToString(float value)
{
return utility::conversions::to_string_t(std::to_string(value));
return utility::conversions::to_string_t(toString(value));
}

utility::string_t ApiClient::parameterToString(double value)
{
return utility::conversions::to_string_t(std::to_string(value));
return utility::conversions::to_string_t(toString(value));
}

utility::string_t ApiClient::parameterToString(const utility::datetime &value)
Expand Down

0 comments on commit 7f5b38a

Please sign in to comment.