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

Add C client generator #516

Merged
merged 123 commits into from
Nov 16, 2018
Merged

Add C client generator #516

merged 123 commits into from
Nov 16, 2018

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Jul 9, 2018

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.1.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

For #226

Still WIP

cc @PowerOfCreation

@wing328 wing328 modified the milestone: 3.1.1 Jul 9, 2018
@wing328 wing328 added this to To do in Add C client generator via automation Jul 9, 2018
@wing328 wing328 moved this from To do to In progress in Add C client generator Jul 9, 2018
import java.util.Iterator;
import java.util.Map;

public class CClientCodegen extends DefaultCodegen implements CodegenConfig {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to name it CCUrlClientCodegen, so that the name reflects the library used as well? maybe in the future we have more than one c client

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

I've renamed it as "CLibcurlClientCodegen.java" to follow the {language}{framework|library}{Client|Server}Codegen pattern.

Ref: https://github.com/curl/curl


modelPackage = "models";
apiPackage = "api";
outputFolder = "generated-code" + File.separator + "C";
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe generated-code/C-LibCurl

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I've switched it to generated-code/C-libcurl.

Copy link
Contributor

@stkrwork stkrwork left a comment

Choose a reason for hiding this comment

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

all the gotos will need to be replaced.

cJSON *item = cJSON_CreateObject();
// api_response->code
if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) {
goto fail;
Copy link
Contributor

Choose a reason for hiding this comment

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

why use goto? Why not insert the "cJSON_Delete(item);" and "return NULL;" in the if statement?

Copy link
Contributor

Choose a reason for hiding this comment

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

also there seems to be an indentation error here.

Copy link
Contributor

Choose a reason for hiding this comment

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

The cJSON library uses goto in the examples as well to avoid doubled code: https://github.com/DaveGamble/cJSON#printing

Copy link
Contributor

Choose a reason for hiding this comment

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

the if statement could be refactored to if (condition1 || condition2 || condition3) cJSON_Delete(item); return NULL;

Then there would also be no code duplikation.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you want to refactor there? The checks are made at each method that allocated new memory to avoid a Segmentation Fault, they can't be combined. By combining and only doing the checks once you will already have a Segmentation Fault before the combined checks. There could only be written a new method that handles the cJSON_Delete(item); return NULL; and would be used somewhat like this:

if(cJSON_AddNumberToObject(item, "code", api_response->code) == NULL) {
return cJSON_deleteJSONObjectAndReturnNull(item);
...
cJSON *cJSON_deleteJSONObjectAndReturnNull(cJSON *item) {
cJSON_Delete(item);
return NULL;
}

This is unnecessary complicated and the goto is a lot easier and cleaner to read in my opinion. I really don't see a point to remove the goto's there as they are also used like that in the example code of the library.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can go with the current implementation for the beta release and consider @stkrwork suggestion as a day-2 requirement for improvement to the auto-generated C code.

Copy link
Contributor

Choose a reason for hiding this comment

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

@wing328
I agree. The community could have the chance to check and add PR to improve.

@PowerOfCreation
Copy link
Contributor

PowerOfCreation commented Oct 12, 2018

I have tried the generator and I'm getting a few errors when trying to build it.

The errors: https://hastebin.com/zuhifemopu.cs
Our specification file: https://hastebin.com/ixuvebudop.bash

Ignore the php thing, this is only the folder name.

wing328 and others added 3 commits October 12, 2018 18:07
* better format in the c template

* minor format fix
* changed basePath from static to dynamic

* removed unnecessary header declaration

* updated sample
* added curl version check in CMakeList.txt

* Updated README for latest curl version
@PowerOfCreation
Copy link
Contributor

PowerOfCreation commented Oct 16, 2018

I'm still facing a lot of errors when compiling the generated code:
Output: https://hastebin.com/yaradifizu.php
Our specification file: https://hastebin.com/ixuvebudop.bash

@zhemant
Copy link
Contributor

zhemant commented Oct 16, 2018

I am facing issue with baseName, E.g. in file event_type.c base name is "event_type" but which it is used in other files it comes as "eventType" It seems like issue with mix of camel case and snake case

@zhemant
Copy link
Contributor

zhemant commented Oct 17, 2018

This is OAS3 modified for petstore example. I have created a schema for enum that how it is generally used in OAS3 and given reference for the enum. It generates files for this schema, but they dont deal with the data of enums. only empty function are generated.
https://hastebin.com/rixotiroqe.bash

zhemant and others added 2 commits November 3, 2018 22:28
* removed static declaration

* changed static declaration

* added difference for string and non string

* added more code for different function for string and non string

* fix issue with param name

* change value in keyPairValue to void

* fixed issue of difference in function name cases

* added support for non char parameters in api

* fix issue of map return data

* modified manual-StoreAPI map return data handling

* fix minor mistake

* added support for map and changed code to support value of keyvaluepair as char and other

* updated sample

* fixed api header declarations

* change map declaration in header

* resolved issues realted to map data handling

* fix minor issues

* add N at start if enum variable starts with number

* override toParamName method

* changed paramters to paramName from baseName

* change variables in apibody from baseName to paramName
* skip test file generation

* skip overwriting CMakeLists.txt
@wing328
Copy link
Member Author

wing328 commented Nov 16, 2018

The Travis failure (timeout) seems to be caused by the change in the build image (not by this PR):

Changed 62 dependencies!
Precompiling executables...
Precompiled build_runner:graph_inspector.
Precompiled build_runner:build_runner.
[INFO] Generating build script...
[INFO] Generating build script completed, took 280ms
The job exceeded the maximum time limit for jobs, and has been terminated.

@wing328 wing328 merged commit edc05df into master Nov 16, 2018
Add C client generator automation moved this from In progress to Done Nov 16, 2018
@wing328 wing328 added this to the 3.3.4 milestone Nov 16, 2018
@wing328
Copy link
Member Author

wing328 commented Nov 16, 2018

For discussion on the future enhancement of the C generator, please refer to #1473

@wing328 wing328 deleted the c_generator branch November 17, 2018 02:51
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
* add c generator (1st commit)

* udpate c model template

* more fixes

* Add string replace function for C generator (OpenAPITools#908)

* Add string replace function for C generator

* Fixed replacement for variable only

* Fixed problem for different datatypes of paramName

* store return value of modified path

* set str_replace variable to be same as original variable.

* [C] Fixed coding style issues

* add uncrustify support

* update petstore sampmles

* add Locale.ROOT

* added test-api-client.c to include test cases for strReplace function

* added header and body mustache and made changes to ClibcurlClientCodegen.java accordingly

* [C] renamed functions in apiKey.c.mustache according to apiKey.h.mustache

* [C] changes in import statement

* renamed apiKey.h to keyValuePair.h and made necessary changes in the codes

* changed apiKey.c according to keyValuePair.h

* fixed import statement in model

* added code for generating struct in model-header.mustache

* added typedef struct for model-headers

* updated sample/client/petstore/C

* fix locale

* [C] Function addition and modification of major structs (OpenAPITools#1020)

* added readme file

* fixed parameters in api headers functions

* made changes to add readme file and typemapping of array to list

* fixed header import statement in apiheader files

* modified struct of model type in model header

* updated sample

* modified README file

* updated sample

* parse from json function added

* modified struct and create function

* added include for model

* modified parsefromjson function

* modified struct and create function for more datatypes

* added mapping for date-time and modified model import return statement

* modified function parameters

* modified include statement

* fix function in api body

* updated sample

* clean up files

* regenerate c petstore

* fix error message when setting uncrustify

* add version to uncrustify usage

* added uncrustify rules in  mustache (OpenAPITools#1021)

* added uncrustify rules in  mustache

* updated sample

* updated same with crustify

* updated sample with uncrusitfy 0.67

* modified readme about uncrustify requirements

* fixed mistakes in readme mustache and sample readme

* fix file import, unformat c petstore

* fix import in test files

* fix model with complexType

* fix free string, format the code using uncrustify

* modified sample

* Modified sample to check

* return type issue figured,more to do to fix it

* minor fixes to make complete code compile

* Compiling sample code. Store has issue with map.

* comment out test file generation which is not req.

* commented operation name

* fixed various issues responsible for code not compiling

* test mustache temporary for testing

* updated sample add,del,getbyid works, formparam yet to do

* few minor changes and added fuction to add different header and content type in apiClient

* added code to upload image

* added function to test upload image

* fixes for fileupload and various other small things

* fixed issue due to xml produces

* updated sample:working sample add,del,find,uploading:tocheck ,

* added free functions for variable where memory is allocated

* rename imagecontainer struct to filestruct

* fix issues with if functions for all list types

* fixed issue with primitive return type in header file

* updated sample w/ free functions

* update c samples

* remove corrupted file

* update samples

* test cases for APIs

* added function to generate test cases from new mustache

* mustache files for manual written test cases

* added default content type to application/json

* fixed issue with primitive return type

* fixed issue with bool type

* added file apiKey.c

* updated sample tested

* update c environment variable (OpenAPITools#1090)

* add mapping for map (OpenAPITools#1103)

* minor update

* revert list paramter check to NULL

* modified return type for primitive(map - list_t)in mustache

* removed apiClient_free as it was two times

* updated sample

* fixed issue of path parameter when string less than parameter len

* fixed issue for form paramters upload

* added checks to avoid seg faults

* updated sample

* added check for null value in form parameter

* modified size of mallocs to dynamic

* updated sample

* Add C Petstore to Travis CI (OpenAPITools#1136)

* setup CI for C petstore

* update bash script permission

* unit petapi test

* fixed memory leak in strReplace and apiClient Functions

* modified return value for status generation

* added enum defination and functions to convert and back from string

* added function for enum and made changes to free memory at necessary places

* added datatype handling for enum

* fixes regarding memory allocation and free

* updated mustache of test files

* updated sample

* renamed manually written test files

* manual test file for pet

* cleaned common api test file for time being

* renamed test files

* added renamed test files to build test bash

* added file null pointer check

* modified uncrusitfy rules

* minor update to c templates (OpenAPITools#1161)

* [C] Fixed enum function declaration  (OpenAPITools#1178)

* fixed enum function declaration in model headers

* fixed enum declaration in header files for sample

* disable curl verbose mode and add response code variable

* added response code variable in apiClient struct

* modified apiClient header and source file

* added response and removed commented code api-body mustache

* removed commented code from model-body mustache

* removed unnecessary print statements from test mustache

* updated sample

* fixed spaces issue

* Better format in C templates (OpenAPITools#1224)

* better format in the c template

* minor format fix

* [C] changed base url from static to dynamic (OpenAPITools#1225)

* changed basePath from static to dynamic

* removed unnecessary header declaration

* updated sample

* [C] added curl version check in CMakeList.txt (OpenAPITools#1248)

* added curl version check in CMakeList.txt

* Updated README for latest curl version

* [C] Major changes to keyValuePair function (OpenAPITools#1282)

* removed static declaration

* changed static declaration

* added difference for string and non string

* added more code for different function for string and non string

* fix issue with param name

* change value in keyPairValue to void

* fixed issue of difference in function name cases

* added support for non char parameters in api

* fix issue of map return data

* modified manual-StoreAPI map return data handling

* fix minor mistake

* added support for map and changed code to support value of keyvaluepair as char and other

* updated sample

* fixed api header declarations

* change map declaration in header

* resolved issues realted to map data handling

* fix minor issues

* add N at start if enum variable starts with number

* override toParamName method

* changed paramters to paramName from baseName

* change variables in apibody from baseName to paramName

* Skip test file generation (OpenAPITools#1459)

* skip test file generation

* skip overwriting CMakeLists.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants