-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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: stringGeneration 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_cSteps to reproduce
- Copy the YAML above into
unique_array_openapi.yaml. - Run
java -jar openapi-generator-cli.jar generate -i unique_array_openapi.yaml -g c -o openapi_c - Examine
openapi_c/model/unique_array.handopenapi_c/model/unique_array.cto see the inconsistent use ofsetandlistand the inclusion of#include "set.h". - Note that there is no
set.hin theopenapi_c/includedirectory. - Note that there is no
set.cin theopenapi_c/srcdirectory.
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.