Skip to content

Fix a few issues with the C generator (part 1)#14388

Closed
eafer wants to merge 4 commits intoOpenAPITools:masterfrom
eafer:c_api_fixes_1
Closed

Fix a few issues with the C generator (part 1)#14388
eafer wants to merge 4 commits intoOpenAPITools:masterfrom
eafer:c_api_fixes_1

Conversation

@eafer
Copy link
Contributor

@eafer eafer commented Jan 5, 2023

As requested, I'm splitting the first three patches from #14379 into a new pull request to make them easier to review. These fix a few compilation errors for the generated code, but they are not enough by themselves to get it to build.

eafer added 4 commits January 3, 2023 21:01
Implement an object type that simply holds a cJSON object inside. This
patch was originally based on an old version of openapi where object_t
was broken, so it's probably unnecessary now. The only difference is
that the internal representation is as a cJSON object, not a string.
I'm guessing that enums have not been used much with the C generator
before, because they always seem to produce code that doesn't build, or
that tries to free them after use. This patch fixes all the problems
we've encountered so far, except for those that need checking the return
type. I'll come back to that later.
As required for a pull request, run the generate-samples.sh script and
commit the changes.
@eafer
Copy link
Contributor Author

eafer commented Jan 5, 2023

@@ -0,0 +1,5 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need add header or source files, you can add a "PreTarget.cmake", e.g. https://github.com/kubernetes-client/c/blob/master/kubernetes/PreTarget.cmake

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The any_type.h header was not our invention, the generator sometimes puts #include "any_type.h" lines in the code, so this was needed to get it to build. A better solution would be to check why these lines are getting added in the first place, but either way this looks like a bug, not something that should be fixed by the user.

By the way this is the api we are working with, in case you want to check why we are running into different problems than you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarification. I'll go to check it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header is generated if the api yaml has anyof present other than that of enums. Currently anyof, oneof and allof are not handled. Anyof is handled but only for enums, there is a hack in java code for this.

Copy link
Contributor Author

@eafer eafer Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the #include lines are added, but the header itself is not generated (at least for our api), so the build is broken. I got it to work by adding an empty any_type.h header, but maybe you know a better way?

@@ -0,0 +1,5 @@
@PACKAGE_INIT@
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a project has its own "Config.cmake.in", your template file will replace it...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, this pull request doesn't seem to include any cmake stuff. Any idea why this is happening?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, probably you need to manually remove it from this PR.


typedef struct object_t {
void *temporary;
cJSON *json;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why void * needs to be changed to cJSON *? May I know what requirements object.h and object.c cannot meet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None at all, I explained that in the commit message. This patch was written at a time when object_t was broken, before you fixed it yourself. So it was easier for us to just keep working with our version, which we had already tested. We didn't think anyone would mind, since so many changes are needed anyway, but I can use your version of object_t if you prefer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to keep the current code. Actually this is not my original. I took the code from 166de17 and found it to work fine. So let's keep the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If needed we could modify it as a union so we can accommodate int, char, void* data types as well

@eafer
Copy link
Contributor Author

eafer commented Jan 11, 2023

I submitted a new version of this pull request, so I guess this one can be closed.

@eafer eafer closed this Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants