Skip to content

Commit e73143d

Browse files
authored
fix cast exception with uuid default (c) (#16449)
1 parent a0350c6 commit e73143d

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public String toDefaultValue(Schema p) {
426426
}
427427
} else if (ModelUtils.isStringSchema(p)) {
428428
if (p.getDefault() != null) {
429-
return "'" + escapeText((String) p.getDefault()) + "'";
429+
return "'" + escapeText(String.valueOf(p.getDefault())) + "'";
430430
}
431431
}
432432

@@ -444,7 +444,7 @@ public String toExampleValue(Schema schema) {
444444
}
445445
// correct "'"s into "'"s after toString()
446446
if (ModelUtils.isStringSchema(schema) && schema.getDefault() != null) {
447-
example = (String) schema.getDefault();
447+
example = String.valueOf(schema.getDefault());
448448
}
449449
if (StringUtils.isNotBlank(example) && !"null".equals(example)) {
450450
if (ModelUtils.isStringSchema(schema)) {

modules/openapi-generator/src/test/resources/2_0/c/petstore.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,7 @@ definitions:
722722
another_property:
723723
type: integer
724724
format: int32
725+
uuid_property:
726+
type: string
727+
format: uuid
728+
default: 1111-2222-3333-4444

samples/client/petstore/c/docs/MappedModel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Name | Type | Description | Notes
55
------------ | ------------- | ------------- | -------------
66
**another_property** | **int** | | [optional]
7+
**uuid_property** | **char \*** | | [optional]
78

89
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
910

samples/client/petstore/c/model/mapped_model.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77

88
MappedModel_t *MappedModel_create(
9-
int another_property
9+
int another_property,
10+
char *uuid_property
1011
) {
1112
MappedModel_t *MappedModel_local_var = malloc(sizeof(MappedModel_t));
1213
if (!MappedModel_local_var) {
1314
return NULL;
1415
}
1516
MappedModel_local_var->another_property = another_property;
17+
MappedModel_local_var->uuid_property = uuid_property;
1618

1719
return MappedModel_local_var;
1820
}
@@ -23,6 +25,10 @@ void MappedModel_free(MappedModel_t *MappedModel) {
2325
return ;
2426
}
2527
listEntry_t *listEntry;
28+
if (MappedModel->uuid_property) {
29+
free(MappedModel->uuid_property);
30+
MappedModel->uuid_property = NULL;
31+
}
2632
free(MappedModel);
2733
}
2834

@@ -36,6 +42,14 @@ cJSON *MappedModel_convertToJSON(MappedModel_t *MappedModel) {
3642
}
3743
}
3844

45+
46+
// MappedModel->uuid_property
47+
if(MappedModel->uuid_property) {
48+
if(cJSON_AddStringToObject(item, "uuid_property", MappedModel->uuid_property) == NULL) {
49+
goto fail; //String
50+
}
51+
}
52+
3953
return item;
4054
fail:
4155
if (item) {
@@ -57,9 +71,19 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
5771
}
5872
}
5973

74+
// MappedModel->uuid_property
75+
cJSON *uuid_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "uuid_property");
76+
if (uuid_property) {
77+
if(!cJSON_IsString(uuid_property) && !cJSON_IsNull(uuid_property))
78+
{
79+
goto end; //String
80+
}
81+
}
82+
6083

6184
MappedModel_local_var = MappedModel_create (
62-
another_property ? another_property->valuedouble : 0
85+
another_property ? another_property->valuedouble : 0,
86+
uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL
6387
);
6488

6589
return MappedModel_local_var;

samples/client/petstore/c/model/mapped_model.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ typedef struct MappedModel_t MappedModel_t;
2020

2121
typedef struct MappedModel_t {
2222
int another_property; //numeric
23+
char *uuid_property; // string
2324

2425
} MappedModel_t;
2526

2627
MappedModel_t *MappedModel_create(
27-
int another_property
28+
int another_property,
29+
char *uuid_property
2830
);
2931

3032
void MappedModel_free(MappedModel_t *MappedModel);

0 commit comments

Comments
 (0)