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

100 lines
2.4 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mapped_model.h"
MappedModel_t *MappedModel_create(
int another_property,
char *uuid_property
) {
MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t));
if (!MappedModel_local_var) {
return NULL;
}
MappedModel_local_var->another_property = another_property;
MappedModel_local_var->uuid_property = uuid_property;
return MappedModel_local_var;
}
void MappedModel_free(MappedModel_t *MappedModel) {
if(NULL == MappedModel){
return ;
}
listEntry_t *listEntry;
if (MappedModel->uuid_property) {
free(MappedModel->uuid_property);
MappedModel->uuid_property = NULL;
}
free(MappedModel);
}
cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) {
cJSON *item = cJSON_CreateObject();
// MappedModel->another_property
if(MappedModel->another_property) {
if(cJSON_AddNumberToObject(item, "another_property", MappedModel->another_property) == NULL) {
goto fail; //Numeric
}
}
// MappedModel->uuid_property
if(MappedModel->uuid_property) {
if(cJSON_AddStringToObject(item, "uuid_property", MappedModel->uuid_property) == NULL) {
goto fail; //String
}
}
return item;
fail:
if (item) {
cJSON_Delete(item);
}
return NULL;
}
MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
MappedModel_t *MappedModel_local_var = NULL;
// MappedModel->another_property
cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property");
if (cJSON_IsNull(another_property)) {
another_property = NULL;
}
if (another_property) {
if(!cJSON_IsNumber(another_property))
{
goto end; //Numeric
}
}
// MappedModel->uuid_property
cJSON *uuid_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "uuid_property");
if (cJSON_IsNull(uuid_property)) {
uuid_property = NULL;
}
if (uuid_property) {
if(!cJSON_IsString(uuid_property) && !cJSON_IsNull(uuid_property))
{
goto end; //String
}
}
MappedModel_local_var = MappedModel_create (
another_property ? another_property->valuedouble : 0,
uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL
);
return MappedModel_local_var;
end:
return NULL;
}