Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
davidjwbbc opened this issue Dec 9, 2022 · 0 comments

Comments

@davidjwbbc
Copy link

davidjwbbc commented Dec 9, 2022

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.

mlebihan added a commit to mlebihan/openapi-generator that referenced this issue May 2, 2024
…fined "Object.h" (OpenAPITools#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues OpenAPITools#2769, OpenAPITools#10266, OpenAPITools#14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```
mlebihan added a commit to mlebihan/openapi-generator that referenced this issue May 2, 2024
…fined "Object.h" (OpenAPITools#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues OpenAPITools#2769, OpenAPITools#10266, OpenAPITools#14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```
wing328 pushed a commit that referenced this issue May 4, 2024
#18553)

* [BUG] [C++][Pistache] cpp-pistache-server generating API include undefined "Object.h" (#2769)

Should handle Object.h, AnyType.h correctly. Set.h also tested.

   - #include Object.h removed and replaced by a typeMapping.put(object, nlohmann::json) like suggested in other issues
   - object had an invalid syntax: ':' instead of '::' in types with namespace
   - extra include of #include nlohmann/json.h removed when there's already #include <nlohmann/json.hpp>
   - nlohmann::json is excluded from model namespace

Tested with custom petstore played, with suggested openapi specs coming from issues #2769, #10266, #14234

   ```bash
   rm -rf samples/server/petstore/cpp-pistache-everything/ && ./bin/generate-samples.sh ./bin/configs/cpp-pistache-server-cpp-pistache-everything.yaml  && cd samples/server/petstore/cpp-pistache-everything/ && mkdir build && cd build && cmake .. && cmake --build . --parallel
   ```

* - Adding to samples/server/petstore cpp-pistache-everything

* - .md and FILES missing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant