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
This commit is contained in:
Ernesto Fernández
2024-12-10 01:23:29 -03:00
committed by GitHub
parent 0a7e784c8c
commit f4271faf3b
16 changed files with 960 additions and 206 deletions

View File

@@ -67,6 +67,9 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t *body)
list_t *localVarContentType = list_createList();
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/pet")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -75,6 +78,7 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t *body)
// Body Param
cJSON *localVarSingleItemJSON_body = NULL;
if (body != NULL)
@@ -132,12 +136,16 @@ PetAPI_deletePet(apiClient_t *apiClient, long petId, char *api_key)
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("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
if(petId == 0){
@@ -219,6 +227,9 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status)
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("/pet/findByStatus")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -227,6 +238,7 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status)
// query parameters
if (status)
{
@@ -252,24 +264,27 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t *status)
//if (apiClient->response_code == 400) {
// printf("%s\n","Invalid status value");
//}
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
if(!cJSON_IsArray(PetAPIlocalVarJSON)) {
return 0;//nonprimitive container
}
list_t *elementToReturn = list_createList();
cJSON *VarJSON;
cJSON_ArrayForEach(VarJSON, PetAPIlocalVarJSON)
{
if(!cJSON_IsObject(VarJSON))
{
// return 0;
list_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
if(!cJSON_IsArray(PetAPIlocalVarJSON)) {
return 0;//nonprimitive container
}
elementToReturn = list_createList();
cJSON *VarJSON;
cJSON_ArrayForEach(VarJSON, PetAPIlocalVarJSON)
{
if(!cJSON_IsObject(VarJSON))
{
// return 0;
}
char *localVarJSONToChar = cJSON_Print(VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}
char *localVarJSONToChar = cJSON_Print(VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}
cJSON_Delete( PetAPIlocalVarJSON);
cJSON_Delete( VarJSON);
cJSON_Delete( PetAPIlocalVarJSON);
cJSON_Delete( VarJSON);
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
@@ -303,6 +318,9 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t *tags)
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("/pet/findByTags")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -311,6 +329,7 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t *tags)
// query parameters
if (tags)
{
@@ -336,24 +355,27 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t *tags)
//if (apiClient->response_code == 400) {
// printf("%s\n","Invalid tag value");
//}
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
if(!cJSON_IsArray(PetAPIlocalVarJSON)) {
return 0;//nonprimitive container
}
list_t *elementToReturn = list_createList();
cJSON *VarJSON;
cJSON_ArrayForEach(VarJSON, PetAPIlocalVarJSON)
{
if(!cJSON_IsObject(VarJSON))
{
// return 0;
list_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
if(!cJSON_IsArray(PetAPIlocalVarJSON)) {
return 0;//nonprimitive container
}
elementToReturn = list_createList();
cJSON *VarJSON;
cJSON_ArrayForEach(VarJSON, PetAPIlocalVarJSON)
{
if(!cJSON_IsObject(VarJSON))
{
// return 0;
}
char *localVarJSONToChar = cJSON_Print(VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}
char *localVarJSONToChar = cJSON_Print(VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}
cJSON_Delete( PetAPIlocalVarJSON);
cJSON_Delete( VarJSON);
cJSON_Delete( PetAPIlocalVarJSON);
cJSON_Delete( VarJSON);
}
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
@@ -373,6 +395,67 @@ end:
}
// Number of days since the last time a pet maimed someone at the store
//
int
PetAPI_getDaysWithoutIncident(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/daysWithoutIncident")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/store/daysWithoutIncident");
list_addElement(localVarHeaderType,"*/*"); //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 simple integer
int elementToReturn = 0;
if(apiClient->response_code >= 200 && apiClient->response_code < 300)
elementToReturn = atoi(apiClient->dataReceived);
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return 0;
}
// Find pet by ID
//
// Returns a single pet
@@ -387,12 +470,16 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
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("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
if(petId == 0){
@@ -433,11 +520,14 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
// printf("%s\n","Pet not found");
//}
//nonprimitive not container
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
pet_t *elementToReturn = pet_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
pet_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = pet_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type
@@ -460,6 +550,67 @@ end:
}
// Get a random picture of someone else's pet
//
binary_t*
PetAPI_getPicture(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("/pet/picture")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/picture");
list_addElement(localVarHeaderType,"*/*"); //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 simple binary
binary_t* elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300)
elementToReturn = instantiate_binary_t(apiClient->dataReceived, apiClient->dataReceivedLen);
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_freeList(localVarHeaderType);
free(localVarPath);
return elementToReturn;
end:
free(localVarPath);
return NULL;
}
// Is this pet still available?
//
openapi_petstore_bit__e
@@ -472,12 +623,16 @@ PetAPI_isPetAvailable(apiClient_t *apiClient, long petId)
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("/pet/{petId}/isAvailable")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/isAvailable");
// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
if(petId == 0){
@@ -509,11 +664,14 @@ PetAPI_isPetAvailable(apiClient_t *apiClient, long petId)
// printf("%s\n","successful operation");
//}
//nonprimitive not container
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
openapi_petstore_bit__e elementToReturn = bit_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == 0) {
// return 0;
openapi_petstore_bit__e elementToReturn = 0;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = bit_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == 0) {
// return 0;
}
}
//return type
@@ -548,6 +706,9 @@ PetAPI_sharePicture(apiClient_t *apiClient, char *picture)
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("/pet/picture")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -556,6 +717,7 @@ PetAPI_sharePicture(apiClient_t *apiClient, char *picture)
// Body Param
localVarBodyParameters = strdup(picture);
list_addElement(localVarHeaderType,"*/*"); //produces
@@ -573,8 +735,10 @@ PetAPI_sharePicture(apiClient_t *apiClient, char *picture)
//if (apiClient->response_code == 200) {
// printf("%s\n","successful operation");
//}
//primitive return type simple
char* elementToReturn = strdup((char*)apiClient->dataReceived);
//primitive return type simple string
char* elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300)
elementToReturn = strdup((char*)apiClient->dataReceived);
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
@@ -609,6 +773,9 @@ PetAPI_specialtyPet(apiClient_t *apiClient)
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("/pet/specialty")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -616,6 +783,7 @@ PetAPI_specialtyPet(apiClient_t *apiClient)
list_addElement(localVarHeaderType,"application/xml"); //produces
list_addElement(localVarHeaderType,"application/json"); //produces
apiClient_invoke(apiClient,
@@ -633,11 +801,14 @@ PetAPI_specialtyPet(apiClient_t *apiClient)
// printf("%s\n","successful operation");
//}
//nonprimitive not container
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
openapi_petstore_preference__e elementToReturn = preference_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == 0) {
// return 0;
openapi_petstore_preference__e elementToReturn = 0;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = preference_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == 0) {
// return 0;
}
}
//return type
@@ -671,6 +842,9 @@ PetAPI_updatePet(apiClient_t *apiClient, pet_t *body)
list_t *localVarContentType = list_createList();
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/pet")+1;
char *localVarPath = malloc(sizeOfPath);
@@ -679,6 +853,7 @@ PetAPI_updatePet(apiClient_t *apiClient, pet_t *body)
// Body Param
cJSON *localVarSingleItemJSON_body = NULL;
if (body != NULL)
@@ -744,12 +919,16 @@ PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId, char *name, char *s
list_t *localVarContentType = list_createList();
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/pet/{petId}")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}");
// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
if(petId == 0){
@@ -851,12 +1030,16 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId, char *additionalMetadata,
list_t *localVarContentType = list_createList();
char *localVarBodyParameters = NULL;
// clear the error code from the previous api call
apiClient->response_code = 0;
// create the path
long sizeOfPath = strlen("/pet/{petId}/uploadImage")+1;
char *localVarPath = malloc(sizeOfPath);
snprintf(localVarPath, sizeOfPath, "/pet/{petId}/uploadImage");
// Path Params
long sizeOfPathParams_petId = sizeof(petId)+3 + strlen("{ petId }");
if(petId == 0){
@@ -913,11 +1096,14 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId, char *additionalMetadata,
// printf("%s\n","successful operation");
//}
//nonprimitive not container
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
api_response_t *elementToReturn = api_response_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
api_response_t *elementToReturn = NULL;
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
elementToReturn = api_response_parseFromJSON(PetAPIlocalVarJSON);
cJSON_Delete(PetAPIlocalVarJSON);
if(elementToReturn == NULL) {
// return 0;
}
}
//return type