Skip to content

Commit 739d1ed

Browse files
authored
[cpp-qt-client]Fix variable redeclarations in the api-body.mustache (#22982)
* [cpp-qt-client]Fix variable redeclarations in the api-body.mustache * [cpp-qt-client]Utilize enum for OauthMethod flow * [cpp-qt-clien]Suffix token variables in OAuth with meaningful state names instead of numeric indexes
1 parent c073b14 commit 739d1ed

File tree

22 files changed

+283
-206
lines changed

22 files changed

+283
-206
lines changed

modules/openapi-generator/src/main/resources/cpp-qt-client/api-body.mustache

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -697,18 +697,18 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
697697
Q_EMIT allPendingRequestsCompleted();
698698
}
699699
});{{#authMethods}}{{#isOAuth}}{{#isCode}}
700-
_OauthMethod = 2;
700+
_OauthMethod = OauthMethod::AuthorizationFlow;
701701
_implicitFlow.unlink();
702702
_credentialFlow.unlink();
703703
_passwordFlow.unlink();
704704
_authFlow.link();
705-
QStringList scope;
705+
QStringList scopeAuthorizationFlow;
706706
{{#scopes}}
707-
scope.append("{{scope}}");
707+
scopeAuthorizationFlow.append("{{scope}}");
708708
{{/scopes}}
709-
auto token = _authFlow.getToken(scope.join(" "));
710-
if(token.isValid())
711-
input.headers.insert("Authorization", "Bearer " + token.getToken());
709+
auto tokenAuthorizationFlow = _authFlow.getToken(scopeAuthorizationFlow.join(" "));
710+
if(tokenAuthorizationFlow.isValid())
711+
input.headers.insert("Authorization", "Bearer " + tokenAuthorizationFlow.getToken());
712712

713713
_latestWorker = new {{prefix}}HttpRequestWorker(this, _manager);
714714
_latestWorker->setTimeOut(_timeOut);
@@ -725,20 +725,20 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
725725
});
726726

727727
_latestInput = input;
728-
_latestScope = scope;{{/isCode}}
728+
_latestScope = scopeAuthorizationFlow;{{/isCode}}
729729
{{#isImplicit}}
730-
_OauthMethod = 1;
730+
_OauthMethod = OauthMethod::ImplicitFlow;
731731
_implicitFlow.link();
732732
_passwordFlow.unlink();
733733
_authFlow.unlink();
734734
_credentialFlow.unlink();
735-
QStringList scope;
735+
QStringList scopeImplicitFlow;
736736
{{#scopes}}
737-
scope.append("{{scope}}");
737+
scopeImplicitFlow.append("{{scope}}");
738738
{{/scopes}}
739-
auto token = _implicitFlow.getToken(scope.join(" "));
740-
if(token.isValid())
741-
input.headers.insert("Authorization", "Bearer " + token.getToken());
739+
auto tokenImplicitFlow = _implicitFlow.getToken(scopeImplicitFlow.join(" "));
740+
if(tokenImplicitFlow.isValid())
741+
input.headers.insert("Authorization", "Bearer " + tokenImplicitFlow.getToken());
742742

743743
_latestWorker = new {{prefix}}HttpRequestWorker(this, _manager);
744744
_latestWorker->setTimeOut(_timeOut);
@@ -755,20 +755,20 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
755755
});
756756

757757
_latestInput = input;
758-
_latestScope = scope;{{/isImplicit}}
758+
_latestScope = scopeImplicitFlow;{{/isImplicit}}
759759
{{#isApplication}}
760-
_OauthMethod = 3;
760+
_OauthMethod = OauthMethod::ClientCredentialsFlow;
761761
_authFlow.unlink();
762762
_implicitFlow.unlink();
763763
_passwordFlow.unlink();
764764
_credentialFlow.link();
765-
QStringList scope;
765+
QStringList scopeClientCredentialsFlow;
766766
{{#scopes}}
767-
scope.append("{{scope}}");
767+
scopeClientCredentialsFlow.append("{{scope}}");
768768
{{/scopes}}
769-
auto token = _credentialFlow.getToken(scope.join(" "));
770-
if(token.isValid())
771-
input.headers.insert("Authorization", "Bearer " + token.getToken());
769+
auto tokenClientCredentialsFlow = _credentialFlow.getToken(scopeClientCredentialsFlow.join(" "));
770+
if(tokenClientCredentialsFlow.isValid())
771+
input.headers.insert("Authorization", "Bearer " + tokenClientCredentialsFlow.getToken());
772772

773773
_latestWorker = new {{prefix}}HttpRequestWorker(this, _manager);
774774
_latestWorker->setTimeOut(_timeOut);
@@ -785,20 +785,20 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
785785
});
786786

787787
_latestInput = input;
788-
_latestScope = scope;{{/isApplication}}
788+
_latestScope = scopeClientCredentialsFlow;{{/isApplication}}
789789
{{#isPassword}}
790-
_OauthMethod = 4;
790+
_OauthMethod = OauthMethod::ResourceOwnerPasswordFlow;
791791
_passwordFlow.link();
792792
_authFlow.unlink();
793793
_implicitFlow.unlink();
794794
_credentialFlow.unlink();
795-
QStringList scope;
795+
QStringList scopeResourceOwnerPasswordFlow;
796796
{{#scopes}}
797-
scope.append("{{scope}}");
797+
scopeResourceOwnerPasswordFlow.append("{{scope}}");
798798
{{/scopes}}
799-
auto token = _passwordFlow.getToken(scope.join(" "));
800-
if(token.isValid())
801-
input.headers.insert("Authorization", "Bearer " + token.getToken());
799+
auto tokenResourceOwnerPasswordFlow = _passwordFlow.getToken(scopeResourceOwnerPasswordFlow.join(" "));
800+
if(tokenResourceOwnerPasswordFlow.isValid())
801+
input.headers.insert("Authorization", "Bearer " + tokenResourceOwnerPasswordFlow.getToken());
802802

803803
_latestWorker = new {{prefix}}HttpRequestWorker(this, _manager);
804804
_latestWorker->setTimeOut(_timeOut);
@@ -815,7 +815,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
815815
});
816816

817817
_latestInput = input;
818-
_latestScope = scope;
818+
_latestScope = scopeResourceOwnerPasswordFlow;
819819
{{/isPassword}}{{/isOAuth}}{{/authMethods}}
820820

821821
worker->execute(&input);
@@ -934,11 +934,11 @@ void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
934934

935935
{{/operation}}
936936
{{/operations}}
937-
void {{classname}}::tokenAvailable(){
937+
void {{classname}}::tokenAvailable() {
938938
939939
oauthToken token;
940940
switch (_OauthMethod) {
941-
case 1: //implicit flow
941+
case OauthMethod::ImplicitFlow:
942942
token = _implicitFlow.getToken(_latestScope.join(" "));
943943
if(token.isValid()){
944944
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -948,7 +948,7 @@ void {{classname}}::tokenAvailable(){
948948
qDebug() << "Could not retrieve a valid token";
949949
}
950950
break;
951-
case 2: //authorization flow
951+
case OauthMethod::AuthorizationFlow:
952952
token = _authFlow.getToken(_latestScope.join(" "));
953953
if(token.isValid()){
954954
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -958,7 +958,7 @@ void {{classname}}::tokenAvailable(){
958958
qDebug() << "Could not retrieve a valid token";
959959
}
960960
break;
961-
case 3: //client credentials flow
961+
case OauthMethod::ClientCredentialsFlow:
962962
token = _credentialFlow.getToken(_latestScope.join(" "));
963963
if(token.isValid()){
964964
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -968,7 +968,7 @@ void {{classname}}::tokenAvailable(){
968968
qDebug() << "Could not retrieve a valid token";
969969
}
970970
break;
971-
case 4: //resource owner password flow
971+
case OauthMethod::ResourceOwnerPasswordFlow:
972972
token = _passwordFlow.getToken(_latestScope.join(" "));
973973
if(token.isValid()){
974974
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());

modules/openapi-generator/src/main/resources/cpp-qt-client/api-header.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public:
6363
{{/operation}}{{/operations}}
6464

6565
private:
66+
enum class OauthMethod : int {
67+
INVALID_VALUE_OPENAPI_GENERATED = 0,
68+
ImplicitFlow = 1,
69+
AuthorizationFlow = 2,
70+
ClientCredentialsFlow = 3,
71+
ResourceOwnerPasswordFlow = 4
72+
};
6673
QMap<QString,int> _serverIndices;
6774
QMap<QString,QList<{{prefix}}ServerConfiguration>> _serverConfigs;
6875
QMap<QString, QString> _apiKeys;
@@ -82,7 +89,7 @@ private:
8289
OauthImplicit _implicitFlow;
8390
OauthCredentials _credentialFlow;
8491
OauthPassword _passwordFlow;
85-
int _OauthMethod = 0;
92+
OauthMethod _OauthMethod = OauthMethod::INVALID_VALUE_OPENAPI_GENERATED;
8693
{{#operations}}{{#operation}}
8794
void {{nickname}}Callback({{prefix}}HttpRequestWorker *worker);{{/operation}}{{/operations}}
8895

samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXFakeApi.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,11 @@ void PFXFakeApi::getEnumInlineOrRefCallback(PFXHttpRequestWorker *worker) {
300300
}
301301
}
302302

303-
void PFXFakeApi::tokenAvailable(){
303+
void PFXFakeApi::tokenAvailable() {
304304

305305
oauthToken token;
306306
switch (_OauthMethod) {
307-
case 1: //implicit flow
307+
case OauthMethod::ImplicitFlow:
308308
token = _implicitFlow.getToken(_latestScope.join(" "));
309309
if(token.isValid()){
310310
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -314,7 +314,7 @@ void PFXFakeApi::tokenAvailable(){
314314
qDebug() << "Could not retrieve a valid token";
315315
}
316316
break;
317-
case 2: //authorization flow
317+
case OauthMethod::AuthorizationFlow:
318318
token = _authFlow.getToken(_latestScope.join(" "));
319319
if(token.isValid()){
320320
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -324,7 +324,7 @@ void PFXFakeApi::tokenAvailable(){
324324
qDebug() << "Could not retrieve a valid token";
325325
}
326326
break;
327-
case 3: //client credentials flow
327+
case OauthMethod::ClientCredentialsFlow:
328328
token = _credentialFlow.getToken(_latestScope.join(" "));
329329
if(token.isValid()){
330330
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -334,7 +334,7 @@ void PFXFakeApi::tokenAvailable(){
334334
qDebug() << "Could not retrieve a valid token";
335335
}
336336
break;
337-
case 4: //resource owner password flow
337+
case OauthMethod::ResourceOwnerPasswordFlow:
338338
token = _passwordFlow.getToken(_latestScope.join(" "));
339339
if(token.isValid()){
340340
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());

samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXFakeApi.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class PFXFakeApi : public QObject {
6565

6666

6767
private:
68+
enum class OauthMethod : int {
69+
INVALID_VALUE_OPENAPI_GENERATED = 0,
70+
ImplicitFlow = 1,
71+
AuthorizationFlow = 2,
72+
ClientCredentialsFlow = 3,
73+
ResourceOwnerPasswordFlow = 4
74+
};
6875
QMap<QString,int> _serverIndices;
6976
QMap<QString,QList<PFXServerConfiguration>> _serverConfigs;
7077
QMap<QString, QString> _apiKeys;
@@ -84,7 +91,7 @@ class PFXFakeApi : public QObject {
8491
OauthImplicit _implicitFlow;
8592
OauthCredentials _credentialFlow;
8693
OauthPassword _passwordFlow;
87-
int _OauthMethod = 0;
94+
OauthMethod _OauthMethod = OauthMethod::INVALID_VALUE_OPENAPI_GENERATED;
8895

8996
void getEnumInlineOrRefCallback(PFXHttpRequestWorker *worker);
9097

0 commit comments

Comments
 (0)