From 893154d7669adbd577aa6d97e2ba6f0a6787d4b7 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 7 Oct 2023 21:56:02 +0800 Subject: [PATCH] map set to list, add tests (#16730) --- .../languages/CLibcurlClientCodegen.java | 1 + .../src/test/resources/2_0/c/petstore.yaml | 14 ++ .../petstore/c/.openapi-generator/FILES | 3 + samples/client/petstore/c/README.md | 1 + .../c/docs/model_with_set_propertes.md | 11 ++ .../c/model/model_with_set_propertes.c | 170 ++++++++++++++++++ .../c/model/model_with_set_propertes.h | 40 +++++ .../unit-test/test_model_with_set_propertes.c | 60 +++++++ 8 files changed, 300 insertions(+) create mode 100644 samples/client/petstore/c/docs/model_with_set_propertes.md create mode 100644 samples/client/petstore/c/model/model_with_set_propertes.c create mode 100644 samples/client/petstore/c/model/model_with_set_propertes.h create mode 100644 samples/client/petstore/c/unit-test/test_model_with_set_propertes.c 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 292af33947fa..3b13f6ba19cd 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 @@ -289,6 +289,7 @@ public CLibcurlClientCodegen() { typeMapping.put("UUID", "char"); typeMapping.put("URI", "char"); typeMapping.put("array", "list"); + typeMapping.put("set", "list"); typeMapping.put("map", "list_t*"); typeMapping.put("date-time", "char"); 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 0a8f3fda813e..c5125f976e74 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 @@ -726,3 +726,17 @@ definitions: type: string format: uuid default: 1111-2222-3333-4444 + model_with_set_propertes: + description: to test set properties + type: object + properties: + tag_set: + type: array + uniqueItems: true + items: + $ref: '#/definitions/Tag' + string_set: + type: array + uniqueItems: true + items: + type: string diff --git a/samples/client/petstore/c/.openapi-generator/FILES b/samples/client/petstore/c/.openapi-generator/FILES index 1e7a61f50807..974bd47125f3 100644 --- a/samples/client/petstore/c/.openapi-generator/FILES +++ b/samples/client/petstore/c/.openapi-generator/FILES @@ -12,6 +12,7 @@ docs/StoreAPI.md docs/UserAPI.md docs/api_response.md docs/category.md +docs/model_with_set_propertes.md docs/order.md docs/pet.md docs/tag.md @@ -30,6 +31,8 @@ model/category.c model/category.h model/mapped_model.c model/mapped_model.h +model/model_with_set_propertes.c +model/model_with_set_propertes.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 ea388a72b66f..ca4556982a45 100644 --- a/samples/client/petstore/c/README.md +++ b/samples/client/petstore/c/README.md @@ -92,6 +92,7 @@ Category | Method | HTTP request | Description - [MappedModel_t](docs/MappedModel.md) - [api_response_t](docs/api_response.md) - [category_t](docs/category.md) + - [model_with_set_propertes_t](docs/model_with_set_propertes.md) - [order_t](docs/order.md) - [pet_t](docs/pet.md) - [tag_t](docs/tag.md) diff --git a/samples/client/petstore/c/docs/model_with_set_propertes.md b/samples/client/petstore/c/docs/model_with_set_propertes.md new file mode 100644 index 000000000000..2b8389e87f86 --- /dev/null +++ b/samples/client/petstore/c/docs/model_with_set_propertes.md @@ -0,0 +1,11 @@ +# model_with_set_propertes_t + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**tag_set** | [**list_t**](tag.md) \* | | [optional] +**string_set** | **list_t \*** | | [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/model_with_set_propertes.c b/samples/client/petstore/c/model/model_with_set_propertes.c new file mode 100644 index 000000000000..45b497263646 --- /dev/null +++ b/samples/client/petstore/c/model/model_with_set_propertes.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include "model_with_set_propertes.h" + + + +model_with_set_propertes_t *model_with_set_propertes_create( + list_t *tag_set, + list_t *string_set + ) { + model_with_set_propertes_t *model_with_set_propertes_local_var = malloc(sizeof(model_with_set_propertes_t)); + if (!model_with_set_propertes_local_var) { + return NULL; + } + model_with_set_propertes_local_var->tag_set = tag_set; + model_with_set_propertes_local_var->string_set = string_set; + + return model_with_set_propertes_local_var; +} + + +void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) { + if(NULL == model_with_set_propertes){ + return ; + } + listEntry_t *listEntry; + if (model_with_set_propertes->tag_set) { + list_ForEach(listEntry, model_with_set_propertes->tag_set) { + tag_free(listEntry->data); + } + list_freeList(model_with_set_propertes->tag_set); + model_with_set_propertes->tag_set = NULL; + } + if (model_with_set_propertes->string_set) { + list_ForEach(listEntry, model_with_set_propertes->string_set) { + free(listEntry->data); + } + list_freeList(model_with_set_propertes->string_set); + model_with_set_propertes->string_set = NULL; + } + free(model_with_set_propertes); +} + +cJSON *model_with_set_propertes_convertToJSON(model_with_set_propertes_t *model_with_set_propertes) { + cJSON *item = cJSON_CreateObject(); + + // model_with_set_propertes->tag_set + if(model_with_set_propertes->tag_set) { + cJSON *tag_set = cJSON_AddArrayToObject(item, "tag_set"); + if(tag_set == NULL) { + goto fail; //nonprimitive container + } + + listEntry_t *tag_setListEntry; + if (model_with_set_propertes->tag_set) { + list_ForEach(tag_setListEntry, model_with_set_propertes->tag_set) { + cJSON *itemLocal = tag_convertToJSON(tag_setListEntry->data); + if(itemLocal == NULL) { + goto fail; + } + cJSON_AddItemToArray(tag_set, itemLocal); + } + } + } + + + // model_with_set_propertes->string_set + if(model_with_set_propertes->string_set) { + cJSON *string_set = cJSON_AddArrayToObject(item, "string_set"); + if(string_set == NULL) { + goto fail; //primitive container + } + + listEntry_t *string_setListEntry; + list_ForEach(string_setListEntry, model_with_set_propertes->string_set) { + if(cJSON_AddStringToObject(string_set, "", (char*)string_setListEntry->data) == NULL) + { + goto fail; + } + } + } + + return item; +fail: + if (item) { + cJSON_Delete(item); + } + return NULL; +} + +model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_with_set_propertesJSON){ + + model_with_set_propertes_t *model_with_set_propertes_local_var = NULL; + + // define the local list for model_with_set_propertes->tag_set + list_t *tag_setList = NULL; + + // define the local list for model_with_set_propertes->string_set + list_t *string_setList = NULL; + + // model_with_set_propertes->tag_set + cJSON *tag_set = cJSON_GetObjectItemCaseSensitive(model_with_set_propertesJSON, "tag_set"); + if (tag_set) { + cJSON *tag_set_local_nonprimitive = NULL; + if(!cJSON_IsArray(tag_set)){ + goto end; //nonprimitive container + } + + tag_setList = list_createList(); + + cJSON_ArrayForEach(tag_set_local_nonprimitive,tag_set ) + { + if(!cJSON_IsObject(tag_set_local_nonprimitive)){ + goto end; + } + tag_t *tag_setItem = tag_parseFromJSON(tag_set_local_nonprimitive); + + list_addElement(tag_setList, tag_setItem); + } + } + + // model_with_set_propertes->string_set + cJSON *string_set = cJSON_GetObjectItemCaseSensitive(model_with_set_propertesJSON, "string_set"); + if (string_set) { + cJSON *string_set_local = NULL; + if(!cJSON_IsArray(string_set)) { + goto end;//primitive container + } + string_setList = list_createList(); + + cJSON_ArrayForEach(string_set_local, string_set) + { + if(!cJSON_IsString(string_set_local)) + { + goto end; + } + list_addElement(string_setList , strdup(string_set_local->valuestring)); + } + } + + + model_with_set_propertes_local_var = model_with_set_propertes_create ( + tag_set ? tag_setList : NULL, + string_set ? string_setList : NULL + ); + + return model_with_set_propertes_local_var; +end: + if (tag_setList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, tag_setList) { + tag_free(listEntry->data); + listEntry->data = NULL; + } + list_freeList(tag_setList); + tag_setList = NULL; + } + if (string_setList) { + listEntry_t *listEntry = NULL; + list_ForEach(listEntry, string_setList) { + free(listEntry->data); + listEntry->data = NULL; + } + list_freeList(string_setList); + string_setList = NULL; + } + return NULL; + +} diff --git a/samples/client/petstore/c/model/model_with_set_propertes.h b/samples/client/petstore/c/model/model_with_set_propertes.h new file mode 100644 index 000000000000..537271638b99 --- /dev/null +++ b/samples/client/petstore/c/model/model_with_set_propertes.h @@ -0,0 +1,40 @@ +/* + * model_with_set_propertes.h + * + * to test set properties + */ + +#ifndef _model_with_set_propertes_H_ +#define _model_with_set_propertes_H_ + +#include +#include "../external/cJSON.h" +#include "../include/list.h" +#include "../include/keyValuePair.h" +#include "../include/binary.h" + +typedef struct model_with_set_propertes_t model_with_set_propertes_t; + +#include "tag.h" + + + +typedef struct model_with_set_propertes_t { + list_t *tag_set; //nonprimitive container + list_t *string_set; //primitive container + +} model_with_set_propertes_t; + +model_with_set_propertes_t *model_with_set_propertes_create( + list_t *tag_set, + list_t *string_set +); + +void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes); + +model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_with_set_propertesJSON); + +cJSON *model_with_set_propertes_convertToJSON(model_with_set_propertes_t *model_with_set_propertes); + +#endif /* _model_with_set_propertes_H_ */ + diff --git a/samples/client/petstore/c/unit-test/test_model_with_set_propertes.c b/samples/client/petstore/c/unit-test/test_model_with_set_propertes.c new file mode 100644 index 000000000000..3c62088bc736 --- /dev/null +++ b/samples/client/petstore/c/unit-test/test_model_with_set_propertes.c @@ -0,0 +1,60 @@ +#ifndef model_with_set_propertes_TEST +#define model_with_set_propertes_TEST + +// the following is to include only the main from the first c file +#ifndef TEST_MAIN +#define TEST_MAIN +#define model_with_set_propertes_MAIN +#endif // TEST_MAIN + +#include +#include +#include +#include +#include "../external/cJSON.h" + +#include "../model/model_with_set_propertes.h" +model_with_set_propertes_t* instantiate_model_with_set_propertes(int include_optional); + + + +model_with_set_propertes_t* instantiate_model_with_set_propertes(int include_optional) { + model_with_set_propertes_t* model_with_set_propertes = NULL; + if (include_optional) { + model_with_set_propertes = model_with_set_propertes_create( + list_createList(), + list_createList() + ); + } else { + model_with_set_propertes = model_with_set_propertes_create( + list_createList(), + list_createList() + ); + } + + return model_with_set_propertes; +} + + +#ifdef model_with_set_propertes_MAIN + +void test_model_with_set_propertes(int include_optional) { + model_with_set_propertes_t* model_with_set_propertes_1 = instantiate_model_with_set_propertes(include_optional); + + cJSON* jsonmodel_with_set_propertes_1 = model_with_set_propertes_convertToJSON(model_with_set_propertes_1); + printf("model_with_set_propertes :\n%s\n", cJSON_Print(jsonmodel_with_set_propertes_1)); + model_with_set_propertes_t* model_with_set_propertes_2 = model_with_set_propertes_parseFromJSON(jsonmodel_with_set_propertes_1); + cJSON* jsonmodel_with_set_propertes_2 = model_with_set_propertes_convertToJSON(model_with_set_propertes_2); + printf("repeating model_with_set_propertes:\n%s\n", cJSON_Print(jsonmodel_with_set_propertes_2)); +} + +int main() { + test_model_with_set_propertes(1); + test_model_with_set_propertes(0); + + printf("Hello world \n"); + return 0; +} + +#endif // model_with_set_propertes_MAIN +#endif // model_with_set_propertes_TEST