OpenAPITools / OpenAPITools/openapi-generator

[BUG] [C] Enum is attempted to be freed

Open
#15,271 0 comments 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)?
  • 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

In certain cases when providing an enum as an input the generated C code will attempt to free the enum after it is used.
For the example spec provided it generates this enum:

// Enum CASCADES for ControllerAPI_deleteitem
typedef enum  { simple_api_deleteitem_CASCADES_NULL = 0, simple_api_deleteitem_CASCADES_none, simple_api_deleteitem_CASCADES_subitem } simple_api_deleteitem_cascades_e;

Which is used once

    simple_api_deleteitem_cascades_e valueQuery_cascades ;
    keyValuePair_t *keyPairQuery_cascades = 0;
    if (cascades)
    {
        keyQuery_cascades = strdup("cascades");
        valueQuery_cascades = (cascades);
        keyPairQuery_cascades = keyValuePair_create(keyQuery_cascades, (void *)strdup(deleteitem_CASCADES_ToString(
		valueQuery_cascades)));
        list_addElement(localVarQueryParameters,keyPairQuery_cascades);
    }

And then for some reason freed which causes a compilation error:

    if(valueQuery_cascades){
        free(valueQuery_cascades);
        valueQuery_cascades = NULL;
    }
openapi-generator version

Originally seen with 6.2.1 and reproduced on 6.5.0 and latest 6.60 snapshot

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Simple API
  version: 1.0.0
servers:
- url: /
paths:
  /{id}:
    delete:
      tags:
      - controller
      summary: Deletes an item and optionally its subitems
      description: |-
        Deletes an item

        Minimum API version: 1.0
      operationId: deleteitem
      parameters:
      - name: id
        in: path
        description: |-
          Delete the specific item in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
          Minimum API version: 1.0
        required: true
        schema:
          type: string
      - name: cascades
        in: query
        schema:
          type: string
          enum:
            - none
            - subitem
      responses:
        204:
          description: Deleted the item and any associated subitems
          content: {}
Generation Details
java -jar ../openapi-generator-cli-6.6.0-20230420.080546-29.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/api/ControllerAPI.c
Related issues/PRs

I tried out this PR which mentions fixing C enums but it didn't fix the issue https://github.com/OpenAPITools/openapi-generator/pull/14434

Suggest a fix

The only extra information I have is that the issue goes away if I define a schema in the response such as the following:

openapi: 3.0.1
info:
  title: Simple API
  version: 1.0.0
servers:
- url: /
paths:
  /{id}:
    delete:
      tags:
      - controller
      summary: Deletes an item and optionally its subitems
      description: |-
        Deletes an item

        Minimum API version: 1.0
      operationId: deleteitem
      parameters:
      - name: id
        in: path
        description: |-
          Delete the specific item in the form of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
          Minimum API version: 1.0
        required: true
        schema:
          type: string
      - name: cascades
        in: query
        schema:
          type: string
          enum:
            - none
            - subitem
      responses:
        204:
          description: Deleted the item and any associated subitems
          content:
            application/json:
              schema:
                type: object

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 the provided OpenAPI YAML and reproduce generation using the documented java command. Inspect the generated cleanup code in out/api/ControllerAPI.c, especially the handling of valueQuery_cascades after the enum is used. Done means the same spec no longer produces an invalid free operation in the generated C client.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
backend-api-design, 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.