Support for additionalProperties in the C generator "Client: C" solves #5395 (#5440)

* Support for additionalProperties in the C generator.

* Support for additionalProperties in the C generator.
This commit is contained in:
Michele Albano 2020-02-28 02:22:50 +01:00 committed by GitHub
parent 50d21cb0d1
commit b0b46d57e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 205 additions and 99 deletions

View File

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

View File

@ -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_ */

View File

@ -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}}}) {
@ -442,7 +442,6 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
}
{{/isString}}
{{/items}}
cJSON_AddItemToObject({{{name}}},"", localMapObject);
}
}
{{/isMapContainer}}
@ -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}}

View File

@ -1 +1 @@
4.2.2-SNAPSHOT
4.3.0-SNAPSHOT

View File

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

View File

@ -11,6 +11,7 @@
typedef struct apiClient_t {
char *basePath;
char *caPath;
void *dataReceived;
long response_code;
list_t *apiKeys;
@ -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);

View File

@ -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_ */

View File

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

View File

@ -21,9 +21,9 @@
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
@ -31,9 +31,9 @@ typedef struct 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
);

View File

@ -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
) {
@ -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;
@ -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);
}
@ -94,8 +94,8 @@ cJSON *pet_convertToJSON(pet_t *pet) {
}
// pet->photoUrls
if (!pet->photoUrls) {
// pet->photo_urls
if (!pet->photo_urls) {
goto fail;
}
@ -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;
@ -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))
{

View File

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

View File

@ -8,12 +8,12 @@
user_t *user_create(
long id,
char *username,
char *firstName,
char *lastName,
char *first_name,
char *last_name,
char *email,
char *password,
char *phone,
int userStatus
int user_status
) {
user_t *user_local_var = malloc(sizeof(user_t));
if (!user_local_var) {
@ -21,12 +21,12 @@ user_t *user_create(
}
user_local_var->id = id;
user_local_var->username = username;
user_local_var->firstName = firstName;
user_local_var->lastName = lastName;
user_local_var->first_name = first_name;
user_local_var->last_name = last_name;
user_local_var->email = email;
user_local_var->password = password;
user_local_var->phone = phone;
user_local_var->userStatus = userStatus;
user_local_var->user_status = user_status;
return user_local_var;
}
@ -35,8 +35,8 @@ user_t *user_create(
void user_free(user_t *user) {
listEntry_t *listEntry;
free(user->username);
free(user->firstName);
free(user->lastName);
free(user->first_name);
free(user->last_name);
free(user->email);
free(user->password);
free(user->phone);
@ -62,17 +62,17 @@ cJSON *user_convertToJSON(user_t *user) {
}
// user->firstName
if(user->firstName) {
if(cJSON_AddStringToObject(item, "firstName", user->firstName) == NULL) {
// user->first_name
if(user->first_name) {
if(cJSON_AddStringToObject(item, "firstName", user->first_name) == NULL) {
goto fail; //String
}
}
// user->lastName
if(user->lastName) {
if(cJSON_AddStringToObject(item, "lastName", user->lastName) == NULL) {
// user->last_name
if(user->last_name) {
if(cJSON_AddStringToObject(item, "lastName", user->last_name) == NULL) {
goto fail; //String
}
}
@ -102,9 +102,9 @@ cJSON *user_convertToJSON(user_t *user) {
}
// user->userStatus
if(user->userStatus) {
if(cJSON_AddNumberToObject(item, "userStatus", user->userStatus) == NULL) {
// user->user_status
if(user->user_status) {
if(cJSON_AddNumberToObject(item, "userStatus", user->user_status) == NULL) {
goto fail; //Numeric
}
}
@ -139,19 +139,19 @@ user_t *user_parseFromJSON(cJSON *userJSON){
}
}
// user->firstName
cJSON *firstName = cJSON_GetObjectItemCaseSensitive(userJSON, "firstName");
if (firstName) {
if(!cJSON_IsString(firstName))
// user->first_name
cJSON *first_name = cJSON_GetObjectItemCaseSensitive(userJSON, "firstName");
if (first_name) {
if(!cJSON_IsString(first_name))
{
goto end; //String
}
}
// user->lastName
cJSON *lastName = cJSON_GetObjectItemCaseSensitive(userJSON, "lastName");
if (lastName) {
if(!cJSON_IsString(lastName))
// user->last_name
cJSON *last_name = cJSON_GetObjectItemCaseSensitive(userJSON, "lastName");
if (last_name) {
if(!cJSON_IsString(last_name))
{
goto end; //String
}
@ -184,10 +184,10 @@ user_t *user_parseFromJSON(cJSON *userJSON){
}
}
// user->userStatus
cJSON *userStatus = cJSON_GetObjectItemCaseSensitive(userJSON, "userStatus");
if (userStatus) {
if(!cJSON_IsNumber(userStatus))
// user->user_status
cJSON *user_status = cJSON_GetObjectItemCaseSensitive(userJSON, "userStatus");
if (user_status) {
if(!cJSON_IsNumber(user_status))
{
goto end; //Numeric
}
@ -197,12 +197,12 @@ user_t *user_parseFromJSON(cJSON *userJSON){
user_local_var = user_create (
id ? id->valuedouble : 0,
username ? strdup(username->valuestring) : NULL,
firstName ? strdup(firstName->valuestring) : NULL,
lastName ? strdup(lastName->valuestring) : NULL,
first_name ? strdup(first_name->valuestring) : NULL,
last_name ? strdup(last_name->valuestring) : NULL,
email ? strdup(email->valuestring) : NULL,
password ? strdup(password->valuestring) : NULL,
phone ? strdup(phone->valuestring) : NULL,
userStatus ? userStatus->valuedouble : 0
user_status ? user_status->valuedouble : 0
);
return user_local_var;

View File

@ -17,24 +17,24 @@
typedef struct user_t {
long id; //numeric
char *username; // string
char *firstName; // string
char *lastName; // string
char *first_name; // string
char *last_name; // string
char *email; // string
char *password; // string
char *phone; // string
int userStatus; //numeric
int user_status; //numeric
} user_t;
user_t *user_create(
long id,
char *username,
char *firstName,
char *lastName,
char *first_name,
char *last_name,
char *email,
char *password,
char *phone,
int userStatus
int user_status
);
void user_free(user_t *user);

View File

@ -12,7 +12,8 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);
apiClient_t *apiClient_create() {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
apiClient->basePath = "http://petstore.swagger.io/v2";
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
apiClient->caPath = NULL;
apiClient->dataReceived = NULL;
apiClient->response_code = 0;
apiClient->apiKeys = NULL;
@ -21,8 +22,61 @@ apiClient_t *apiClient_create() {
return apiClient;
}
apiClient_t *apiClient_create_with_base_path(const char *basePath
, const char *caPath
, list_t *apiKeys
) {
curl_global_init(CURL_GLOBAL_ALL);
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
if(basePath){
apiClient->basePath = strdup(basePath);
}else{
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
}
if(caPath){
apiClient->caPath = strdup(caPath);
}else{
apiClient->caPath = NULL;
}
apiClient->dataReceived = NULL;
apiClient->response_code = 0;
if(apiKeys!= NULL) {
apiClient->apiKeys = list_create();
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, apiKeys) {
keyValuePair_t *pair = listEntry->data;
keyValuePair_t *pairDup = keyValuePair_create(strdup(pair->key), strdup(pair->value));
list_addElement(apiClient->apiKeys, pairDup);
}
}else{
apiClient->apiKeys = NULL;
}
apiClient->accessToken = NULL;
return apiClient;
}
void apiClient_free(apiClient_t *apiClient) {
if(apiClient->accessToken) {
if(apiClient->basePath) {
free(apiClient->basePath);
}
if(apiClient->caPath) {
free(apiClient->caPath);
}
if(apiClient->apiKeys) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, apiClient->apiKeys) {
keyValuePair_t *pair = listEntry->data;
if(pair->key){
free(pair->key);
}
if(pair->value){
free(pair->value);
}
keyValuePair_free(pair);
}
list_free(apiClient->apiKeys);
}
if(apiClient->accessToken) {
@ -287,6 +341,17 @@ void apiClient_invoke(apiClient_t *apiClient,
free(headerValueToWrite);
}
}
if( strstr(apiClient->basePath, "https") != NULL ){
if (apiClient->caPath) {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath);
} else {
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
}
}
// this would only be generated for apiKey authentication
if (apiClient->apiKeys != NULL)
{

View File

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