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

226 lines
6.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "DefaultAPI.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)
// Returns private information.
//
// This endpoint requires global security settings.
//
object_t*
DefaultAPI_privateGet(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("/private")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/private");
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","A JSON object with private information");
//}
//nonprimitive not container
object_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *DefaultAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = object_parseFromJSON(DefaultAPIlocalVarJSON);
cJSON_Delete(DefaultAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Returns public information.
//
// This endpoint does not require authentication.
//
object_t*
DefaultAPI_publicGet(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("/public")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/public");
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","A JSON object with public information");
//}
//nonprimitive not container
object_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *DefaultAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = object_parseFromJSON(DefaultAPIlocalVarJSON);
cJSON_Delete(DefaultAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Returns a list of users.
//
// Optional extended description in CommonMark or HTML.
//
list_t*
DefaultAPI_usersGet(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("/users")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/users");
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","A JSON array of user names");
//}
//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;
}