Skip to content

[BUG] [C] Inconsistent function naming for enum string conversions #17510

@bookerdj

Description

@bookerdj

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

When generating code containing enums we get functions to convert to/from a string. However the function name and the prototype are different.

In item_resource.c

char* valueitem_resource_ToString(simple_api_item_resource_VALUE_e value) {
    char* valueArray[] =  { "NULL", "value1", "value2" };
    return valueArray[value];
}

In item_resource.h

char* item_resource_value_ToString(simple_api_item_resource_VALUE_e value);

I would expect them to have the same names. We have instead just been patching the output.

openapi-generator version

Tested with 6.6.0 originally but reproduced on 7.2.0 and master (8b5b5a7).

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Simple API
  version: 1.0.0
servers:
- url: /
paths:
  /{id}:
    get:
      summary: Get item
      description: |-
        Gets the item.
      parameters:
      - name: id
        in: path
        description: |-
          Gets the specific item in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
        required: true
        schema:
          type: string
      - name: content
        in: query
        schema:
          type: string
          enum:
            - all
            - subset
      responses:
        '200':
          $ref: '#/components/responses/GetItem'
components:
  schemas:
    ItemResource:
      type: object
      properties:
        value:
          type: string
          enum:
            - value1
            - value2
    GetItemResult:
      type: object
      properties:
        key:
          $ref: '#/components/schemas/ItemResource'
      description: |-
        Representation of an item
  responses:
    GetItem:
      description: |-
        Representation of an item returned
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/GetItemResult'
Generation Details
java -jar ../openapi-generator-cli-7.2.0.jar generate -g c  --additional-properties=prependFormOrBodyParameters=true -o out -i ../simple.yaml
Steps to reproduce
  1. Generate the C client with the above command line and the provided spec
  2. Observe the incorrect code output in out/model/item_resource.(c/h)
Related issues/PRs

I have looked through all the issues marked with [C] and can't see any duplicates. I also can't find any open PRs attempting to fix this.

Suggest a fix

I think this is the fix but I don't know if it's complete or if it will break anything else. I'll try and make a PR one I work out how to do that.

diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache
index 1cb6759f7ad..98bbfd37998 100644
--- a/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache
+++ b/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache
@@ -77,12 +77,12 @@ end:
     {{^isContainer}}
     {{^isModel}}
     {{#isEnum}}
-char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) {
+char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) {
     char* {{name}}Array[] =  { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} };
     return {{name}}Array[{{name}}];
 }
 
-{{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}){
+{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_{{name}}_FromString(char* {{name}}){
     int stringToReturn = 0;
     char *{{name}}Array[] =  { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} };
     size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]);
@@ -101,12 +101,12 @@ char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName
     {{#items}}
     {{^isModel}}
     {{#isEnum}}
-char* {{name}}{{classname}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) {
+char* {{classname}}_{{name}}_ToString({{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}) {
     char *{{name}}Array[] =  { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} };
     return {{name}}Array[{{name}} - 1];
 }
 
-{{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}) {
+{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_{{name}}_FromString(char* {{name}}) {
 }
 
-{{projectName}}_{{classVarName}}_{{enumName}}_e {{name}}{{classname}}_FromString(char* {{name}}) {
+{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_{{name}}_FromString(char* {{name}}) {
     int stringToReturn = 0;
     char *{{name}}Array[] =  { "NULL"{{#allowableValues}}{{#values}}, "{{.}}"{{/values}}{{/allowableValues}} };
     size_t sizeofArray = sizeof({{name}}Array) / sizeof({{name}}Array[0]);
@@ -629,7 +629,7 @@ fail:
     {
     goto end; //Enum
     }
-    {{name}}Variable = {{name}}{{classname}}_FromString({{{name}}}->valuestring);
+    {{name}}Variable = {{classname}}_{{name}}_FromString({{{name}}}->valuestring);
     {{/isString}}
     {{/isEnum}}
     {{^isEnum}}

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