Skip to content

Commit

Permalink
Support for additionalProperties in the C generator "Client: C" solves
Browse files Browse the repository at this point in the history
…#5395 (#5440)

* Support for additionalProperties in the C generator.

* Support for additionalProperties in the C generator.
  • Loading branch information
michelealbano authored Feb 28, 2020
1 parent 50d21cb commit b0b46d5
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ keyValuePair_t *keyValuePair_create(char *key, void *value) {
return keyValuePair;
}

keyValuePair_t* keyValuePair_create_allocate(char* key, double value) {
double* boolpointer = malloc(sizeof(value));
memcpy(boolpointer, &value, sizeof(value));
return keyValuePair_create(key, boolpointer);
}

void keyValuePair_free(keyValuePair_t *keyValuePair) {
free(keyValuePair);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ typedef struct keyValuePair_t {

keyValuePair_t *keyValuePair_create(char *key, void *value);

keyValuePair_t* keyValuePair_create_allocate(char* key, double value);

void keyValuePair_free(keyValuePair_t *keyValuePair);

#endif /* _keyValuePair_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
if({{{name}}} == NULL) {
goto fail; //primitive map container
}
cJSON *localMapObject = cJSON_CreateObject(); //Memory free to be implemented in user code
cJSON *localMapObject = {{{name}}};
listEntry_t *{{{name}}}ListEntry;
if ({{{classname}}}->{{{name}}}) {
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
Expand All @@ -442,7 +442,6 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
}
{{/isString}}
{{/items}}
cJSON_AddItemToObject({{{name}}},"", localMapObject);
}
}
{{/isMapContainer}}
Expand Down Expand Up @@ -643,22 +642,24 @@ fail:
keyValuePair_t *localMapKeyPair;
cJSON_ArrayForEach({{{name}}}_local_map, {{{name}}})
{
cJSON *localMapObject = {{{name}}}_local_map;
{{#items}}
{{#isString}}
if(!cJSON_IsString({{{name}}}_local_map))
if(!cJSON_IsString(localMapObject))
{
goto end;
}
localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),strdup({{{name}}}_local_map->valuestring))
list_addElement({{{name}}}List , localMapKeyPair);
localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),strdup(localMapObject->valuestring));
{{/isString}}
{{^isString}}
if(!cJSON_IsNumber({{{name}}}_local_map))
if(!cJSON_IsNumber(localMapObject))
{
goto end;
}
localMapKeyPair = keyValuePair_create(strdup({{{name}}}_local_map->string),&{{{name}}}_local_map->valuedouble );
list_addElement({{{name}}}List , localMapKeyPair);
localMapKeyPair = keyValuePair_create(strdup(localMapObject->string),&localMapObject->valuedouble );
{{/isString}}
{{/items}}
list_addElement({{{name}}}List , localMapKeyPair);
}
{{/isMapContainer}}
{{/isContainer}}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/c/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.2-SNAPSHOT
4.3.0-SNAPSHOT
38 changes: 28 additions & 10 deletions samples/client/petstore/c/api/UserAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)


// query parameters
char *keyQuery_username;
char * valueQuery_username;
char *keyQuery_username = NULL;
char * valueQuery_username = NULL;
keyValuePair_t *keyPairQuery_username = 0;
if (username)
{
Expand All @@ -398,8 +398,8 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)
}

// query parameters
char *keyQuery_password;
char * valueQuery_password;
char *keyQuery_password = NULL;
char * valueQuery_password = NULL;
keyValuePair_t *keyPairQuery_password = 0;
if (password)
{
Expand Down Expand Up @@ -438,12 +438,30 @@ UserAPI_loginUser(apiClient_t *apiClient ,char * username ,char * password)
list_free(localVarHeaderType);

free(localVarPath);
free(keyQuery_username);
free(valueQuery_username);
keyValuePair_free(keyPairQuery_username);
free(keyQuery_password);
free(valueQuery_password);
keyValuePair_free(keyPairQuery_password);
if(keyQuery_username){
free(keyQuery_username);
keyQuery_username = NULL;
}
if(valueQuery_username){
free(valueQuery_username);
valueQuery_username = NULL;
}
if(keyPairQuery_username){
keyValuePair_free(keyPairQuery_username);
keyPairQuery_username = NULL;
}
if(keyQuery_password){
free(keyQuery_password);
keyQuery_password = NULL;
}
if(valueQuery_password){
free(valueQuery_password);
valueQuery_password = NULL;
}
if(keyPairQuery_password){
keyValuePair_free(keyPairQuery_password);
keyPairQuery_password = NULL;
}
return elementToReturn;
end:
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions samples/client/petstore/c/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

typedef struct apiClient_t {
char *basePath;
char *caPath;
void *dataReceived;
long response_code;
list_t *apiKeys;
Expand All @@ -25,6 +26,11 @@ typedef struct binary_t

apiClient_t* apiClient_create();

apiClient_t* apiClient_create_with_base_path(const char *basePath
, const char *caPath
, list_t *apiKeys
);

void apiClient_free(apiClient_t *apiClient);

void apiClient_invoke(apiClient_t *apiClient,char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters,list_t *headerType,list_t *contentType, char *bodyParameters, char *requestType);
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/c/include/keyValuePair.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ typedef struct keyValuePair_t {

keyValuePair_t *keyValuePair_create(char *key, void *value);

keyValuePair_t* keyValuePair_create_allocate(char* key, double value);

void keyValuePair_free(keyValuePair_t *keyValuePair);

#endif /* _keyValuePair_H_ */
42 changes: 21 additions & 21 deletions samples/client/petstore/c/model/order.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

order_t *order_create(
long id,
long petId,
long pet_id,
int quantity,
char *shipDate,
char *ship_date,
status_e status,
int complete
) {
Expand All @@ -35,9 +35,9 @@ order_t *order_create(
return NULL;
}
order_local_var->id = id;
order_local_var->petId = petId;
order_local_var->pet_id = pet_id;
order_local_var->quantity = quantity;
order_local_var->shipDate = shipDate;
order_local_var->ship_date = ship_date;
order_local_var->status = status;
order_local_var->complete = complete;

Expand All @@ -47,7 +47,7 @@ order_t *order_create(

void order_free(order_t *order) {
listEntry_t *listEntry;
free(order->shipDate);
free(order->ship_date);
free(order);
}

Expand All @@ -62,9 +62,9 @@ cJSON *order_convertToJSON(order_t *order) {
}


// order->petId
if(order->petId) {
if(cJSON_AddNumberToObject(item, "petId", order->petId) == NULL) {
// order->pet_id
if(order->pet_id) {
if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL) {
goto fail; //Numeric
}
}
Expand All @@ -78,9 +78,9 @@ cJSON *order_convertToJSON(order_t *order) {
}


// order->shipDate
if(order->shipDate) {
if(cJSON_AddStringToObject(item, "shipDate", order->shipDate) == NULL) {
// order->ship_date
if(order->ship_date) {
if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL) {
goto fail; //Date-Time
}
}
Expand Down Expand Up @@ -123,10 +123,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
}
}

// order->petId
cJSON *petId = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId");
if (petId) {
if(!cJSON_IsNumber(petId))
// order->pet_id
cJSON *pet_id = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId");
if (pet_id) {
if(!cJSON_IsNumber(pet_id))
{
goto end; //Numeric
}
Expand All @@ -141,10 +141,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
}
}

// order->shipDate
cJSON *shipDate = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate");
if (shipDate) {
if(!cJSON_IsString(shipDate))
// order->ship_date
cJSON *ship_date = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate");
if (ship_date) {
if(!cJSON_IsString(ship_date))
{
goto end; //DateTime
}
Expand Down Expand Up @@ -173,9 +173,9 @@ order_t *order_parseFromJSON(cJSON *orderJSON){

order_local_var = order_create (
id ? id->valuedouble : 0,
petId ? petId->valuedouble : 0,
pet_id ? pet_id->valuedouble : 0,
quantity ? quantity->valuedouble : 0,
shipDate ? strdup(shipDate->valuestring) : NULL,
ship_date ? strdup(ship_date->valuestring) : NULL,
status ? statusVariable : -1,
complete ? complete->valueint : 0
);
Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/c/model/order.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@

typedef struct order_t {
long id; //numeric
long petId; //numeric
long pet_id; //numeric
int quantity; //numeric
char *shipDate; //date time
char *ship_date; //date time
status_e status; //enum
int complete; //boolean

} order_t;

order_t *order_create(
long id,
long petId,
long pet_id,
int quantity,
char *shipDate,
char *ship_date,
status_e status,
int complete
);
Expand Down
24 changes: 12 additions & 12 deletions samples/client/petstore/c/model/pet.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pet_t *pet_create(
long id,
category_t *category,
char *name,
list_t *photoUrls,
list_t *photo_urls,
list_t *tags,
status_e status
) {
Expand All @@ -37,7 +37,7 @@ pet_t *pet_create(
pet_local_var->id = id;
pet_local_var->category = category;
pet_local_var->name = name;
pet_local_var->photoUrls = photoUrls;
pet_local_var->photo_urls = photo_urls;
pet_local_var->tags = tags;
pet_local_var->status = status;

Expand All @@ -49,10 +49,10 @@ void pet_free(pet_t *pet) {
listEntry_t *listEntry;
category_free(pet->category);
free(pet->name);
list_ForEach(listEntry, pet->photoUrls) {
list_ForEach(listEntry, pet->photo_urls) {
free(listEntry->data);
}
list_free(pet->photoUrls);
list_free(pet->photo_urls);
list_ForEach(listEntry, pet->tags) {
tag_free(listEntry->data);
}
Expand Down Expand Up @@ -94,8 +94,8 @@ cJSON *pet_convertToJSON(pet_t *pet) {
}


// pet->photoUrls
if (!pet->photoUrls) {
// pet->photo_urls
if (!pet->photo_urls) {
goto fail;
}

Expand All @@ -105,7 +105,7 @@ cJSON *pet_convertToJSON(pet_t *pet) {
}

listEntry_t *photo_urlsListEntry;
list_ForEach(photo_urlsListEntry, pet->photoUrls) {
list_ForEach(photo_urlsListEntry, pet->photo_urls) {
if(cJSON_AddStringToObject(photo_urls, "", (char*)photo_urlsListEntry->data) == NULL)
{
goto fail;
Expand Down Expand Up @@ -181,21 +181,21 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
goto end; //String
}

// pet->photoUrls
cJSON *photoUrls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls");
if (!photoUrls) {
// pet->photo_urls
cJSON *photo_urls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls");
if (!photo_urls) {
goto end;
}

list_t *photo_urlsList;

cJSON *photo_urls_local;
if(!cJSON_IsArray(photoUrls)) {
if(!cJSON_IsArray(photo_urls)) {
goto end;//primitive container
}
photo_urlsList = list_create();

cJSON_ArrayForEach(photo_urls_local, photoUrls)
cJSON_ArrayForEach(photo_urls_local, photo_urls)
{
if(!cJSON_IsString(photo_urls_local))
{
Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore/c/model/pet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

typedef struct pet_t {
long id; //numeric
category_t *category; //model
struct category_t *category; //model
char *name; // string
list_t *photoUrls; //primitive container
list_t *photo_urls; //primitive container
list_t *tags; //nonprimitive container
status_e status; //enum

Expand All @@ -35,7 +35,7 @@ pet_t *pet_create(
long id,
category_t *category,
char *name,
list_t *photoUrls,
list_t *photo_urls,
list_t *tags,
status_e status
);
Expand Down
Loading

0 comments on commit b0b46d5

Please sign in to comment.