Skip to content

Commit

Permalink
map set to list, add tests (OpenAPITools#16730)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored and AlanCitrix committed Oct 26, 2023
1 parent 93e3320 commit 7e32b44
Show file tree
Hide file tree
Showing 8 changed files with 300 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
14 changes: 14 additions & 0 deletions modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions samples/client/petstore/c/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions samples/client/petstore/c/docs/model_with_set_propertes.md
Original file line number Diff line number Diff line change
@@ -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)


170 changes: 170 additions & 0 deletions samples/client/petstore/c/model/model_with_set_propertes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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;

}
40 changes: 40 additions & 0 deletions samples/client/petstore/c/model/model_with_set_propertes.h
Original file line number Diff line number Diff line change
@@ -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 <string.h>
#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_ */

Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#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

0 comments on commit 7e32b44

Please sign in to comment.