Skip to content

Commit 037cb12

Browse files
authored
Tests for recent C fixes (#20200)
* [C] Add test schemas for the recent changes The recent commit 47665aa ("Fix a few issues with the C generator (part 1 version 2) (#14434)") didn't include any test schemas. Add them now, as requested: #14434 (comment) * Update samples * Fix sample update with missing files * More fixes for sample updates
1 parent f8ca36b commit 037cb12

File tree

26 files changed

+749
-6
lines changed

26 files changed

+749
-6
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ paths:
7575
- petstore_auth:
7676
- 'write:pets'
7777
- 'read:pets'
78+
/pet/specialty:
79+
get:
80+
tags:
81+
- pet
82+
summary: Specialty of the shop
83+
description: Returns the kind of pet the store specializes in
84+
operationId: specialtyPet
85+
produces:
86+
- application/xml
87+
- application/json
88+
parameters: []
89+
responses:
90+
'200':
91+
description: successful operation
92+
schema:
93+
$ref: '#/definitions/Preference'
94+
security:
95+
- api_key: []
7896
/pet/findByStatus:
7997
get:
8098
tags:
@@ -626,6 +644,17 @@ definitions:
626644
type: string
627645
xml:
628646
name: Category
647+
Preference:
648+
title: Pet preference
649+
description: A user's preference in pets
650+
type: string
651+
enum:
652+
- cats
653+
- dogs
654+
- birds
655+
- fish
656+
- snakes
657+
- other
629658
User:
630659
title: a User
631660
description: A User who is purchasing from the pet store
@@ -650,6 +679,12 @@ definitions:
650679
type: integer
651680
format: int32
652681
description: User Status
682+
extra:
683+
type: object
684+
nullable: true
685+
additionalProperties: true
686+
preference:
687+
$ref: '#/definitions/Preference'
653688
xml:
654689
name: User
655690
Tag:

samples/client/petstore/c-useJsonUnformatted/.openapi-generator/FILES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ docs/category.md
1616
docs/model_with_set_propertes.md
1717
docs/order.md
1818
docs/pet.md
19+
docs/preference.md
1920
docs/tag.md
2021
docs/user.md
2122
external/cJSON.c
@@ -41,6 +42,8 @@ model/order.c
4142
model/order.h
4243
model/pet.c
4344
model/pet.h
45+
model/preference.c
46+
model/preference.h
4447
model/tag.c
4548
model/tag.h
4649
model/user.c

samples/client/petstore/c-useJsonUnformatted/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set(SRCS
6060
model/model_with_set_propertes.c
6161
model/order.c
6262
model/pet.c
63+
model/preference.c
6364
model/tag.c
6465
model/user.c
6566
api/PetAPI.c
@@ -82,6 +83,7 @@ set(HDRS
8283
model/model_with_set_propertes.h
8384
model/order.h
8485
model/pet.h
86+
model/preference.h
8587
model/tag.h
8688
model/user.h
8789
api/PetAPI.h

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Category | Method | HTTP request | Description
7070
*PetAPI* | [**PetAPI_findPetsByStatus**](docs/PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status
7171
*PetAPI* | [**PetAPI_findPetsByTags**](docs/PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
7272
*PetAPI* | [**PetAPI_getPetById**](docs/PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
73+
*PetAPI* | [**PetAPI_specialtyPet**](docs/PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop
7374
*PetAPI* | [**PetAPI_updatePet**](docs/PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet
7475
*PetAPI* | [**PetAPI_updatePetWithForm**](docs/PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data
7576
*PetAPI* | [**PetAPI_uploadFile**](docs/PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image
@@ -96,6 +97,7 @@ Category | Method | HTTP request | Description
9697
- [model_with_set_propertes_t](docs/model_with_set_propertes.md)
9798
- [order_t](docs/order.md)
9899
- [pet_t](docs/pet.md)
100+
- [preference_t](docs/preference.md)
99101
- [tag_t](docs/tag.md)
100102
- [user_t](docs/user.md)
101103

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,70 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId)
460460

461461
}
462462

463+
// Specialty of the shop
464+
//
465+
// Returns the kind of pet the store specializes in
466+
//
467+
openapi_petstore_preference__e
468+
PetAPI_specialtyPet(apiClient_t *apiClient)
469+
{
470+
list_t *localVarQueryParameters = NULL;
471+
list_t *localVarHeaderParameters = NULL;
472+
list_t *localVarFormParameters = NULL;
473+
list_t *localVarHeaderType = list_createList();
474+
list_t *localVarContentType = NULL;
475+
char *localVarBodyParameters = NULL;
476+
477+
// create the path
478+
long sizeOfPath = strlen("/pet/specialty")+1;
479+
char *localVarPath = malloc(sizeOfPath);
480+
snprintf(localVarPath, sizeOfPath, "/pet/specialty");
481+
482+
483+
484+
list_addElement(localVarHeaderType,"application/xml"); //produces
485+
list_addElement(localVarHeaderType,"application/json"); //produces
486+
apiClient_invoke(apiClient,
487+
localVarPath,
488+
localVarQueryParameters,
489+
localVarHeaderParameters,
490+
localVarFormParameters,
491+
localVarHeaderType,
492+
localVarContentType,
493+
localVarBodyParameters,
494+
"GET");
495+
496+
// uncomment below to debug the error response
497+
//if (apiClient->response_code == 200) {
498+
// printf("%s\n","successful operation");
499+
//}
500+
//nonprimitive not container
501+
cJSON *PetAPIlocalVarJSON = cJSON_Parse(apiClient->dataReceived);
502+
openapi_petstore_preference__e elementToReturn = preference_parseFromJSON(PetAPIlocalVarJSON);
503+
cJSON_Delete(PetAPIlocalVarJSON);
504+
if(elementToReturn == 0) {
505+
// return 0;
506+
}
507+
508+
//return type
509+
if (apiClient->dataReceived) {
510+
free(apiClient->dataReceived);
511+
apiClient->dataReceived = NULL;
512+
apiClient->dataReceivedLen = 0;
513+
}
514+
515+
516+
517+
list_freeList(localVarHeaderType);
518+
519+
free(localVarPath);
520+
return elementToReturn;
521+
end:
522+
free(localVarPath);
523+
return 0;
524+
525+
}
526+
463527
// Update an existing pet
464528
//
465529
void

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../include/binary.h"
88
#include "../model/api_response.h"
99
#include "../model/pet.h"
10+
#include "../model/preference.h"
1011

1112
// Enum STATUS for PetAPI_findPetsByStatus
1213
typedef enum { openapi_petstore_findPetsByStatus_STATUS_NULL = 0, openapi_petstore_findPetsByStatus_STATUS_available, openapi_petstore_findPetsByStatus_STATUS_pending, openapi_petstore_findPetsByStatus_STATUS_sold } openapi_petstore_findPetsByStatus_status_e;
@@ -48,6 +49,14 @@ pet_t*
4849
PetAPI_getPetById(apiClient_t *apiClient, long petId);
4950

5051

52+
// Specialty of the shop
53+
//
54+
// Returns the kind of pet the store specializes in
55+
//
56+
openapi_petstore_preference__e
57+
PetAPI_specialtyPet(apiClient_t *apiClient);
58+
59+
5160
// Update an existing pet
5261
//
5362
void

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Method | HTTP request | Description
99
[**PetAPI_findPetsByStatus**](PetAPI.md#PetAPI_findPetsByStatus) | **GET** /pet/findByStatus | Finds Pets by status
1010
[**PetAPI_findPetsByTags**](PetAPI.md#PetAPI_findPetsByTags) | **GET** /pet/findByTags | Finds Pets by tags
1111
[**PetAPI_getPetById**](PetAPI.md#PetAPI_getPetById) | **GET** /pet/{petId} | Find pet by ID
12+
[**PetAPI_specialtyPet**](PetAPI.md#PetAPI_specialtyPet) | **GET** /pet/specialty | Specialty of the shop
1213
[**PetAPI_updatePet**](PetAPI.md#PetAPI_updatePet) | **PUT** /pet | Update an existing pet
1314
[**PetAPI_updatePetWithForm**](PetAPI.md#PetAPI_updatePetWithForm) | **POST** /pet/{petId} | Updates a pet in the store with form data
1415
[**PetAPI_uploadFile**](PetAPI.md#PetAPI_uploadFile) | **POST** /pet/{petId}/uploadImage | uploads an image
@@ -153,6 +154,36 @@ Name | Type | Description | Notes
153154
[pet_t](pet.md) *
154155
155156
157+
### Authorization
158+
159+
[api_key](../README.md#api_key)
160+
161+
### HTTP request headers
162+
163+
- **Content-Type**: Not defined
164+
- **Accept**: application/xml, application/json
165+
166+
[[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)
167+
168+
# **PetAPI_specialtyPet**
169+
```c
170+
// Specialty of the shop
171+
//
172+
// Returns the kind of pet the store specializes in
173+
//
174+
preference_t* PetAPI_specialtyPet(apiClient_t *apiClient);
175+
```
176+
177+
### Parameters
178+
Name | Type | Description | Notes
179+
------------- | ------------- | ------------- | -------------
180+
**apiClient** | **apiClient_t \*** | context containing the client configuration |
181+
182+
### Return type
183+
184+
[preference_t](preference.md) *
185+
186+
156187
### Authorization
157188

158189
[api_key](../README.md#api_key)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# preference_t
2+
3+
## Properties
4+
Name | Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
7+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
8+
9+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Name | Type | Description | Notes
1111
**password** | **char \*** | | [optional]
1212
**phone** | **char \*** | | [optional]
1313
**user_status** | **int** | User Status | [optional]
14+
**extra** | **list_t*** | | [optional]
15+
**preference** | **preference_t \*** | | [optional]
1416

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include "preference.h"
5+
6+
7+
char* preference_preference_ToString(openapi_petstore_preference__e preference) {
8+
char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" };
9+
return preferenceArray[preference];
10+
}
11+
12+
openapi_petstore_preference__e preference_preference_FromString(char* preference) {
13+
int stringToReturn = 0;
14+
char *preferenceArray[] = { "NULL", "cats", "dogs", "birds", "fish", "snakes", "other" };
15+
size_t sizeofArray = sizeof(preferenceArray) / sizeof(preferenceArray[0]);
16+
while(stringToReturn < sizeofArray) {
17+
if(strcmp(preference, preferenceArray[stringToReturn]) == 0) {
18+
return stringToReturn;
19+
}
20+
stringToReturn++;
21+
}
22+
return 0;
23+
}
24+
25+
cJSON *preference_convertToJSON(openapi_petstore_preference__e preference) {
26+
cJSON *item = cJSON_CreateObject();
27+
if(cJSON_AddStringToObject(item, "preference", preference_preference_ToString(preference)) == NULL) {
28+
goto fail;
29+
}
30+
return item;
31+
fail:
32+
cJSON_Delete(item);
33+
return NULL;
34+
}
35+
36+
openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON) {
37+
openapi_petstore_preference__e *preference = NULL;
38+
openapi_petstore_preference__e preferenceVariable;
39+
cJSON *preferenceVar = cJSON_GetObjectItemCaseSensitive(preferenceJSON, "preference");
40+
if(!cJSON_IsString(preferenceVar) || (preferenceVar->valuestring == NULL)){
41+
goto end;
42+
}
43+
preferenceVariable = preference_preference_FromString(preferenceVar->valuestring);
44+
return preferenceVariable;
45+
end:
46+
return 0;
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* preference.h
3+
*
4+
* A user&#39;s preference in pets
5+
*/
6+
7+
#ifndef _preference_H_
8+
#define _preference_H_
9+
10+
#include <string.h>
11+
#include "../external/cJSON.h"
12+
#include "../include/list.h"
13+
#include "../include/keyValuePair.h"
14+
#include "../include/binary.h"
15+
16+
typedef struct preference_t preference_t;
17+
18+
19+
// Enum for preference
20+
21+
typedef enum { openapi_petstore_preference__NULL = 0, openapi_petstore_preference__cats, openapi_petstore_preference__dogs, openapi_petstore_preference__birds, openapi_petstore_preference__fish, openapi_petstore_preference__snakes, openapi_petstore_preference__other } openapi_petstore_preference__e;
22+
23+
char* preference_preference_ToString(openapi_petstore_preference__e preference);
24+
25+
openapi_petstore_preference__e preference_preference_FromString(char* preference);
26+
27+
cJSON *preference_convertToJSON(openapi_petstore_preference__e preference);
28+
29+
openapi_petstore_preference__e preference_parseFromJSON(cJSON *preferenceJSON);
30+
31+
#endif /* _preference_H_ */
32+

0 commit comments

Comments
 (0)