Ernesto Fernández f4271faf3b
Fix a few issues with the C generator (part 3) (#20269)
* [C] Clear the response code from previous api calls

Before making an api call, reset apiClient->response_code to zero. That
will protect us from checking stale values if the curl request fails.

* [C] Check that string arguments are not null

Check early on that the arguments are not null, to prevent crashes on
strdup() calls.

* [C] Don't attempt to fill in a type with error info

Check if the api call returned an error before attempting to parse the
reply as the expected type.

* [C] Handle binary and integer return types

* [C] Update test schemas with binary and integer return types

* Update samples
2024-12-10 12:23:29 +08:00

339 lines
9.9 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "StoreAPI.h"
#define MAX_NUMBER_LENGTH 16
#define MAX_BUFFER_LENGTH 4096
#define intToStr(dst, src) \
do {\
char dst[256];\
snprintf(dst, 256, "%ld", (long int)(src));\
}while(0)
// Delete purchase order by ID
//
// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
//
void
StoreAPI_deleteOrder(apiClient_t *apiClient, char *orderId)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = NULL;
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/store/order/{orderId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order/{orderId}");
if(!orderId)
goto end;
// Path Params
long sizeOfPathParams_orderId = strlen(orderId)+3 + strlen("{ orderId }");
if(orderId == NULL) {
goto end;
}
char* localVarToReplace_orderId = malloc(sizeOfPathParams_orderId);
sprintf(localVarToReplace_orderId, "{%s}", "orderId");
localVarPath = strReplace(localVarPath, localVarToReplace_orderId, orderId);
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"DELETE");
// uncomment below to debug the error response
//if (apiClient->response_code == 400) {
// printf("%s\n","Invalid ID supplied");
//}
// uncomment below to debug the error response
//if (apiClient->response_code == 404) {
// printf("%s\n","Order not found");
//}
//No return type
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
free(localVarPath);
free(localVarToReplace_orderId);
}
// Returns pet inventories by status
//
// Returns a map of status codes to quantities
//
list_t*
StoreAPI_getInventory(apiClient_t *apiClient)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/store/inventory")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/inventory");
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"GET");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","successful operation");
//}
//primitive return type not simple
list_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *localVarJSON = cJSON_Parse(apiClient->dataReceived);
cJSON *VarJSON;
elementToReturn = list_createList();
cJSON_ArrayForEach(VarJSON, localVarJSON){
keyValuePair_t *keyPair = keyValuePair_create(strdup(VarJSON->string), cJSON_Print(VarJSON));
list_addElement(elementToReturn, keyPair);
}
cJSON_Delete(localVarJSON);
}
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Find purchase order by ID
//
// For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions
//
order_t*
StoreAPI_getOrderById(apiClient_t *apiClient, long orderId)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/store/order/{orderId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order/{orderId}");
// Path Params
long sizeOfPathParams_orderId = sizeof(orderId)+3 + strlen("{ orderId }");
if(orderId == 0){
goto end;
}
char* localVarToReplace_orderId = malloc(sizeOfPathParams_orderId);
snprintf(localVarToReplace_orderId, sizeOfPathParams_orderId, "{%s}", "orderId");
char localVarBuff_orderId[256];
intToStr(localVarBuff_orderId, orderId);
localVarPath = strReplace(localVarPath, localVarToReplace_orderId, localVarBuff_orderId);
list_addElement(localVarHeaderType,"application/xml"); //produces
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"GET");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","successful operation");
//}
// uncomment below to debug the error response
//if (apiClient->response_code == 400) {
// printf("%s\n","Invalid ID supplied");
//}
// uncomment below to debug the error response
//if (apiClient->response_code == 404) {
// printf("%s\n","Order not found");
//}
//nonprimitive not container
order_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON);
cJSON_Delete(StoreAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
free(localVarToReplace_orderId);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Place an order for a pet
//
order_t*
StoreAPI_placeOrder(apiClient_t *apiClient, order_t *body)
{
list_t *localVarQueryParameters = NULL;
list_t *localVarHeaderParameters = NULL;
list_t *localVarFormParameters = NULL;
list_t *localVarHeaderType = list_createList();
list_t *localVarContentType = NULL;
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/store/order")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/order");
// Body Param
cJSON *localVarSingleItemJSON_body = NULL;
if (body != NULL)
{
//string
localVarSingleItemJSON_body = order_convertToJSON(body);
localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_body);
}
list_addElement(localVarHeaderType,"application/xml"); //produces
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
localVarPath,
localVarQueryParameters,
localVarHeaderParameters,
localVarFormParameters,
localVarHeaderType,
localVarContentType,
localVarBodyParameters,
"POST");
// uncomment below to debug the error response
//if (apiClient->response_code == 200) {
// printf("%s\n","successful operation");
//}
// uncomment below to debug the error response
//if (apiClient->response_code == 400) {
// printf("%s\n","Invalid Order");
//}
//nonprimitive not container
order_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *StoreAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = order_parseFromJSON(StoreAPIlocalVarJSON);
cJSON_Delete(StoreAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
if (localVarSingleItemJSON_body) {
cJSON_Delete(localVarSingleItemJSON_body);
localVarSingleItemJSON_body = NULL;
}
free(localVarBodyParameters);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}