Skip to content

Commit 9c9c45a

Browse files
authored
add mapping features to c client generator (#16371)
1 parent 32fe92f commit 9c9c45a

9 files changed

Lines changed: 203 additions & 0 deletions

File tree

bin/configs/c.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ generatorName: c
22
outputDir: samples/client/petstore/c
33
inputSpec: modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml
44
templateDir: modules/openapi-generator/src/main/resources/C-libcurl
5+
modelNameMappings:
6+
another_model: MappedModel
7+
another_property: mappedProperty

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,11 @@ public String getSchemaType(Schema schema) {
559559

560560
@Override
561561
public String toVarName(String name) {
562+
// obtain the name from nameMapping directly if provided
563+
if (nameMapping.containsKey(name)) {
564+
return nameMapping.get(name);
565+
}
566+
562567
// sanitize name
563568
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
564569
// if it's all upper case, convert to lower case
@@ -578,6 +583,11 @@ public String toVarName(String name) {
578583

579584
@Override
580585
public String toParamName(String name) {
586+
// obtain the name from parameterNameMapping directly if provided
587+
if (parameterNameMapping.containsKey(name)) {
588+
return parameterNameMapping.get(name);
589+
}
590+
581591
// should be the same as variable name
582592
if (isReservedWord(name) || name.matches("^\\d.*")) {
583593
name = escapeReservedWord(name);
@@ -588,6 +598,11 @@ public String toParamName(String name) {
588598

589599
@Override
590600
public String toModelName(String name) {
601+
// obtain the name from modelNameMapping directly if provided
602+
if (modelNameMapping.containsKey(name)) {
603+
return modelNameMapping.get(name);
604+
}
605+
591606
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
592607

593608
if (!StringUtils.isEmpty(modelNamePrefix)) {

modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,3 +715,10 @@ definitions:
715715
type: string
716716
message:
717717
type: string
718+
another_model:
719+
description: to test mapping features
720+
type: object
721+
properties:
722+
another_property:
723+
type: integer
724+
format: int32

samples/client/petstore/c/.openapi-generator/FILES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ api/StoreAPI.c
66
api/StoreAPI.h
77
api/UserAPI.c
88
api/UserAPI.h
9+
docs/MappedModel.md
910
docs/PetAPI.md
1011
docs/StoreAPI.md
1112
docs/UserAPI.md
@@ -27,6 +28,8 @@ model/api_response.c
2728
model/api_response.h
2829
model/category.c
2930
model/category.h
31+
model/mapped_model.c
32+
model/mapped_model.h
3033
model/object.c
3134
model/object.h
3235
model/order.c

samples/client/petstore/c/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Category | Method | HTTP request | Description
8989

9090
## Documentation for Models
9191

92+
- [MappedModel_t](docs/MappedModel.md)
9293
- [api_response_t](docs/api_response.md)
9394
- [category_t](docs/category.md)
9495
- [order_t](docs/order.md)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# MappedModel_t
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
**another_property** | **int** | | [optional]
7+
8+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
9+
10+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include "MappedModel.h"
5+
6+
7+
8+
MappedModel_t *MappedModel_create(
9+
int another_property
10+
) {
11+
MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t));
12+
if (!MappedModel_local_var) {
13+
return NULL;
14+
}
15+
MappedModel_local_var->another_property = another_property;
16+
17+
return MappedModel_local_var;
18+
}
19+
20+
21+
void MappedModel_free(MappedModel_t *MappedModel) {
22+
if(NULL == MappedModel){
23+
return ;
24+
}
25+
listEntry_t *listEntry;
26+
free(MappedModel);
27+
}
28+
29+
cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) {
30+
cJSON *item = cJSON_CreateObject();
31+
32+
// MappedModel->another_property
33+
if(MappedModel->another_property) {
34+
if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) {
35+
goto fail; //Numeric
36+
}
37+
}
38+
39+
return item;
40+
fail:
41+
if (item) {
42+
cJSON_Delete(item);
43+
}
44+
return NULL;
45+
}
46+
47+
MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
48+
49+
MappedModel_t *MappedModel_local_var = NULL;
50+
51+
// MappedModel->another_property
52+
cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property");
53+
if (another_property) {
54+
if(!cJSON_IsNumber(another_property))
55+
{
56+
goto end; //Numeric
57+
}
58+
}
59+
60+
61+
MappedModel_local_var = MappedModel_create (
62+
another_property ? another_property->valuedouble : 0
63+
);
64+
65+
return MappedModel_local_var;
66+
end:
67+
return NULL;
68+
69+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* MappedModel.h
3+
*
4+
* to test mapping features
5+
*/
6+
7+
#ifndef _MappedModel_H_
8+
#define _MappedModel_H_
9+
10+
#include <string.h>
11+
#include "../external/cJSON.h"
12+
#include "../include/list.h"
13+
#include "../include/keyValuePair.h"
14+
#include "../include/binary.h"
15+
16+
typedef struct MappedModel_t MappedModel_t;
17+
18+
19+
20+
21+
typedef struct MappedModel_t {
22+
int another_property; //numeric
23+
24+
} MappedModel_t;
25+
26+
MappedModel_t *MappedModel_create(
27+
int another_property
28+
);
29+
30+
void MappedModel_free(MappedModel_t *MappedModel);
31+
32+
MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON);
33+
34+
cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel);
35+
36+
#endif /* _MappedModel_H_ */
37+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef mapped_model_TEST
2+
#define mapped_model_TEST
3+
4+
// the following is to include only the main from the first c file
5+
#ifndef TEST_MAIN
6+
#define TEST_MAIN
7+
#define mapped_model_MAIN
8+
#endif // TEST_MAIN
9+
10+
#include <stdlib.h>
11+
#include <string.h>
12+
#include <stdio.h>
13+
#include <stdbool.h>
14+
#include "../external/cJSON.h"
15+
16+
#include "../model/mapped_model.h"
17+
MappedModel_t* instantiate_MappedModel(int include_optional);
18+
19+
20+
21+
MappedModel_t* instantiate_MappedModel(int include_optional) {
22+
MappedModel_t* MappedModel = NULL;
23+
if (include_optional) {
24+
MappedModel = MappedModel_create(
25+
56
26+
);
27+
} else {
28+
MappedModel = MappedModel_create(
29+
56
30+
);
31+
}
32+
33+
return MappedModel;
34+
}
35+
36+
37+
#ifdef mapped_model_MAIN
38+
39+
void test_MappedModel(int include_optional) {
40+
MappedModel_t* MappedModel_1 = instantiate_MappedModel(include_optional);
41+
42+
cJSON* jsonMappedModel_1 = MappedModel_convertToJSON(MappedModel_1);
43+
printf("MappedModel :\n%s\n", cJSON_Print(jsonMappedModel_1));
44+
MappedModel_t* MappedModel_2 = MappedModel_parseFromJSON(jsonMappedModel_1);
45+
cJSON* jsonMappedModel_2 = MappedModel_convertToJSON(MappedModel_2);
46+
printf("repeating MappedModel:\n%s\n", cJSON_Print(jsonMappedModel_2));
47+
}
48+
49+
int main() {
50+
test_MappedModel(1);
51+
test_MappedModel(0);
52+
53+
printf("Hello world \n");
54+
return 0;
55+
}
56+
57+
#endif // mapped_model_MAIN
58+
#endif // mapped_model_TEST

0 commit comments

Comments
 (0)