forked from loafle/openapi-generator-original
[cpp pistache] fix leading number in model names and apply modelNamePrefix in all cases (#730)
* Fix numbers in nameInCamelCase. Add possibility to prefix Names to avoid conflicts * Allow unique prefixing for template functions as well
This commit is contained in:
@@ -168,7 +168,7 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
|
||||
return sanitizeName(name);
|
||||
}
|
||||
|
||||
if (isReservedWord(name)) {
|
||||
if (isReservedWord(name) || name.matches("^\\d.*")) {
|
||||
return escapeReservedWord(name);
|
||||
}
|
||||
|
||||
@@ -205,6 +205,10 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
|
||||
|
||||
@Override
|
||||
public String toParamName(String name) {
|
||||
if (isReservedWord(name) || name.matches("^\\d.*")) {
|
||||
return escapeReservedWord(name);
|
||||
}
|
||||
|
||||
return sanitizeName(super.toParamName(name));
|
||||
}
|
||||
|
||||
@@ -217,6 +221,9 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
|
||||
} else {
|
||||
nameInCamelCase = sanitizeName(nameInCamelCase);
|
||||
}
|
||||
if (isReservedWord(nameInCamelCase) || nameInCamelCase.matches("^\\d.*")) {
|
||||
nameInCamelCase = escapeReservedWord(nameInCamelCase);
|
||||
}
|
||||
property.nameInCamelCase = nameInCamelCase;
|
||||
return property;
|
||||
}
|
||||
|
||||
@@ -46,6 +46,8 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
protected boolean isAddExternalLibs = true;
|
||||
public static final String OPTIONAL_EXTERNAL_LIB = "addExternalLibs";
|
||||
public static final String OPTIONAL_EXTERNAL_LIB_DESC = "Add the Possibility to fetch and compile external Libraries needed by this Framework.";
|
||||
protected final String PREFIX = "";
|
||||
|
||||
@Override
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.SERVER;
|
||||
@@ -63,6 +65,9 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
|
||||
public CppPistacheServerCodegen() {
|
||||
super();
|
||||
if (StringUtils.isEmpty(modelNamePrefix)) {
|
||||
modelNamePrefix = PREFIX;
|
||||
}
|
||||
|
||||
apiPackage = "org.openapitools.server.api";
|
||||
modelPackage = "org.openapitools.server.model";
|
||||
@@ -83,8 +88,8 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
|
||||
reservedWords = new HashSet<>();
|
||||
|
||||
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", "ModelBase.h"));
|
||||
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", "ModelBase.cpp"));
|
||||
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h"));
|
||||
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp"));
|
||||
supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
|
||||
@@ -117,7 +122,14 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("modelNamePrefix")) {
|
||||
additionalProperties().put("prefix", modelNamePrefix);
|
||||
supportingFiles.clear();
|
||||
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "model", modelNamePrefix + "ModelBase.h"));
|
||||
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp"));
|
||||
supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
}
|
||||
additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\."));
|
||||
additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::"));
|
||||
additionalProperties.put("apiNamespaceDeclarations", apiPackage.split("\\."));
|
||||
@@ -138,6 +150,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CodegenModel fromModel(String name, Schema model, Map<String, Schema> allDefinitions) {
|
||||
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
|
||||
@@ -240,7 +253,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return initialCaps(name);
|
||||
return initialCaps(toModelName(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -265,7 +278,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
|
||||
@Override
|
||||
public String toApiFilename(String name) {
|
||||
return initialCaps(name) + "Api";
|
||||
return modelNamePrefix + initialCaps(name) + "Api";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@ void {{classname}}::{{operationIdSnakeCase}}_handler(const Pistache::Rest::Reque
|
||||
nlohmann::json request_body = nlohmann::json::parse(request.body());
|
||||
{{^isPrimitiveType}}{{^isContainer}}
|
||||
{{paramName}}.fromJson(request_body);
|
||||
{{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} ModelArrayHelper{{/isListContainer}}{{#isMapContainer}} ModelMapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}}
|
||||
{{/isContainer}}{{/isPrimitiveType}}{{#isContainer}} {{paramName}} = {{#isListContainer}} {{prefix}}ModelArrayHelper{{/isListContainer}}{{#isMapContainer}} {{prefix}}ModelMapHelper{{/isMapContainer}}::fromJson<{{items.baseType}}>(request_body);{{/isContainer}}
|
||||
{{#isPrimitiveType}}
|
||||
// The conversion is done automatically by the json library
|
||||
{{paramName}} = request_body;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#define {{classname}}_H_
|
||||
|
||||
{{{defaultInclude}}}
|
||||
#include "ModelBase.h"
|
||||
#include "{{prefix}}ModelBase.h"
|
||||
|
||||
{{#imports}}{{{this}}}
|
||||
{{/imports}}
|
||||
@@ -22,14 +22,14 @@ namespace {{this}} {
|
||||
/// {{description}}
|
||||
/// </summary>
|
||||
class {{declspec}} {{classname}}
|
||||
: public ModelBase
|
||||
: public {{prefix}}ModelBase
|
||||
{
|
||||
public:
|
||||
{{classname}}();
|
||||
virtual ~{{classname}}();
|
||||
|
||||
/////////////////////////////////////////////
|
||||
/// ModelBase overrides
|
||||
/// {{prefix}}ModelBase overrides
|
||||
|
||||
void validate() override;
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ nlohmann::json {{classname}}::toJson() const
|
||||
nlohmann::json jsonArray;
|
||||
for( auto& item : m_{{name}} )
|
||||
{
|
||||
jsonArray.push_back(ModelBase::toJson(item));
|
||||
jsonArray.push_back({{prefix}}ModelBase::toJson(item));
|
||||
}
|
||||
{{#required}}val["{{baseName}}"] = jsonArray;
|
||||
{{/required}}{{^required}}
|
||||
@@ -50,9 +50,9 @@ nlohmann::json {{classname}}::toJson() const
|
||||
}
|
||||
{{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet)
|
||||
{
|
||||
val["{{baseName}}"] = ModelBase::toJson(m_{{name}});
|
||||
val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
||||
}
|
||||
{{/required}}{{#required}}val["{{baseName}}"] = ModelBase::toJson(m_{{name}});
|
||||
{{/required}}{{#required}}val["{{baseName}}"] = {{prefix}}ModelBase::toJson(m_{{name}});
|
||||
{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
|
||||
|
||||
return val;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{{>licenseInfo}}
|
||||
/*
|
||||
* ModelBase.h
|
||||
* {{prefix}}ModelBase.h
|
||||
*
|
||||
* This is the base class for all model classes
|
||||
*/
|
||||
|
||||
#ifndef ModelBase_H_
|
||||
#define ModelBase_H_
|
||||
#ifndef {{prefix}}ModelBase_H_
|
||||
#define {{prefix}}ModelBase_H_
|
||||
|
||||
{{{defaultInclude}}}
|
||||
#include "json.hpp"
|
||||
@@ -19,11 +19,11 @@
|
||||
namespace {{this}} {
|
||||
{{/modelNamespaceDeclarations}}
|
||||
|
||||
class {{declspec}} ModelBase
|
||||
class {{declspec}} {{prefix}}ModelBase
|
||||
{
|
||||
public:
|
||||
ModelBase();
|
||||
virtual ~ModelBase();
|
||||
{{prefix}}ModelBase();
|
||||
virtual ~{{prefix}}ModelBase();
|
||||
|
||||
virtual void validate() = 0;
|
||||
|
||||
@@ -36,16 +36,16 @@ public:
|
||||
static int64_t toJson( int64_t const value );
|
||||
static double toJson( double const value );
|
||||
static bool toJson( bool const value );
|
||||
static nlohmann::json toJson(ModelBase const& content );
|
||||
static nlohmann::json toJson({{prefix}}ModelBase const& content );
|
||||
};
|
||||
|
||||
class ModelArrayHelper {
|
||||
class {{prefix}}ModelArrayHelper {
|
||||
public:
|
||||
template<typename T>
|
||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
||||
T *ptrTest;
|
||||
std::vector<T> val;
|
||||
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
|
||||
if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) {
|
||||
if (!json.empty()) {
|
||||
for (auto &item : json.items()) {
|
||||
T entry;
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ArrayHelper {
|
||||
class {{prefix}}ArrayHelper {
|
||||
public:
|
||||
template<typename T>
|
||||
static std::vector<T> fromJson(nlohmann::json& json) {
|
||||
@@ -82,13 +82,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ModelMapHelper {
|
||||
class {{prefix}}ModelMapHelper {
|
||||
public:
|
||||
template<typename T>
|
||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
||||
T *ptrTest;
|
||||
std::map<std::string, T> val;
|
||||
if (dynamic_cast<ModelBase*>(ptrTest) != nullptr) {
|
||||
if (dynamic_cast<{{prefix}}ModelBase*>(ptrTest) != nullptr) {
|
||||
if (!json.empty()) {
|
||||
for (auto &item : json.items()) {
|
||||
T entry;
|
||||
@@ -110,7 +110,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class MapHelper {
|
||||
class {{prefix}}MapHelper {
|
||||
public:
|
||||
template<typename T>
|
||||
static std::map<std::string, T> & fromJson(nlohmann::json& json) {
|
||||
@@ -139,4 +139,4 @@ public:
|
||||
}
|
||||
{{/modelNamespaceDeclarations}}
|
||||
|
||||
#endif /* ModelBase_H_ */
|
||||
#endif /* {{prefix}}ModelBase_H_ */
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
{{>licenseInfo}}
|
||||
#include "ModelBase.h"
|
||||
#include "{{prefix}}ModelBase.h"
|
||||
|
||||
{{#modelNamespaceDeclarations}}
|
||||
namespace {{this}} {
|
||||
{{/modelNamespaceDeclarations}}
|
||||
|
||||
ModelBase::ModelBase()
|
||||
{{prefix}}ModelBase::{{prefix}}ModelBase()
|
||||
{
|
||||
}
|
||||
ModelBase::~ModelBase()
|
||||
{{prefix}}ModelBase::~{{prefix}}ModelBase()
|
||||
{
|
||||
}
|
||||
|
||||
std::string ModelBase::toJson( std::string const& value )
|
||||
std::string {{prefix}}ModelBase::toJson( std::string const& value )
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string ModelBase::toJson( std::time_t const& value )
|
||||
std::string {{prefix}}ModelBase::toJson( std::time_t const& value )
|
||||
{
|
||||
char buf[sizeof "2011-10-08T07:07:09Z"];
|
||||
strftime(buf, sizeof buf, "%FT%TZ", gmtime(&value));
|
||||
return buf;
|
||||
}
|
||||
|
||||
int32_t ModelBase::toJson( int32_t const value )
|
||||
int32_t {{prefix}}ModelBase::toJson( int32_t const value )
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
int64_t ModelBase::toJson( int64_t const value )
|
||||
int64_t {{prefix}}ModelBase::toJson( int64_t const value )
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
double ModelBase::toJson( double const value )
|
||||
double {{prefix}}ModelBase::toJson( double const value )
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
bool ModelBase::toJson( bool const value )
|
||||
bool {{prefix}}ModelBase::toJson( bool const value )
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
nlohmann::json ModelBase::toJson(ModelBase const& content )
|
||||
nlohmann::json {{prefix}}ModelBase::toJson({{prefix}}ModelBase const& content )
|
||||
{
|
||||
return content.toJson();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user