Skip to content

[BUG][C] When an enum is missing from JSON the default value is set to -1 #17513

@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 assign a default value of -1 when reading the item from JSON.

In item_resource.c

    item_resource_local_var = item_resource_create (
        value ? valueVariable : -1
        );

Expected

    item_resource_local_var = item_resource_create (
        value ? valueVariable : simple_api_item_resource_VALUE_NULL
        );
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
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.

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 98bbfd3799..e8f0841d60 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
@@ -845,7 +845,7 @@ fail:
         {{^isPrimitiveType}}
         {{#isModel}}
         {{#isEnum}}
-        {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim_enum{{^required}} : -1{{/required}}{{^-last}},{{/-last}}
+        {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim_enum{{^required}} : {{projectName}}_{{classVarName}}_{{enumName}}_NULL{{/required}}{{^-last}},{{/-last}}
         {{/isEnum}}
         {{^isEnum}}
         {{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_nonprim{{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
@@ -875,7 +875,7 @@ fail:
         {{/isBoolean}}
         {{#isEnum}}
         {{#isString}}
-        {{^required}}{{{name}}} ? {{/required}}{{name}}Variable{{^required}} : -1{{/required}}{{^-last}},{{/-last}}
+        {{^required}}{{{name}}} ? {{/required}}{{name}}Variable{{^required}} : {{projectName}}_{{classVarName}}_{{enumName}}_NULL{{/required}}{{
^-last}},{{/-last}}
         {{/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