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] When an enum is missing from JSON the default value is set to -1 #17513

Closed
5 of 6 tasks
bookerdj opened this issue Jan 2, 2024 · 0 comments · Fixed by #18040
Closed
5 of 6 tasks

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

bookerdj opened this issue Jan 2, 2024 · 0 comments · Fixed by #18040

Comments

@bookerdj
Copy link
Contributor

bookerdj commented Jan 2, 2024

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}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant