kubernetes-client / c

Official C client library for Kubernetes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v1_daemon_set_convertToJSON is returning NULL cJSON object

hirishh opened this issue · comments

commented

Hello,

I've debugged this and this function is returning null while parsing the status. I have a simple Daemonset with this status:

status:
  currentNumberScheduled: 1
  numberMisscheduled: 0
  desiredNumberScheduled: 1
  numberReady: 1
  observedGeneration: 1
  updatedNumberScheduled: 1
  numberAvailable: 1

The conversion is failing here: https://github.com/kubernetes-client/c/blob/master/kubernetes/model/v1_daemon_set_status.c#L113

Is there any reason why this code must check the value of the int and fail if it's zero? I don't understand the logic here.

Best,

hirishh

commented

I think this is a defect that has never been raised before.
Thank you for reporting this.

commented

If you want to fix this issue, please note that the code is generated by openapi-generator from here: https://github.com/OpenAPITools/openapi-generator/blob/59ba00e1f3ddb1efa5ae064987cb4e5a6286e8d5/modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache#L357

Thank you for pointing me out. I tried to had a look. As far as I understand the problem is when a field is an int and also required, indeed the same issue is present for currentNumberScheduled, desiredNumberScheduled and numberReady that are required fields in the deamonset status object.

But I have zero experience with openapi-generator and I'm struggling with the syntax there (for example I don't understand the difference between {{#required}} and {{^required}}, maybe # -> is and ^-> is not) ?

commented

Maybe this is enough to fix the issue? (L333 - L344)

    {{#required}}
    {{^isEnum}}
    {{^isNumeric}}
    if (!{{{classname}}}->{{{name}}}) {
        goto fail;
    }
    {{/isNumeric}}
    {{/isEnum}}
    {{#isEnum}}
    if ({{projectName}}_{{classVarName}}_{{enumName}}_NULL == {{{classname}}}->{{{name}}}) {
        goto fail;
    }
    {{/isEnum}}
    {{/required}}
commented

Your fix is correct. Now it can return a valid cJSON object.
We can also add ^isBoolean here to fix boolean type (0).

And for the non-required property, we'd better to add the code too:

    {{^required}}  <-- L345
    {{^isEnum}}
    {{^isNumeric}}
    {{^isBoolean}}
    if({{{classname}}}->{{{name}}}) {
    {{/isBoolean}}
    {{/isNumeric}}
    {{/isEnum}}
    {{#isEnum}}
    if({{{classname}}}->{{{name}}} != {{projectName}}_{{classVarName}}_{{enumName}}_NULL) {
    {{/isEnum}}
    {{/required}}
    {{^required}}  <-- L550
    {{^isNumeric}}
    {{^isBoolean}}
    }
    {{/isBoolean}}
    {{/isNumeric}}
    {{/required}}
commented

Hello @ityuhui, how we should proceed with this issue?

commented

I'll look at the reviewer's comments and talk with him.