Skip to content

[C] Optimize memory usage when printing JSON #18072

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

Merged
merged 10 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion .github/workflows/samples-c-libcurl-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ on:
push:
paths:
- 'samples/client/petstore/c/**'
- 'samples/client/petstore/c-useJsonUnformatted/**'
pull_request:
paths:
- 'samples/client/petstore/c/**'
- 'samples/client/petstore/c-useJsonUnformatted/**'

jobs:
build:
name: Build c libcurl client
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sample:
- 'samples/client/petstore/c/'
- 'samples/client/petstore/c-useJsonUnformatted/'

steps:
- uses: actions/checkout@v4
- name: Prepare
run: |
sudo apt-get update
sudo apt-get install -y libssl-dev libcurl4-openssl-dev
- name: Build
working-directory: "samples/client/petstore/c"
working-directory: ${{ matrix.sample }}
run: |
mkdir build
cd build
Expand Down
9 changes: 9 additions & 0 deletions bin/configs/c-useJsonUnformatted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
generatorName: c
outputDir: samples/client/petstore/c-useJsonUnformatted
inputSpec: modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/C-libcurl
additionalProperties:
useJsonUnformatted: true
modelNameMappings:
another_model: MappedModel
another_property: mappedProperty
1 change: 1 addition & 0 deletions docs/generators/c.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|useJsonUnformatted|Use cJSON_PrintUnformatted instead of cJSON_Print when creating the JSON string.| |false|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConfig {
private final Logger LOGGER = LoggerFactory.getLogger(CLibcurlClientCodegen.class);

public static final String USE_JSON_UNFORMATTED = "useJsonUnformatted";
public static final String USE_JSON_UNFORMATTED_DESC = "Use cJSON_PrintUnformatted instead of cJSON_Print when creating the JSON string.";

public static final String PROJECT_NAME = "projectName";
protected String moduleName;
protected String projectName;
Expand All @@ -47,6 +50,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf
protected String libFolder = "lib";
protected String apiDocPath = "docs/";
protected String modelDocPath = "docs/";
protected boolean useJsonUnformatted = false;

protected static int emptyMethodNameCounter = 0;

Expand Down Expand Up @@ -306,6 +310,8 @@ public CLibcurlClientCodegen() {
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC).
defaultValue(Boolean.TRUE.toString()));

cliOptions.add(new CliOption(USE_JSON_UNFORMATTED, USE_JSON_UNFORMATTED_DESC).
defaultValue(Boolean.FALSE.toString()));
}

@Override
Expand All @@ -316,6 +322,15 @@ public void processOpts() {
LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg");
}

if (additionalProperties.containsKey(USE_JSON_UNFORMATTED)) {
useJsonUnformatted = Boolean.parseBoolean(additionalProperties.get(USE_JSON_UNFORMATTED).toString());
}
if (useJsonUnformatted) {
additionalProperties.put("cJSONPrint", "cJSON_PrintUnformatted");
} else {
additionalProperties.put("cJSONPrint", "cJSON_Print");
}

// make api and model doc path available in mustache template
additionalProperties.put("apiDocPath", apiDocPath);
additionalProperties.put("modelDocPath", modelDocPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ set(SRCS
model/object.c
{{#models}}
{{#model}}
model/{{classname}}.c
model/{{classFilename}}.c
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
api/{{classname}}.c
api/{{classFilename}}.c
{{/operations}}
{{/apis}}
{{/apiInfo}}
Expand All @@ -78,13 +78,13 @@ set(HDRS
model/object.h
{{#models}}
{{#model}}
model/{{classname}}.h
model/{{classFilename}}.h
{{/model}}
{{/models}}
{{#apiInfo}}
{{#apis}}
{{#operations}}
api/{{classname}}.h
api/{{classFilename}}.h
{{/operations}}
{{/apis}}
{{/apiInfo}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ end:
goto end;
}
cJSON_AddItemToArray(localVarSingleItemJSON_{{paramName}}, localVar_{{paramName}});
localVarBodyParameters = cJSON_Print(localVarItemJSON_{{paramName}});
localVarBodyParameters = {{{cJSONPrint}}}(localVarItemJSON_{{paramName}});
}
{{/isArray}}
{{^isArray}}
Expand All @@ -329,7 +329,7 @@ end:
{
//string
localVarSingleItemJSON_{{paramName}} = {{dataType}}_convertToJSON({{paramName}});
localVarBodyParameters = cJSON_Print(localVarSingleItemJSON_{{paramName}});
localVarBodyParameters = {{{cJSONPrint}}}(localVarSingleItemJSON_{{paramName}});
}
{{/isArray}}
{{/bodyParam}}
Expand Down Expand Up @@ -368,7 +368,7 @@ end:
cJSON *{{{paramName}}}VarJSON;
list_t *elementToReturn = list_createList();
cJSON_ArrayForEach({{{paramName}}}VarJSON, {{paramName}}localVarJSON){
keyValuePair_t *keyPair = keyValuePair_create(strdup({{{paramName}}}VarJSON->string), cJSON_Print({{{paramName}}}VarJSON));
keyValuePair_t *keyPair = keyValuePair_create(strdup({{{paramName}}}VarJSON->string), {{{cJSONPrint}}}({{{paramName}}}VarJSON));
list_addElement(elementToReturn, keyPair);
}
cJSON_Delete({{paramName}}localVarJSON);
Expand All @@ -389,7 +389,7 @@ end:
{
// return 0;
}
char *localVarJSONToChar = cJSON_Print({{{paramName}}}VarJSON);
char *localVarJSONToChar = {{{cJSONPrint}}}({{{paramName}}}VarJSON);
list_addElement(elementToReturn , localVarJSONToChar);
}

Expand Down

This file was deleted.

This file was deleted.

Loading