Ernesto Fernández 52b5b8fb76
Fix a few issues with the C generator (part 2) (#20227)
* [C] Don't convert post body strings to JSON

If the body provided for the api request is a just a string itself,
don't try to convert it to JSON, simply submit the string.

* [C] Implement BearerToken authentication

* [C] Handle nullable fields correctly

* [C] Fix implementation of FromString for enums

* [C] Update the test schemas to cover the changes

* Update samples

* Fix the updated samples

* [C] Add the new samples folder to the CI workflow
2024-12-06 01:32:34 +08:00

42 lines
1.3 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "preference.h"
char* preference_preference_ToString(openapi_petstore_preference__e preference) {
char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" };
return preferenceArray[preference];
}
openapi_petstore_preference__e preference_preference_FromString(char* preference) {
int stringToReturn = 0;
char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" };
size_t sizeofArray = sizeof(preferenceArray) / sizeof(preferenceArray[0]);
while(stringToReturn < sizeofArray) {
if(strcmp(preference, preferenceArray[stringToReturn]) == 0) {
return stringToReturn;
}
stringToReturn++;
}
return 0;
}
cJSON *preference_convertToJSON(openapi_petstore_preference__e preference) {
cJSON *item = cJSON_CreateObject();
if(cJSON_AddStringToObject(item, "preference", preference_preference_ToString(preference)) == NULL) {
goto fail;
}
return item;
fail:
cJSON_Delete(item);
return NULL;
}
openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON) {
if(!cJSON_IsString(preferenceJSON) || (preferenceJSON->valuestring == NULL)) {
return 0;
}
return preference_preference_FromString(preferenceJSON->valuestring);
}