OpenAPITools / OpenAPITools/openapi-generator

[BUG][C] C-client: referenced enum missing in model

Open
#5,610 1 comment 0 reactions 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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When a simple enum is referenced in an object properties, it's missing in the model body (_create function) and header (model struct).

openapi-generator version

master branch on 2020-03-17T10:41:00Z.
Don't really know if it's a regression, I tried using the latest stable version too, but then I was mainly stuck with #5477 (this is why I'm using the master branch)

OpenAPI declaration file content or url

enum-bug.yaml

Command line used for generation
./run-in-docker.sh generate -i samples/client/enum-bug.yaml -g c -o /gen/out/enum-c
Steps to reproduce
  1. Use the provided "enum-bug.yaml" specification, or any API with a referenced enum inside an object schema
  2. Generate C-Libcurl client against the yaml
  3. Try to compile it using CMake
  4. Multiple errors

Here is the debugModels output:
debug_models.txt

Here is the model header file with error commented:
object_dummy.h

Here is the model body file with error commented:
object_dummy.c

To get the compiler errors, just try to compile the generated code, it will be much simpler.

Related issues/PRs

My last pull request #5604 that fixed a problem that was still present after #5477 was merged

Suggest a fix

A fix could be something like the following fix, as stated in the debugModels output, the referenced enum is considered as "^isModel" and "^isPrimitiveType", so we have to add a third case : an enum that is neither a model nor a primitive type.

Although maybe it should be considered as a model, and that this causes the bug, but I'm not enough familiar with those definitions to be sure.

diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache
index 03a03276f6..5d492a95ac 100644
--- a/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache
+++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-header.mustache
@@ -91,6 +91,9 @@ typedef struct {{classname}}_t {
     {{#isFreeFormObject}}
     {{datatype}}_t *{{name}}; //object
     {{/isFreeFormObject}}
+    {{#isEnum}}
+    {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}; //enum
+    {{/isEnum}}
     {{/isPrimitiveType}}
     {{#isPrimitiveType}}
     {{#isNumeric}}
@@ -161,6 +164,9 @@ typedef struct {{classname}}_t {
     {{#isFreeFormObject}}
     {{datatype}}_t *{{name}}{{#hasMore}},{{/hasMore}}
     {{/isFreeFormObject}}
+    {{#isEnum}}
+    {{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{#hasMore}},{{/hasMore}}
+    {{/isEnum}}
     {{/isPrimitiveType}}
     {{#isPrimitiveType}}
     {{#isNumeric}}

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 enum-bug.yaml and reproduce generation using the provided run-in-docker.sh command, then inspect the generated object_dummy.h and object_dummy.c alongside debugModels output. Read model-header.mustache and the corresponding C-libcurl model template to trace how referenced enums are classified. Done means the generated client includes the referenced enum in the model and compiles successfully with CMake.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.