diff --git a/bin/configs/c.yaml b/bin/configs/c.yaml index 378b5d1f367..2759591e2ac 100644 --- a/bin/configs/c.yaml +++ b/bin/configs/c.yaml @@ -2,3 +2,6 @@ generatorName: c outputDir: samples/client/petstore/c inputSpec: modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml templateDir: modules/openapi-generator/src/main/resources/C-libcurl +modelNameMappings: + another_model: MappedModel + another_property: mappedProperty diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index 16436597ffc..2d20d66df12 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -559,6 +559,11 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf @Override public String toVarName(String name) { + // obtain the name from nameMapping directly if provided + if (nameMapping.containsKey(name)) { + return nameMapping.get(name); + } + // sanitize name name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'. // if it's all upper case, convert to lower case @@ -578,6 +583,11 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf @Override public String toParamName(String name) { + // obtain the name from parameterNameMapping directly if provided + if (parameterNameMapping.containsKey(name)) { + return parameterNameMapping.get(name); + } + // should be the same as variable name if (isReservedWord(name) || name.matches("^\\d.*")) { name = escapeReservedWord(name); @@ -588,6 +598,11 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf @Override public String toModelName(String name) { + // obtain the name from modelNameMapping directly if provided + if (modelNameMapping.containsKey(name)) { + return modelNameMapping.get(name); + } + name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'. if (!StringUtils.isEmpty(modelNamePrefix)) { diff --git a/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml b/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml index 475f6cb678f..82276427b6f 100644 --- a/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml +++ b/modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml @@ -715,3 +715,10 @@ definitions: type: string message: type: string + another_model: + description: to test mapping features + type: object + properties: + another_property: + type: integer + format: int32 diff --git a/samples/client/petstore/c/.openapi-generator/FILES b/samples/client/petstore/c/.openapi-generator/FILES index 51014a47cdc..1e7a61f5080 100644 --- a/samples/client/petstore/c/.openapi-generator/FILES +++ b/samples/client/petstore/c/.openapi-generator/FILES @@ -6,6 +6,7 @@ api/StoreAPI.c api/StoreAPI.h api/UserAPI.c api/UserAPI.h +docs/MappedModel.md docs/PetAPI.md docs/StoreAPI.md docs/UserAPI.md @@ -27,6 +28,8 @@ model/api_response.c model/api_response.h model/category.c model/category.h +model/mapped_model.c +model/mapped_model.h model/object.c model/object.h model/order.c diff --git a/samples/client/petstore/c/README.md b/samples/client/petstore/c/README.md index d6d5155cca5..ea388a72b66 100644 --- a/samples/client/petstore/c/README.md +++ b/samples/client/petstore/c/README.md @@ -89,6 +89,7 @@ Category | Method | HTTP request | Description ## Documentation for Models + - [MappedModel_t](docs/MappedModel.md) - [api_response_t](docs/api_response.md) - [category_t](docs/category.md) - [order_t](docs/order.md) diff --git a/samples/client/petstore/c/docs/MappedModel.md b/samples/client/petstore/c/docs/MappedModel.md new file mode 100644 index 00000000000..88489745e67 --- /dev/null +++ b/samples/client/petstore/c/docs/MappedModel.md @@ -0,0 +1,10 @@ +# MappedModel_t + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**another_property** | **int** | | [optional] + +[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) + + diff --git a/samples/client/petstore/c/model/mapped_model.c b/samples/client/petstore/c/model/mapped_model.c new file mode 100644 index 00000000000..4c70c720bfc --- /dev/null +++ b/samples/client/petstore/c/model/mapped_model.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "MappedModel.h" + + + +MappedModel_t *MappedModel_create( + int another_property + ) { + MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t)); + if (!MappedModel_local_var) { + return NULL; + } + MappedModel_local_var->another_property = another_property; + + return MappedModel_local_var; +} + + +void MappedModel_free(MappedModel_t *MappedModel) { + if(NULL == MappedModel){ + return ; + } + listEntry_t *listEntry; + free(MappedModel); +} + +cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) { + cJSON *item = cJSON_CreateObject(); + + // MappedModel->another_property + if(MappedModel->another_property) { + if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) { + goto fail; //Numeric + } + } + + return item; +fail: + if (item) { + cJSON_Delete(item); + } + return NULL; +} + +MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){ + + MappedModel_t *MappedModel_local_var = NULL; + + // MappedModel->another_property + cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property"); + if (another_property) { + if(!cJSON_IsNumber(another_property)) + { + goto end; //Numeric + } + } + + + MappedModel_local_var = MappedModel_create ( + another_property ? another_property->valuedouble : 0 + ); + + return MappedModel_local_var; +end: + return NULL; + +} diff --git a/samples/client/petstore/c/model/mapped_model.h b/samples/client/petstore/c/model/mapped_model.h new file mode 100644 index 00000000000..244aab44cbc --- /dev/null +++ b/samples/client/petstore/c/model/mapped_model.h @@ -0,0 +1,37 @@ +/* + * MappedModel.h + * + * to test mapping features + */ + +#ifndef _MappedModel_H_ +#define _MappedModel_H_ + +#include +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" +#include "../include/binary.h" + +typedef struct MappedModel_t MappedModel_t; + + + + +typedef struct MappedModel_t { + int another_property; //numeric + +} MappedModel_t; + +MappedModel_t *MappedModel_create( + int another_property +); + +void MappedModel_free(MappedModel_t *MappedModel); + +MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON); + +cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel); + +#endif /* _MappedModel_H_ */ + diff --git a/samples/client/petstore/c/unit-test/test_mapped_model.c b/samples/client/petstore/c/unit-test/test_mapped_model.c new file mode 100644 index 00000000000..214b17d14e6 --- /dev/null +++ b/samples/client/petstore/c/unit-test/test_mapped_model.c @@ -0,0 +1,58 @@ +#ifndef mapped_model_TEST +#define mapped_model_TEST + +// the following is to include only the main from the first c file +#ifndef TEST_MAIN +#define TEST_MAIN +#define mapped_model_MAIN +#endif // TEST_MAIN + +#include +#include +#include +#include +#include "../external/cJSON.h" + +#include "../model/mapped_model.h" +MappedModel_t* instantiate_MappedModel(int include_optional); + + + +MappedModel_t* instantiate_MappedModel(int include_optional) { + MappedModel_t* MappedModel = NULL; + if (include_optional) { + MappedModel = MappedModel_create( + 56 + ); + } else { + MappedModel = MappedModel_create( + 56 + ); + } + + return MappedModel; +} + + +#ifdef mapped_model_MAIN + +void test_MappedModel(int include_optional) { + MappedModel_t* MappedModel_1 = instantiate_MappedModel(include_optional); + + cJSON* jsonMappedModel_1 = MappedModel_convertToJSON(MappedModel_1); + printf("MappedModel :\n%s\n", cJSON_Print(jsonMappedModel_1)); + MappedModel_t* MappedModel_2 = MappedModel_parseFromJSON(jsonMappedModel_1); + cJSON* jsonMappedModel_2 = MappedModel_convertToJSON(MappedModel_2); + printf("repeating MappedModel:\n%s\n", cJSON_Print(jsonMappedModel_2)); +} + +int main() { + test_MappedModel(1); + test_MappedModel(0); + + printf("Hello world \n"); + return 0; +} + +#endif // mapped_model_MAIN +#endif // mapped_model_TEST