Skip to content

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

Open
@davidjwbbc

Description

@davidjwbbc

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions