add mapping features to c client generator (#16371)

This commit is contained in:
William Cheng 2023-08-22 14:58:41 +08:00 committed by GitHub
parent 32fe92fee9
commit 9c9c45a73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 203 additions and 0 deletions

View File

@ -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

View File

@ -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)) {

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,69 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;
}

View File

@ -0,0 +1,37 @@
/*
* MappedModel.h
*
* to test mapping features
*/
#ifndef _MappedModel_H_
#define _MappedModel_H_
#include <string.h>
#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_ */

View File

@ -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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#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