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] Enum is attempted to be freed #15271

Open
5 of 6 tasks
bookerdj opened this issue Apr 20, 2023 · 0 comments
Open
5 of 6 tasks

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

bookerdj opened this issue Apr 20, 2023 · 0 comments

Comments

@bookerdj
Copy link
Contributor

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

No branches or pull requests

1 participant