[cpp-restbed-server] Allow to implement validation of input data for enumerations (#7717)

* Allow to implement validation of input data for enumerations

* Regen petstore sample
This commit is contained in:
Francesco Montorsi 2020-12-08 17:34:35 +01:00 committed by GitHub
parent 0b6d70d351
commit bb6785ad70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 50 deletions

View File

@ -183,6 +183,8 @@ void {{classname}}::{{setter}}({{{dataType}}} value)
{ {
{{#isEnum}}if (std::find(m_{{enumName}}.begin(), m_{{enumName}}.end(), value) != m_{{enumName}}.end()) { {{#isEnum}}if (std::find(m_{{enumName}}.begin(), m_{{enumName}}.end(), value) != m_{{enumName}}.end()) {
{{/isEnum}}m_{{name}} = value;{{#isEnum}} {{/isEnum}}m_{{name}} = value;{{#isEnum}}
} else {
throw std::runtime_error("Value " + value + " not allowed");
}{{/isEnum}} }{{/isEnum}}
} }
{{/vars}} {{/vars}}

View File

@ -38,23 +38,6 @@ namespace api {
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
class PetApi: public restbed::Service
{
public:
PetApi();
~PetApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<PetApiPetResource> m_spPetApiPetResource;
std::shared_ptr<PetApiPetPetIdResource> m_spPetApiPetPetIdResource;
std::shared_ptr<PetApiPetFindByStatusResource> m_spPetApiPetFindByStatusResource;
std::shared_ptr<PetApiPetFindByTagsResource> m_spPetApiPetFindByTagsResource;
std::shared_ptr<PetApiPetPetIdUploadImageResource> m_spPetApiPetPetIdUploadImageResource;
};
/// <summary> /// <summary>
/// Add a new pet to the store /// Add a new pet to the store
/// </summary> /// </summary>
@ -222,6 +205,26 @@ private:
}; };
//
// The restbed service to actually implement the REST server
//
class PetApi: public restbed::Service
{
public:
PetApi();
~PetApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<PetApiPetResource> m_spPetApiPetResource;
std::shared_ptr<PetApiPetPetIdResource> m_spPetApiPetPetIdResource;
std::shared_ptr<PetApiPetFindByStatusResource> m_spPetApiPetFindByStatusResource;
std::shared_ptr<PetApiPetFindByTagsResource> m_spPetApiPetFindByTagsResource;
std::shared_ptr<PetApiPetPetIdUploadImageResource> m_spPetApiPetPetIdUploadImageResource;
};
} }
} }
} }

View File

@ -38,21 +38,6 @@ namespace api {
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
class StoreApi: public restbed::Service
{
public:
StoreApi();
~StoreApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<StoreApiStoreOrderOrderIdResource> m_spStoreApiStoreOrderOrderIdResource;
std::shared_ptr<StoreApiStoreInventoryResource> m_spStoreApiStoreInventoryResource;
std::shared_ptr<StoreApiStoreOrderResource> m_spStoreApiStoreOrderResource;
};
/// <summary> /// <summary>
/// Delete purchase order by ID /// Delete purchase order by ID
/// </summary> /// </summary>
@ -149,6 +134,24 @@ private:
}; };
//
// The restbed service to actually implement the REST server
//
class StoreApi: public restbed::Service
{
public:
StoreApi();
~StoreApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<StoreApiStoreOrderOrderIdResource> m_spStoreApiStoreOrderOrderIdResource;
std::shared_ptr<StoreApiStoreInventoryResource> m_spStoreApiStoreInventoryResource;
std::shared_ptr<StoreApiStoreOrderResource> m_spStoreApiStoreOrderResource;
};
} }
} }
} }

View File

@ -38,24 +38,6 @@ namespace api {
using namespace org::openapitools::server::model; using namespace org::openapitools::server::model;
class UserApi: public restbed::Service
{
public:
UserApi();
~UserApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<UserApiUserResource> m_spUserApiUserResource;
std::shared_ptr<UserApiUserCreateWithArrayResource> m_spUserApiUserCreateWithArrayResource;
std::shared_ptr<UserApiUserCreateWithListResource> m_spUserApiUserCreateWithListResource;
std::shared_ptr<UserApiUserUsernameResource> m_spUserApiUserUsernameResource;
std::shared_ptr<UserApiUserLoginResource> m_spUserApiUserLoginResource;
std::shared_ptr<UserApiUserLogoutResource> m_spUserApiUserLogoutResource;
};
/// <summary> /// <summary>
/// Create user /// Create user
/// </summary> /// </summary>
@ -240,6 +222,27 @@ private:
}; };
//
// The restbed service to actually implement the REST server
//
class UserApi: public restbed::Service
{
public:
UserApi();
~UserApi();
void startService(int const& port);
void stopService();
protected:
std::shared_ptr<UserApiUserResource> m_spUserApiUserResource;
std::shared_ptr<UserApiUserCreateWithArrayResource> m_spUserApiUserCreateWithArrayResource;
std::shared_ptr<UserApiUserCreateWithListResource> m_spUserApiUserCreateWithListResource;
std::shared_ptr<UserApiUserUsernameResource> m_spUserApiUserUsernameResource;
std::shared_ptr<UserApiUserLoginResource> m_spUserApiUserLoginResource;
std::shared_ptr<UserApiUserLogoutResource> m_spUserApiUserLogoutResource;
};
} }
} }
} }

View File

@ -123,6 +123,8 @@ void Order::setStatus(std::string value)
{ {
if (std::find(m_StatusEnum.begin(), m_StatusEnum.end(), value) != m_StatusEnum.end()) { if (std::find(m_StatusEnum.begin(), m_StatusEnum.end(), value) != m_StatusEnum.end()) {
m_Status = value; m_Status = value;
} else {
throw std::runtime_error("Value " + value + " not allowed");
} }
} }
bool Order::isComplete() const bool Order::isComplete() const

View File

@ -161,6 +161,8 @@ void Pet::setStatus(std::string value)
{ {
if (std::find(m_StatusEnum.begin(), m_StatusEnum.end(), value) != m_StatusEnum.end()) { if (std::find(m_StatusEnum.begin(), m_StatusEnum.end(), value) != m_StatusEnum.end()) {
m_Status = value; m_Status = value;
} else {
throw std::runtime_error("Value " + value + " not allowed");
} }
} }