Skip to content

Commit c4d799a

Browse files
committed
Merge pull request #2155 from wing328/multi_auth_setting
Add support for multi-authentication setting
2 parents 5678939 + 4aa0dc5 commit c4d799a

File tree

5 files changed

+122
-36
lines changed

5 files changed

+122
-36
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java

+23-29
Original file line numberDiff line numberDiff line change
@@ -613,35 +613,29 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
613613
continue;
614614
}
615615
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
616-
// NOTE: Use only the first security requirement for now.
617-
// See the "security" field of "Swagger Object":
618-
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object
619-
// "there is a logical OR between the security requirements"
620-
if (securities.size() > 1) {
621-
LOGGER.warn("More than 1 security requirements are found, using only the first one");
622-
}
623-
Map<String, List<String>> security = securities.get(0);
624-
for (String securityName : security.keySet()) {
625-
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
626-
if (securityDefinition != null) {
627-
if(securityDefinition instanceof OAuth2Definition) {
628-
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
629-
OAuth2Definition oauth2Operation = new OAuth2Definition();
630-
oauth2Operation.setType(oauth2Definition.getType());
631-
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
632-
oauth2Operation.setFlow(oauth2Definition.getFlow());
633-
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
634-
oauth2Operation.setScopes(new HashMap<String, String>());
635-
for (String scope : security.get(securityName)) {
636-
if (oauth2Definition.getScopes().containsKey(scope)) {
637-
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
638-
}
639-
}
640-
authMethods.put(securityName, oauth2Operation);
641-
} else {
642-
authMethods.put(securityName, securityDefinition);
643-
}
644-
}
616+
for (Map<String, List<String>> security: securities) {
617+
for (String securityName : security.keySet()) {
618+
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
619+
if (securityDefinition != null) {
620+
if(securityDefinition instanceof OAuth2Definition) {
621+
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
622+
OAuth2Definition oauth2Operation = new OAuth2Definition();
623+
oauth2Operation.setType(oauth2Definition.getType());
624+
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
625+
oauth2Operation.setFlow(oauth2Definition.getFlow());
626+
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
627+
oauth2Operation.setScopes(new HashMap<String, String>());
628+
for (String scope : security.get(securityName)) {
629+
if (oauth2Definition.getScopes().containsKey(scope)) {
630+
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
631+
}
632+
}
633+
authMethods.put(securityName, oauth2Operation);
634+
} else {
635+
authMethods.put(securityName, securityDefinition);
636+
}
637+
}
638+
}
645639
}
646640
if (!authMethods.isEmpty()) {
647641
co.authMethods = config.fromSecurity(authMethods);

modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ public void testSecurityWithoutGlobal() throws Exception {
5252
gen.opts(clientOptInput);
5353
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
5454

55-
CodegenSecurity apiKey, petstoreAuth;
55+
CodegenSecurity cs, apiKey, petstoreAuth;
5656

5757
// security of "getPetById": api_key
5858
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
59-
assertEquals(getPetById.authMethods.size(), 1);
60-
apiKey = getPetById.authMethods.iterator().next();
59+
assertEquals(getPetById.authMethods.size(), 2);
60+
cs = getPetById.authMethods.get(0);
61+
if ("api_key".equals(cs.name)) {
62+
apiKey = cs;
63+
petstoreAuth = getPetById.authMethods.get(1);
64+
} else {
65+
petstoreAuth = cs;
66+
apiKey = getPetById.authMethods.get(1);
67+
}
68+
assertEquals(petstoreAuth.name, "petstore_auth");
69+
assertEquals(petstoreAuth.type, "oauth2");
70+
71+
6172
assertEquals(apiKey.name, "api_key");
6273
assertEquals(apiKey.type, "apiKey");
6374

@@ -88,8 +99,17 @@ public void testSecurityWithGlobal() throws Exception {
8899

89100
// security of "getPetById": api_key
90101
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
91-
assertEquals(getPetById.authMethods.size(), 1);
92-
apiKey = getPetById.authMethods.iterator().next();
102+
assertEquals(getPetById.authMethods.size(), 2);
103+
cs = getPetById.authMethods.get(0);
104+
if ("api_key".equals(cs.name)) {
105+
apiKey = cs;
106+
petstoreAuth = getPetById.authMethods.get(1);
107+
} else {
108+
petstoreAuth = cs;
109+
apiKey = getPetById.authMethods.get(1);
110+
}
111+
assertEquals(petstoreAuth.type, "oauth2");
112+
assertEquals(petstoreAuth.name, "petstore_auth");
93113
assertEquals(apiKey.name, "api_key");
94114
assertEquals(apiKey.type, "apiKey");
95115

modules/swagger-codegen/src/test/resources/2_0/petstore.json

+36-2
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,13 @@
559559
"400": {
560560
"description": "Invalid Order"
561561
}
562-
}
562+
},
563+
"security": [
564+
{
565+
"test_api_client_id": [],
566+
"test_api_client_secret": []
567+
}
568+
]
563569
}
564570
},
565571
"/store/order/{orderId}": {
@@ -596,7 +602,15 @@
596602
"400": {
597603
"description": "Invalid ID supplied"
598604
}
599-
}
605+
},
606+
"security": [
607+
{
608+
"test_api_key_header": []
609+
},
610+
{
611+
"test_api_key_query": []
612+
}
613+
]
600614
},
601615
"delete": {
602616
"tags": [
@@ -915,6 +929,26 @@
915929
"write:pets": "modify pets in your account",
916930
"read:pets": "read your pets"
917931
}
932+
},
933+
"test_api_client_id": {
934+
"type": "apiKey",
935+
"name": "x-test_api_client_id",
936+
"in": "header"
937+
},
938+
"test_api_client_secret": {
939+
"type": "apiKey",
940+
"name": "x-test_api_client_secret",
941+
"in": "header"
942+
},
943+
"test_api_key_header": {
944+
"type": "apiKey",
945+
"name": "test_api_key_header",
946+
"in": "header"
947+
},
948+
"test_api_key_query": {
949+
"type": "apiKey",
950+
"name": "test_api_key_query",
951+
"in": "query"
918952
}
919953
},
920954
"definitions": {

samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php

+10
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ public function getPetByIdWithHttpInfo($pet_id)
527527
}
528528

529529

530+
// this endpoint requires OAuth (access token)
531+
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
532+
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
533+
}
534+
530535
// make the API Call
531536
try {
532537
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
@@ -949,6 +954,11 @@ public function getPetByIdWithByteArrayWithHttpInfo($pet_id)
949954
}
950955

951956

957+
// this endpoint requires OAuth (access token)
958+
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
959+
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
960+
}
961+
952962
// make the API Call
953963
try {
954964
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(

samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php

+28
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ public function placeOrderWithHttpInfo($body = null)
240240
$httpBody = $formParams; // for HTTP post (form)
241241
}
242242

243+
// this endpoint requires API key authentication
244+
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_id');
245+
if (strlen($apiKey) !== 0) {
246+
$headerParams['x-test_api_client_id'] = $apiKey;
247+
}
248+
249+
250+
// this endpoint requires API key authentication
251+
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_secret');
252+
if (strlen($apiKey) !== 0) {
253+
$headerParams['x-test_api_client_secret'] = $apiKey;
254+
}
255+
256+
243257
// make the API Call
244258
try {
245259
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
@@ -335,6 +349,20 @@ public function getOrderByIdWithHttpInfo($order_id)
335349
$httpBody = $formParams; // for HTTP post (form)
336350
}
337351

352+
// this endpoint requires API key authentication
353+
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_header');
354+
if (strlen($apiKey) !== 0) {
355+
$headerParams['test_api_key_header'] = $apiKey;
356+
}
357+
358+
359+
// this endpoint requires API key authentication
360+
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_query');
361+
if (strlen($apiKey) !== 0) {
362+
$queryParams['test_api_key_query'] = $apiKey;
363+
}
364+
365+
338366
// make the API Call
339367
try {
340368
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(

0 commit comments

Comments
 (0)