[C-libcurl] Heap-allocate boolean primitive list items

HI
2d ago
+607-2

cc @zhemant @ityuhui @michelealbano @eafer (C technical committee)

Problem

For an array of booleans (type: array, items: {type: boolean}), the C-libcurl
deserializer adds each item with the raw int value:

list_addElement({{name}}List, {{name}}_local->valueint);

list_addElement takes a pointer, so the generated code does not compile under
-Werror=int-conversion. The generated _free also calls free() on every list
entry, and _convertToJSON reads each entry as *(cJSON_bool *)data, so a cast
alone would not be correct.

The same line exists in two templates:

  • model-body.mustache (default output)
  • model-body-v7_20_0.mustache (used with declareNumberBooleanWithoutPointer=true)

Real-world trigger: Discovered this issue while building the c library for Kubernetes 1.37 spec (DeviceAttribute.bools). It failed to build before this fix.

Fix

Heap-allocate each item, as the numeric branch already does, in both templates:

cJSON_bool *{{name}}_local_value = calloc(1, sizeof(cJSON_bool));
if(!{{name}}_local_value) { goto end; }
*{{name}}_local_value = {{name}}_local->valueint;
list_addElement({{name}}List, {{name}}_local_value);

Testing

  • Flags schema with a boolean array added to 2_0/c/petstore.yaml.
  • The c and c-useJsonUnformatted samples build in CI with -Werror=int-conversion, so the
    generated model is compiled there. Both builds fail with the previous template (checked).
  • samples/client/petstore/c/unit-tests/manual-flags.c: round trip (parse, inspect, serialize,
    free). Passes with the fix.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes generated C-libcurl code for boolean array items so it compiles under -Werror=int-conversion and matches how numeric items are handled.

  • Boolean array items are heap-allocated as cJSON_bool pointers before being added to the list, in both model-body.mustache and model-body-v7_20_0.mustache.
  • Adds a Flags schema with a boolean array to the C petstore spec and includes a manual round-trip test.
  • Fixes a leak where a parse error after a boolean item left the item unfreed, and makes the v7_20_0 template return NULL when allocation fails instead of skipping cleanup.
  • The manual test's failing-parse case stores the result before asserting, so the error path runs even under NDEBUG.