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

Fix a few issues with the C generator (part 2) #20227

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/configs/c-bearer-auth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: c
outputDir: samples/client/others/c/bearerAuth
inputSpec: modules/openapi-generator/src/test/resources/3_0/c/bearer_auth.yaml
templateDir: modules/openapi-generator/src/main/resources/C-libcurl
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,18 @@ end:
}
{{/isArray}}
{{^isArray}}
{{#isString}}
localVarBodyParameters = strdup({{paramName}});
{{/isString}}
{{^isString}}
cJSON *localVarSingleItemJSON_{{paramName}} = NULL;
if ({{paramName}} != NULL)
{
//string
localVarSingleItemJSON_{{paramName}} = {{dataType}}_convertToJSON({{paramName}});
localVarBodyParameters = {{{cJSONPrint}}}(localVarSingleItemJSON_{{paramName}});
}
{{/isString}}
{{/isArray}}
{{/bodyParam}}
{{#produces}}
Expand Down Expand Up @@ -458,10 +463,12 @@ end:
free(localVarBodyParameters);
{{/isArray}}
{{^isArray}}
{{^isString}}
if (localVarSingleItemJSON_{{paramName}}) {
cJSON_Delete(localVarSingleItemJSON_{{paramName}});
localVarSingleItemJSON_{{paramName}} = NULL;
}
{{/isString}}
free(localVarBodyParameters);
{{/isArray}}
{{/bodyParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ apiClient_t *apiClient_create() {
apiClient->username = NULL;
apiClient->password = NULL;
{{/isBasicBasic}}
{{#isBasicBearer}}
apiClient->accessToken = NULL;
{{/isBasicBearer}}
{{#isOAuth}}
apiClient->accessToken = NULL;
{{/isOAuth}}
Expand Down Expand Up @@ -69,6 +72,9 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
apiClient->username = NULL;
apiClient->password = NULL;
{{/isBasicBasic}}
{{#isBasicBearer}}
apiClient->accessToken = NULL;
{{/isBasicBearer}}
{{#isOAuth}}
apiClient->accessToken = NULL;
{{/isOAuth}}
Expand Down Expand Up @@ -108,6 +114,11 @@ void apiClient_free(apiClient_t *apiClient) {
free(apiClient->password);
}
{{/isBasicBasic}}
{{#isBasicBearer}}
if(apiClient->accessToken) {
free(apiClient->accessToken);
}
{{/isBasicBearer}}
{{#isOAuth}}
if(apiClient->accessToken) {
free(apiClient->accessToken);
Expand Down Expand Up @@ -467,6 +478,20 @@ void apiClient_invoke(apiClient_t *apiClient,
}
}
{{/isApiKey}}
{{#isBasicBearer}}
// this would only be generated for bearer token authentication
if(apiClient->accessToken != NULL)
{
int authHeaderSize;
char *authHeader = NULL;

authHeaderSize = snprintf(NULL, 0, "Authorization: Bearer %s", apiClient->accessToken) + 1;
authHeader = malloc(authHeaderSize);
snprintf(authHeader, authHeaderSize, "Authorization: Bearer %s", apiClient->accessToken);
headers = curl_slist_append(headers, authHeader);
free(authHeader);
}
{{/isBasicBearer}}
{{/authMethods}}
{{/hasAuthMethods}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ typedef struct apiClient_t {
char *username;
char *password;
{{/isBasicBasic}}
{{#isBasicBearer}}
char *accessToken;
{{/isBasicBearer}}
{{#isOAuth}}
char *accessToken;
{{/isOAuth}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,20 @@ fail:
}

{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}_parseFromJSON(cJSON *{{classname}}JSON) {
{{projectName}}_{{classVarName}}_{{enumName}}_e *{{classname}} = NULL;
{{#isEnum}}
{{#isNumeric}}
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsNumber({{{classname}}}Var))
{
goto end;
if(!cJSON_IsNumber({{{classname}}}JSON)) {
return 0;
}
return {{classname}}JSON->valueint;
{{/isNumeric}}
{{#isString}}
{{projectName}}_{{classVarName}}_{{enumName}}_e {{classname}}Variable;
cJSON *{{{classname}}}Var = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{classname}}}");
if(!cJSON_IsString({{{classname}}}Var) || ({{{classname}}}Var->valuestring == NULL)){
goto end;
if(!cJSON_IsString({{{classname}}}JSON) || ({{{classname}}}JSON->valuestring == NULL)) {
return 0;
}
{{classname}}Variable = {{classFilename}}_{{classname}}_FromString({{{classname}}}Var->valuestring);
return {{classFilename}}_{{classname}}_FromString({{{classname}}}JSON->valuestring);
{{/isString}}
{{/isEnum}}
return {{classname}}Variable;
end:
return 0;
}
{{/isEnum}}
{{^isEnum}}
Expand Down Expand Up @@ -623,6 +616,9 @@ fail:
{{#vars}}
// {{{classname}}}->{{{name}}}
cJSON *{{{name}}} = cJSON_GetObjectItemCaseSensitive({{classname}}JSON, "{{{baseName}}}");
if (cJSON_IsNull({{{name}}})) {
{{{name}}} = NULL;
}
{{#required}}
if (!{{{name}}}) {
goto end;
Expand Down
51 changes: 51 additions & 0 deletions modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ paths:
- petstore_auth:
- 'write:pets'
- 'read:pets'
/pet/picture:
post:
tags:
- pet
summary: Send a picture of your happy pet
description: ''
operationId: sharePicture
parameters:
- in: body
name: picture
description: A picture you want to share
required: true
schema:
type: string
responses:
'200':
description: successful operation
schema:
type: string
description: Thank you message
'/pet/{petId}/uploadImage':
post:
tags:
Expand Down Expand Up @@ -290,6 +310,31 @@ paths:
- petstore_auth:
- 'write:pets'
- 'read:pets'
'/pet/{petId}/isAvailable':
post:
tags:
- pet
summary: Is this pet still available?
description: ''
operationId: isPetAvailable
produces:
- application/json
parameters:
- name: petId
in: path
description: ID of pet to check
required: true
type: integer
format: int64
responses:
'200':
description: successful operation
schema:
$ref: '#/definitions/Bit'
security:
- petstore_auth:
- 'write:pets'
- 'read:pets'
/store/inventory:
get:
tags:
Expand Down Expand Up @@ -775,3 +820,9 @@ definitions:
uniqueItems: true
items:
type: string
Bit:
description: bit value
type: number
enum:
- 0
- 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copied from k6
openapi: 3.0.0
info:
title: Sample API
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
version: 0.1.9
servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) server
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
security:
- bearerAuth: []
paths:
/users:
get:
summary: Returns a list of users.
description: Optional extended description in CommonMark or HTML.
responses:
"200": # status code
description: A JSON array of user names
content:
application/json:
schema:
type: array
items:
type: string
security:
- bearerAuth: []
/public:
get:
summary: Returns public information.
description: This endpoint does not require authentication.
responses:
"200":
description: A JSON object with public information
content:
application/json:
schema:
type: object
security: []
/private:
get:
summary: Returns private information.
description: This endpoint requires global security settings.
responses:
"200":
description: A JSON object with private information
content:
application/json:
schema:
type: object
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT # Optional: specify the format (e.g., JWT) if applicable
23 changes: 23 additions & 0 deletions samples/client/others/c/bearerAuth/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
22 changes: 22 additions & 0 deletions samples/client/others/c/bearerAuth/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CMakeLists.txt
Packing.cmake
README.md
api/DefaultAPI.c
api/DefaultAPI.h
docs/DefaultAPI.md
external/cJSON.c
external/cJSON.h
external/cJSON.licence
include/apiClient.h
include/binary.h
include/keyValuePair.h
include/list.h
libcurl.licence
model/any_type.h
model/object.c
model/object.h
src/apiClient.c
src/apiKey.c
src/binary.c
src/list.c
uncrustify-rules.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.11.0-SNAPSHOT
Loading
Loading