OpenAPITools / OpenAPITools/openapi-generator

[BUG] [C] Presence of an array of uniqueItems results in incomplete and inconsistent model files

Open
#14,234 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The c language generator does not produce code that will compile and contains inconsistent use of the set_t and list_t types.

The generated code tries to #include "set.h" in the model header (${OUTDIR}/model/unique_array.h generated from the example given below), but this file is not a standard header file or part of the generated files. This means the generated source files are incomplete.

The model header uses the set_t data type to implement an OpenAPI array where uniqueItems == true, but the model implementation (${OUTDIR}/model/unique_array.c generated from the example given below) treats the same data structure component as a list_t using list_... functions to manipulate and traverse it.

openapi-generator version

Checked with v5.2.0, v6.2.1 and the latest 7.0.0 Snapshot (openapi-generator-cli-7.0.0-20221012.083708-4.jar).

OpenAPI declaration file content or url

OpenAPI 3.0.0 YAML example that reproduces the issue:

openapi: 3.0.0
info:
  title: UniqueItemsArrayCheck
  version: 0.0.0
  description: |
    A test OpenAPI YAML file to exercise the handling of uniqueItems in arrays.
servers:
  - url: '{apiRoot}/uniqueArray'
    variables:
      apiRoot:
        default: https://example.com
        description: The root of the API
paths:
  /item:
    post:
      operationId: createUniqueArray
      summary: 'Create an array of unique items resource'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/UniqueArray'
      responses:
        '201':
          description: Object created
          headers:
            Location:
              required: true
              schema:
                type: string
                format: uri
components:
  schemas:
    UniqueArray:
      type: object
      required:
        - theSet
      properties:
        theSet:
          type: array
          uniqueItems: true
          items:
            type: string
Generation Details

Used the following command to generate the model code to check:

java -jar openapi-generator-cli-7.0.0-20221012.083708-4.jar generate -i unique_array_openapi.yaml -g c -o openapi_c
Steps to reproduce
  1. Copy the YAML above into unique_array_openapi.yaml.
  2. Run java -jar openapi-generator-cli.jar generate -i unique_array_openapi.yaml -g c -o openapi_c
  3. Examine openapi_c/model/unique_array.h and openapi_c/model/unique_array.c to see the inconsistent use of set and list and the inclusion of #include "set.h".
  4. Note that there is no set.h in the openapi_c/include directory.
  5. Note that there is no set.c in the openapi_c/src directory.
Related issues/PRs

A similar issue appears to have been reported against version 5.0.0 in #6519.

Suggest a fix

The templates for the C-libcurl language should not use list_... functions (e.g. line 303 in openapi-genertaor/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache using list_ForEach) and should use {{datatype}}_... functions instead, or have an exception for arrays of unique items, to use the correct set_... functions where appropriate. One exception to the simple one for one change from list to set is the set_createSet function (instead of list_createList) which will need to additionally take a pointer to a comparison function for the data type being stored in the set, so that new set entries can be compared for uniqueness. This will necessitate the defining of comparison functions for generated complex data types in the model.

#include "set.h" should be #include "../include/set.h" in the model header. The set.h header file should be created in the generated include directory. The set.c file should be created in the src directory and included in the CMake files.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache and reproduce the issue using the supplied unique_array_openapi.yaml and generation command. Compare the generated model/unique_array.h and model/unique_array.c, then inspect the generated include, src, and CMake files. Done means uniqueItems models use consistent set handling and the generated C project contains the required headers, source, and build entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, cmake
Domain
build-system, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.