forked from loafle/openapi-generator-original
When components/schema/<object> reference other objects, validate() was getting called on the referenced objects with no arguments. The return value was void, but checked as if it was a boolean value.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
#include "Helpers.h"
|
||||
#include <regex>
|
||||
|
||||
namespace org::openapitools::server::helpers
|
||||
{
|
||||
|
||||
const std::regex regexRfc3339_date(R"(^(\d{4})\-(\d{2})\-(\d{2})$)");
|
||||
const std::regex regexRfc3339_date_time(
|
||||
R"(^(\d{4})\-(\d{2})\-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(\.\d+)?([Zz]|([\+\-])(\d{2}):(\d{2}))$)"
|
||||
);
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
// Determine if given year is a leap year
|
||||
// See RFC 3339, Appendix C https://tools.ietf.org/html/rfc3339#appendix-C
|
||||
bool isLeapYear(const uint16_t year) {
|
||||
return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
|
||||
}
|
||||
|
||||
bool validateDateValues(const uint16_t year, const uint16_t month, const uint16_t day) {
|
||||
return !(
|
||||
(month == 0 || month > 12)
|
||||
|| (day == 0)
|
||||
|| (month == 2 && day > (28 + (isLeapYear(year) ? 1 : 0)))
|
||||
|| (month <= 7 && day > (30 + month % 2))
|
||||
|| (month >= 8 && day > (31 - month % 2))
|
||||
);
|
||||
}
|
||||
|
||||
bool validateTimeValues(const uint16_t hours, const uint16_t minutes, const uint16_t seconds) {
|
||||
return (hours <= 23) && (minutes <= 59) && (seconds <= 60);
|
||||
}
|
||||
}
|
||||
|
||||
bool validateRfc3339_date(const std::string& str) {
|
||||
std::smatch match;
|
||||
const bool found = std::regex_search(str, match, regexRfc3339_date);
|
||||
return found && validateDateValues(static_cast<uint16_t>(std::stoi(match[1])),
|
||||
static_cast<uint16_t>(std::stoi(match[2])),
|
||||
static_cast<uint16_t>(std::stoi(match[3])));
|
||||
}
|
||||
|
||||
bool validateRfc3339_date_time(const std::string& str) {
|
||||
std::smatch match;
|
||||
const bool found = std::regex_search(str, match, regexRfc3339_date_time);
|
||||
return found
|
||||
&& validateDateValues(static_cast<uint16_t>(std::stoi(match[1])),
|
||||
static_cast<uint16_t>(std::stoi(match[2])),
|
||||
static_cast<uint16_t>(std::stoi(match[3])))
|
||||
&& validateTimeValues(static_cast<uint16_t>(std::stoi(match[4])),
|
||||
static_cast<uint16_t>(std::stoi(match[5])),
|
||||
static_cast<uint16_t>(std::stoi(match[6])));
|
||||
}
|
||||
|
||||
std::string toStringValue(const std::string &value){
|
||||
return std::string(value);
|
||||
}
|
||||
|
||||
std::string toStringValue(const int32_t value){
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
std::string toStringValue(const int64_t value){
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
std::string toStringValue(const bool value){
|
||||
return value ? std::string("true") : std::string("false");
|
||||
}
|
||||
|
||||
std::string toStringValue(const float value){
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
std::string toStringValue(const double value){
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, std::string &value){
|
||||
value = std::string(inStr);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, int32_t &value){
|
||||
try {
|
||||
value = std::stoi( inStr );
|
||||
}
|
||||
catch (const std::invalid_argument&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, int64_t &value){
|
||||
try {
|
||||
value = std::stol( inStr );
|
||||
}
|
||||
catch (const std::invalid_argument&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, bool &value){
|
||||
if (inStr == "true") {
|
||||
value = true;
|
||||
return true;
|
||||
}
|
||||
if (inStr == "false") {
|
||||
value = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, float &value){
|
||||
try {
|
||||
value = std::stof( inStr );
|
||||
}
|
||||
catch (const std::invalid_argument&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool fromStringValue(const std::string &inStr, double &value){
|
||||
try {
|
||||
value = std::stod( inStr );
|
||||
}
|
||||
catch (const std::invalid_argument&) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace org::openapitools::server::helpers
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
/*
|
||||
* Helpers.h
|
||||
*
|
||||
* This is the helper class for models and primitives
|
||||
*/
|
||||
|
||||
#ifndef Helpers_H_
|
||||
#define Helpers_H_
|
||||
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace org::openapitools::server::helpers
|
||||
{
|
||||
|
||||
class ValidationException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
explicit ValidationException(const std::string& what)
|
||||
: std::runtime_error(what)
|
||||
{ }
|
||||
~ValidationException() override = default;
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Validate a string against the full-date definition of RFC 3339, section 5.6.
|
||||
/// </summary>
|
||||
bool validateRfc3339_date(const std::string& str);
|
||||
|
||||
/// <summary>
|
||||
/// Validate a string against the date-time definition of RFC 3339, section 5.6.
|
||||
/// </summary>
|
||||
bool validateRfc3339_date_time(const std::string& str);
|
||||
|
||||
namespace sfinae_helpers
|
||||
{
|
||||
struct NoType {};
|
||||
template <typename T1, typename T2> NoType operator==(const T1&, const T2&);
|
||||
|
||||
template <typename T1, typename T2> class EqualsOperatorAvailable
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
value = !std::is_same< decltype(std::declval<T1>() == std::declval<T2>()), NoType >::value
|
||||
};
|
||||
};
|
||||
} // namespace sfinae_helpers
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the given vector<T> only has unique elements. T must provide the == operator.
|
||||
/// </summary>
|
||||
template <typename T>
|
||||
bool hasOnlyUniqueItems(const std::vector<T>& vec)
|
||||
{
|
||||
static_assert(sfinae_helpers::EqualsOperatorAvailable<T, T>::value,
|
||||
"hasOnlyUniqueItems<T> cannot be called, passed template type does not provide == operator.");
|
||||
if (vec.size() <= 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// Compare every element of vec to every other element of vec.
|
||||
// This isn't an elegant way to do this, since it's O(n^2),
|
||||
// but it's the best solution working only with the == operator.
|
||||
// This could be greatly improved if our models provided a valid hash
|
||||
// and/or the < operator
|
||||
for (size_t i = 0; i < vec.size() - 1; i++)
|
||||
{
|
||||
for (size_t j = i + 1; j < vec.size(); j++)
|
||||
{
|
||||
if (vec[i] == vec[j])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string toStringValue(const std::string &value);
|
||||
std::string toStringValue(const int32_t value);
|
||||
std::string toStringValue(const int64_t value);
|
||||
std::string toStringValue(const bool value);
|
||||
std::string toStringValue(const float value);
|
||||
std::string toStringValue(const double value);
|
||||
|
||||
bool fromStringValue(const std::string &inStr, std::string &value);
|
||||
bool fromStringValue(const std::string &inStr, int32_t &value);
|
||||
bool fromStringValue(const std::string &inStr, int64_t &value);
|
||||
bool fromStringValue(const std::string &inStr, bool &value);
|
||||
bool fromStringValue(const std::string &inStr, float &value);
|
||||
bool fromStringValue(const std::string &inStr, double &value);
|
||||
template<typename T>
|
||||
bool fromStringValue(const std::vector<std::string> &inStr, std::vector<T> &value){
|
||||
try{
|
||||
for(auto & item : inStr){
|
||||
T itemValue;
|
||||
if(fromStringValue(item, itemValue)){
|
||||
value.push_back(itemValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...){
|
||||
return false;
|
||||
}
|
||||
return value.size() > 0;
|
||||
}
|
||||
template<typename T>
|
||||
bool fromStringValue(const std::string &inStr, std::vector<T> &value, char separator = ','){
|
||||
std::vector<std::string> inStrings;
|
||||
std::istringstream f(inStr);
|
||||
std::string s;
|
||||
while (std::getline(f, s, separator)) {
|
||||
inStrings.push_back(s);
|
||||
}
|
||||
return fromStringValue(inStrings, value);
|
||||
}
|
||||
|
||||
} // namespace org::openapitools::server::helpers
|
||||
|
||||
#endif // Helpers_H_
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
|
||||
#include "MiddleType.h"
|
||||
#include "Helpers.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace org::openapitools::server::model
|
||||
{
|
||||
|
||||
MiddleType::MiddleType()
|
||||
{
|
||||
m_Inner = "";
|
||||
|
||||
}
|
||||
|
||||
void MiddleType::validate() const
|
||||
{
|
||||
std::stringstream msg;
|
||||
if (!validate(msg))
|
||||
{
|
||||
throw org::openapitools::server::helpers::ValidationException(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
bool MiddleType::validate(std::stringstream& msg) const
|
||||
{
|
||||
return validate(msg, "");
|
||||
}
|
||||
|
||||
bool MiddleType::validate(std::stringstream& msg, const std::string& pathPrefix) const
|
||||
{
|
||||
bool success = true;
|
||||
const std::string _pathPrefix = pathPrefix.empty() ? "MiddleType" : pathPrefix;
|
||||
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool MiddleType::operator==(const MiddleType& rhs) const
|
||||
{
|
||||
return
|
||||
|
||||
|
||||
(getInner() == rhs.getInner())
|
||||
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
bool MiddleType::operator!=(const MiddleType& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json& j, const MiddleType& o)
|
||||
{
|
||||
j = nlohmann::json::object();
|
||||
j["inner"] = o.m_Inner;
|
||||
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json& j, MiddleType& o)
|
||||
{
|
||||
j.at("inner").get_to(o.m_Inner);
|
||||
|
||||
}
|
||||
|
||||
std::string MiddleType::getInner() const
|
||||
{
|
||||
return m_Inner;
|
||||
}
|
||||
void MiddleType::setInner(std::string const& value)
|
||||
{
|
||||
m_Inner = value;
|
||||
}
|
||||
|
||||
|
||||
} // namespace org::openapitools::server::model
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
/*
|
||||
* MiddleType.h
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MiddleType_H_
|
||||
#define MiddleType_H_
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace org::openapitools::server::model
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
class MiddleType
|
||||
{
|
||||
public:
|
||||
MiddleType();
|
||||
virtual ~MiddleType() = default;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validate the current data in the model. Throws a ValidationException on failure.
|
||||
/// </summary>
|
||||
void validate() const;
|
||||
|
||||
/// <summary>
|
||||
/// Validate the current data in the model. Returns false on error and writes an error
|
||||
/// message into the given stringstream.
|
||||
/// </summary>
|
||||
bool validate(std::stringstream& msg) const;
|
||||
|
||||
/// <summary>
|
||||
/// Helper overload for validate. Used when one model stores another model and calls it's validate.
|
||||
/// Not meant to be called outside that case.
|
||||
/// </summary>
|
||||
bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
|
||||
|
||||
bool operator==(const MiddleType& rhs) const;
|
||||
bool operator!=(const MiddleType& rhs) const;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// MiddleType members
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
std::string getInner() const;
|
||||
void setInner(std::string const& value);
|
||||
|
||||
friend void to_json(nlohmann::json& j, const MiddleType& o);
|
||||
friend void from_json(const nlohmann::json& j, MiddleType& o);
|
||||
protected:
|
||||
std::string m_Inner;
|
||||
|
||||
|
||||
};
|
||||
|
||||
} // namespace org::openapitools::server::model
|
||||
|
||||
#endif /* MiddleType_H_ */
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
|
||||
|
||||
#include "OuterType.h"
|
||||
#include "Helpers.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace org::openapitools::server::model
|
||||
{
|
||||
|
||||
OuterType::OuterType()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OuterType::validate() const
|
||||
{
|
||||
std::stringstream msg;
|
||||
if (!validate(msg))
|
||||
{
|
||||
throw org::openapitools::server::helpers::ValidationException(msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
bool OuterType::validate(std::stringstream& msg) const
|
||||
{
|
||||
return validate(msg, "");
|
||||
}
|
||||
|
||||
bool OuterType::validate(std::stringstream& msg, const std::string& pathPrefix) const
|
||||
{
|
||||
bool success = true;
|
||||
const std::string _pathPrefix = pathPrefix.empty() ? "OuterType" : pathPrefix;
|
||||
|
||||
|
||||
if (!m_Middle.validate(msg, _pathPrefix + ".middle")) {
|
||||
msg << _pathPrefix << ": Middle is invalid;";
|
||||
success = false;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
bool OuterType::operator==(const OuterType& rhs) const
|
||||
{
|
||||
return
|
||||
|
||||
|
||||
(getMiddle() == rhs.getMiddle())
|
||||
|
||||
|
||||
;
|
||||
}
|
||||
|
||||
bool OuterType::operator!=(const OuterType& rhs) const
|
||||
{
|
||||
return !(*this == rhs);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json& j, const OuterType& o)
|
||||
{
|
||||
j = nlohmann::json::object();
|
||||
j["middle"] = o.m_Middle;
|
||||
|
||||
}
|
||||
|
||||
void from_json(const nlohmann::json& j, OuterType& o)
|
||||
{
|
||||
j.at("middle").get_to(o.m_Middle);
|
||||
|
||||
}
|
||||
|
||||
org::openapitools::server::model::MiddleType OuterType::getMiddle() const
|
||||
{
|
||||
return m_Middle;
|
||||
}
|
||||
void OuterType::setMiddle(org::openapitools::server::model::MiddleType const& value)
|
||||
{
|
||||
m_Middle = value;
|
||||
}
|
||||
|
||||
|
||||
} // namespace org::openapitools::server::model
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Test swagger file
|
||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
* https://openapi-generator.tech
|
||||
* Do not edit the class manually.
|
||||
*/
|
||||
/*
|
||||
* OuterType.h
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef OuterType_H_
|
||||
#define OuterType_H_
|
||||
|
||||
|
||||
#include "MiddleType.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace org::openapitools::server::model
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
class OuterType
|
||||
{
|
||||
public:
|
||||
OuterType();
|
||||
virtual ~OuterType() = default;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validate the current data in the model. Throws a ValidationException on failure.
|
||||
/// </summary>
|
||||
void validate() const;
|
||||
|
||||
/// <summary>
|
||||
/// Validate the current data in the model. Returns false on error and writes an error
|
||||
/// message into the given stringstream.
|
||||
/// </summary>
|
||||
bool validate(std::stringstream& msg) const;
|
||||
|
||||
/// <summary>
|
||||
/// Helper overload for validate. Used when one model stores another model and calls it's validate.
|
||||
/// Not meant to be called outside that case.
|
||||
/// </summary>
|
||||
bool validate(std::stringstream& msg, const std::string& pathPrefix) const;
|
||||
|
||||
bool operator==(const OuterType& rhs) const;
|
||||
bool operator!=(const OuterType& rhs) const;
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// OuterType members
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
org::openapitools::server::model::MiddleType getMiddle() const;
|
||||
void setMiddle(org::openapitools::server::model::MiddleType const& value);
|
||||
|
||||
friend void to_json(nlohmann::json& j, const OuterType& o);
|
||||
friend void from_json(const nlohmann::json& j, OuterType& o);
|
||||
protected:
|
||||
org::openapitools::server::model::MiddleType m_Middle;
|
||||
|
||||
|
||||
};
|
||||
|
||||
} // namespace org::openapitools::server::model
|
||||
|
||||
#endif /* OuterType_H_ */
|
||||
Reference in New Issue
Block a user