Skip to content

Commit 48648a4

Browse files
authored
fix(c): Remove duplicate code generation for UUID path parameters (#22285)
* fix(c): Remove duplicate code generation for UUID path parameters The C generator was generating duplicate path parameter handling code when a parameter had format: uuid. This occurred because UUID parameters have both isString=true and isUuid=true flags set, causing both the {{#isString}} and {{#isUuid}} template blocks to execute. The duplicate {{#isUuid}} block has been removed since it generated identical code to the {{#isString}} block. This makes UUID parameters consistent with email parameters, which already work this way (isEmail=true and isString=true, but only use the {{#isString}} block). The generated code now compiles successfully without duplicate variable declarations. * test(c): Add UUID path parameter test case to petstore Adds endpoint with UUID path parameter to verify C generator produces compilable code without duplicate variable declarations. * chore(samples): Regenerate all C samples with template fix Regenerated all C sample variants to include the UUID path parameter example.
1 parent a6c753e commit 48648a4

File tree

10 files changed

+294
-9
lines changed

10 files changed

+294
-9
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,6 @@ end:
188188

189189
localVarPath = strReplace(localVarPath, localVarToReplace_{{paramName}}, {{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}});
190190
{{/isString}}
191-
{{#isUuid}}
192-
if({{paramName}} == NULL) {
193-
goto end;
194-
}
195-
char* localVarToReplace_{{paramName}} = malloc(sizeOfPathParams_{{paramName}});
196-
sprintf(localVarToReplace_{{paramName}}, "{%s}", "{{baseName}}");
197-
198-
localVarPath = strReplace(localVarPath, localVarToReplace_{{paramName}}, {{paramName}});
199-
{{/isUuid}}
200191
{{/pathParams}}
201192

202193

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,34 @@ paths:
253253
- petstore_auth:
254254
- 'write:pets'
255255
- 'read:pets'
256+
'/pet/byUuid/{uuid}':
257+
get:
258+
tags:
259+
- pet
260+
summary: Find pet by UUID
261+
description: Returns a single pet identified by UUID
262+
operationId: getPetByUuid
263+
produces:
264+
- application/xml
265+
- application/json
266+
parameters:
267+
- name: uuid
268+
in: path
269+
description: UUID of pet to return
270+
required: true
271+
type: string
272+
format: uuid
273+
responses:
274+
'200':
275+
description: successful operation
276+
schema:
277+
$ref: '#/definitions/Pet'
278+
'400':
279+
description: Invalid UUID supplied
280+
'404':
281+
description: Pet not found
282+
security:
283+
- api_key: []
256284
/pet/picture:
257285
get:
258286
tags:

samples/client/petstore/c-useJsonUnformatted/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Category | Method | HTTP request | Description
7171
*PetAPI* | [**PetAPI_findPetsByTags**](docs/PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
7272
*PetAPI* | [**PetAPI_getDaysWithoutIncident**](docs/PetAPI.md#PetAPI_getDaysWithoutIncident) | **GET** /store/daysWithoutIncident | Number of days since the last time a pet maimed someone at the store
7373
*PetAPI* | [**PetAPI_getPetById**](docs/PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
74+
*PetAPI* | [**PetAPI_getPetByUuid**](docs/PetAPI.md#PetAPI_getPetByUuid) | **GET** /pet/byUuid/{uuid} | Find pet by UUID
7475
*PetAPI* | [**PetAPI_getPicture**](docs/PetAPI.md#PetAPI_getPicture) | **GET** /pet/picture | Get a random picture of someone else's pet
7576
*PetAPI* | [**PetAPI_isPetAvailable**](docs/PetAPI.md#PetAPI_isPetAvailable) | **POST** /pet/{petId}/isAvailable | Is this pet still available?
7677
*PetAPI* | [**PetAPI_sharePicture**](docs/PetAPI.md#PetAPI_sharePicture) | **POST** /pet/picture | Send a picture of your happy pet

samples/client/petstore/c-useJsonUnformatted/api/PetAPI.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,98 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
548548

549549
}
550550

551+
// Find pet by UUID
552+
//
553+
// Returns a single pet identified by UUID
554+
//
555+
pet_t*
556+
PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid)
557+
{
558+
list_t *localVarQueryParameters = NULL;
559+
list_t *localVarHeaderParameters = NULL;
560+
list_t *localVarFormParameters = NULL;
561+
list_t *localVarHeaderType = list_createList();
562+
list_t *localVarContentType = NULL;
563+
char *localVarBodyParameters = NULL;
564+
size_t localVarBodyLength = 0;
565+
566+
// clear the error code from the previous api call
567+
apiClient->response_code = 0;
568+
569+
// create the path
570+
char *localVarPath = strdup("/pet/byUuid/{uuid}");
571+
572+
if(!uuid)
573+
goto end;
574+
575+
576+
// Path Params
577+
long sizeOfPathParams_uuid = strlen(uuid)+3 + sizeof("{ uuid }") - 1;
578+
if(uuid == NULL) {
579+
goto end;
580+
}
581+
char* localVarToReplace_uuid = malloc(sizeOfPathParams_uuid);
582+
sprintf(localVarToReplace_uuid, "{%s}", "uuid");
583+
584+
localVarPath = strReplace(localVarPath, localVarToReplace_uuid, uuid);
585+
586+
587+
list_addElement(localVarHeaderType,"application/xml"); //produces
588+
list_addElement(localVarHeaderType,"application/json"); //produces
589+
apiClient_invoke(apiClient,
590+
localVarPath,
591+
localVarQueryParameters,
592+
localVarHeaderParameters,
593+
localVarFormParameters,
594+
localVarHeaderType,
595+
localVarContentType,
596+
localVarBodyParameters,
597+
localVarBodyLength,
598+
"GET");
599+
600+
// uncomment below to debug the error response
601+
//if (apiClient->response_code == 200) {
602+
// printf("%s\n","successful operation");
603+
//}
604+
// uncomment below to debug the error response
605+
//if (apiClient->response_code == 400) {
606+
// printf("%s\n","Invalid UUID supplied");
607+
//}
608+
// uncomment below to debug the error response
609+
//if (apiClient->response_code == 404) {
610+
// printf("%s\n","Pet not found");
611+
//}
612+
//nonprimitive not container
613+
pet_t *elementToReturn = NULL;
614+
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
615+
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
616+
elementToReturn = pet_parseFromJSON(PetAPIlocalVarJSON);
617+
cJSON_Delete(PetAPIlocalVarJSON);
618+
if(elementToReturn == NULL) {
619+
// return 0;
620+
}
621+
}
622+
623+
//return type
624+
if (apiClient->dataReceived) {
625+
free(apiClient->dataReceived);
626+
apiClient->dataReceived = NULL;
627+
apiClient->dataReceivedLen = 0;
628+
}
629+
630+
631+
632+
list_freeList(localVarHeaderType);
633+
634+
free(localVarPath);
635+
free(localVarToReplace_uuid);
636+
return elementToReturn;
637+
end:
638+
free(localVarPath);
639+
return NULL;
640+
641+
}
642+
551643
// Get a random picture of someone else's pet
552644
//
553645
binary_t*

samples/client/petstore/c-useJsonUnformatted/api/PetAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ pet_t*
5656
PetAPI_getPetById(apiClient_t *apiClient, long petId);
5757

5858

59+
// Find pet by UUID
60+
//
61+
// Returns a single pet identified by UUID
62+
//
63+
pet_t*
64+
PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid);
65+
66+
5967
// Get a random picture of someone else's pet
6068
//
6169
binary_t*

samples/client/petstore/c-useJsonUnformatted/docs/PetAPI.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Method | HTTP request | Description
1010
[**PetAPI_findPetsByTags**](PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
1111
[**PetAPI_getDaysWithoutIncident**](PetAPI.md#PetAPI_getDaysWithoutIncident) | **GET** /store/daysWithoutIncident | Number of days since the last time a pet maimed someone at the store
1212
[**PetAPI_getPetById**](PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
13+
[**PetAPI_getPetByUuid**](PetAPI.md#PetAPI_getPetByUuid) | **GET** /pet/byUuid/{uuid} | Find pet by UUID
1314
[**PetAPI_getPicture**](PetAPI.md#PetAPI_getPicture) | **GET** /pet/picture | Get a random picture of someone else&#39;s pet
1415
[**PetAPI_isPetAvailable**](PetAPI.md#PetAPI_isPetAvailable) | **POST** /pet/{petId}/isAvailable | Is this pet still available?
1516
[**PetAPI_sharePicture**](PetAPI.md#PetAPI_sharePicture) | **POST** /pet/picture | Send a picture of your happy pet
@@ -187,6 +188,37 @@ Name | Type | Description | Notes
187188
[pet_t](pet.md) *
188189

189190

191+
### Authorization
192+
193+
[api_key](../README.md#api_key)
194+
195+
### HTTP request headers
196+
197+
- **Content-Type**: Not defined
198+
- **Accept**: application/xml, application/json
199+
200+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
201+
202+
# **PetAPI_getPetByUuid**
203+
```c
204+
// Find pet by UUID
205+
//
206+
// Returns a single pet identified by UUID
207+
//
208+
pet_t* PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid);
209+
```
210+
211+
### Parameters
212+
Name | Type | Description | Notes
213+
------------- | ------------- | ------------- | -------------
214+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
215+
**uuid** | **char \*** | UUID of pet to return |
216+
217+
### Return type
218+
219+
[pet_t](pet.md) *
220+
221+
190222
### Authorization
191223
192224
[api_key](../README.md#api_key)

samples/client/petstore/c/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Category | Method | HTTP request | Description
7171
*PetAPI* | [**PetAPI_findPetsByTags**](docs/PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
7272
*PetAPI* | [**PetAPI_getDaysWithoutIncident**](docs/PetAPI.md#PetAPI_getDaysWithoutIncident) | **GET** /store/daysWithoutIncident | Number of days since the last time a pet maimed someone at the store
7373
*PetAPI* | [**PetAPI_getPetById**](docs/PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
74+
*PetAPI* | [**PetAPI_getPetByUuid**](docs/PetAPI.md#PetAPI_getPetByUuid) | **GET** /pet/byUuid/{uuid} | Find pet by UUID
7475
*PetAPI* | [**PetAPI_getPicture**](docs/PetAPI.md#PetAPI_getPicture) | **GET** /pet/picture | Get a random picture of someone else's pet
7576
*PetAPI* | [**PetAPI_isPetAvailable**](docs/PetAPI.md#PetAPI_isPetAvailable) | **POST** /pet/{petId}/isAvailable | Is this pet still available?
7677
*PetAPI* | [**PetAPI_sharePicture**](docs/PetAPI.md#PetAPI_sharePicture) | **POST** /pet/picture | Send a picture of your happy pet

samples/client/petstore/c/api/PetAPI.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,98 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
548548

549549
}
550550

551+
// Find pet by UUID
552+
//
553+
// Returns a single pet identified by UUID
554+
//
555+
pet_t*
556+
PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid)
557+
{
558+
list_t *localVarQueryParameters = NULL;
559+
list_t *localVarHeaderParameters = NULL;
560+
list_t *localVarFormParameters = NULL;
561+
list_t *localVarHeaderType = list_createList();
562+
list_t *localVarContentType = NULL;
563+
char *localVarBodyParameters = NULL;
564+
size_t localVarBodyLength = 0;
565+
566+
// clear the error code from the previous api call
567+
apiClient->response_code = 0;
568+
569+
// create the path
570+
char *localVarPath = strdup("/pet/byUuid/{uuid}");
571+
572+
if(!uuid)
573+
goto end;
574+
575+
576+
// Path Params
577+
long sizeOfPathParams_uuid = strlen(uuid)+3 + sizeof("{ uuid }") - 1;
578+
if(uuid == NULL) {
579+
goto end;
580+
}
581+
char* localVarToReplace_uuid = malloc(sizeOfPathParams_uuid);
582+
sprintf(localVarToReplace_uuid, "{%s}", "uuid");
583+
584+
localVarPath = strReplace(localVarPath, localVarToReplace_uuid, uuid);
585+
586+
587+
list_addElement(localVarHeaderType,"application/xml"); //produces
588+
list_addElement(localVarHeaderType,"application/json"); //produces
589+
apiClient_invoke(apiClient,
590+
localVarPath,
591+
localVarQueryParameters,
592+
localVarHeaderParameters,
593+
localVarFormParameters,
594+
localVarHeaderType,
595+
localVarContentType,
596+
localVarBodyParameters,
597+
localVarBodyLength,
598+
"GET");
599+
600+
// uncomment below to debug the error response
601+
//if (apiClient->response_code == 200) {
602+
// printf("%s\n","successful operation");
603+
//}
604+
// uncomment below to debug the error response
605+
//if (apiClient->response_code == 400) {
606+
// printf("%s\n","Invalid UUID supplied");
607+
//}
608+
// uncomment below to debug the error response
609+
//if (apiClient->response_code == 404) {
610+
// printf("%s\n","Pet not found");
611+
//}
612+
//nonprimitive not container
613+
pet_t *elementToReturn = NULL;
614+
if(apiClient->response_code >= 200 && apiClient->response_code < 300) {
615+
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
616+
elementToReturn = pet_parseFromJSON(PetAPIlocalVarJSON);
617+
cJSON_Delete(PetAPIlocalVarJSON);
618+
if(elementToReturn == NULL) {
619+
// return 0;
620+
}
621+
}
622+
623+
//return type
624+
if (apiClient->dataReceived) {
625+
free(apiClient->dataReceived);
626+
apiClient->dataReceived = NULL;
627+
apiClient->dataReceivedLen = 0;
628+
}
629+
630+
631+
632+
list_freeList(localVarHeaderType);
633+
634+
free(localVarPath);
635+
free(localVarToReplace_uuid);
636+
return elementToReturn;
637+
end:
638+
free(localVarPath);
639+
return NULL;
640+
641+
}
642+
551643
// Get a random picture of someone else's pet
552644
//
553645
binary_t*

samples/client/petstore/c/api/PetAPI.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ pet_t*
5656
PetAPI_getPetById(apiClient_t *apiClient, long petId);
5757

5858

59+
// Find pet by UUID
60+
//
61+
// Returns a single pet identified by UUID
62+
//
63+
pet_t*
64+
PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid);
65+
66+
5967
// Get a random picture of someone else's pet
6068
//
6169
binary_t*

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Method | HTTP request | Description
1010
[**PetAPI_findPetsByTags**](PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
1111
[**PetAPI_getDaysWithoutIncident**](PetAPI.md#PetAPI_getDaysWithoutIncident) | **GET** /store/daysWithoutIncident | Number of days since the last time a pet maimed someone at the store
1212
[**PetAPI_getPetById**](PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
13+
[**PetAPI_getPetByUuid**](PetAPI.md#PetAPI_getPetByUuid) | **GET** /pet/byUuid/{uuid} | Find pet by UUID
1314
[**PetAPI_getPicture**](PetAPI.md#PetAPI_getPicture) | **GET** /pet/picture | Get a random picture of someone else&#39;s pet
1415
[**PetAPI_isPetAvailable**](PetAPI.md#PetAPI_isPetAvailable) | **POST** /pet/{petId}/isAvailable | Is this pet still available?
1516
[**PetAPI_sharePicture**](PetAPI.md#PetAPI_sharePicture) | **POST** /pet/picture | Send a picture of your happy pet
@@ -187,6 +188,37 @@ Name | Type | Description | Notes
187188
[pet_t](pet.md) *
188189

189190

191+
### Authorization
192+
193+
[api_key](../README.md#api_key)
194+
195+
### HTTP request headers
196+
197+
- **Content-Type**: Not defined
198+
- **Accept**: application/xml, application/json
199+
200+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
201+
202+
# **PetAPI_getPetByUuid**
203+
```c
204+
// Find pet by UUID
205+
//
206+
// Returns a single pet identified by UUID
207+
//
208+
pet_t* PetAPI_getPetByUuid(apiClient_t *apiClient, char *uuid);
209+
```
210+
211+
### Parameters
212+
Name | Type | Description | Notes
213+
------------- | ------------- | ------------- | -------------
214+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
215+
**uuid** | **char \*** | UUID of pet to return |
216+
217+
### Return type
218+
219+
[pet_t](pet.md) *
220+
221+
190222
### Authorization
191223
192224
[api_key](../README.md#api_key)

0 commit comments

Comments
 (0)