add multi auth setting (based on #1961)

This commit is contained in:
wing328 2016-02-16 17:17:42 +08:00
parent 812ff2f2c5
commit a4825d8142
5 changed files with 124 additions and 30 deletions

View File

@ -618,9 +618,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object
// "there is a logical OR between the security requirements"
if (securities.size() > 1) {
LOGGER.warn("More than 1 security requirements are found, using only the first one");
// LOGGER.warn("More than 1 security requirements are found, using only the first one");
}
Map<String, List<String>> security = securities.get(0);
//Map<String, List<String>> security = securities.get(0);
for (Map<String, List<String>> security: securities) {
for (String securityName : security.keySet()) {
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
if (securityDefinition != null) {
@ -643,6 +644,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
}
}
}
}
if (!authMethods.isEmpty()) {
co.authMethods = config.fromSecurity(authMethods);
co.hasAuthMethods = true;

View File

@ -52,12 +52,23 @@ public class DefaultGeneratorTest {
gen.opts(clientOptInput);
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
CodegenSecurity apiKey, petstoreAuth;
CodegenSecurity cs, apiKey, petstoreAuth;
// security of "getPetById": api_key
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
assertEquals(getPetById.authMethods.size(), 1);
apiKey = getPetById.authMethods.iterator().next();
assertEquals(getPetById.authMethods.size(), 2);
cs = getPetById.authMethods.get(0);
if ("api_key".equals(cs.name)) {
apiKey = cs;
petstoreAuth = getPetById.authMethods.get(1);
} else {
petstoreAuth = cs;
apiKey = getPetById.authMethods.get(1);
}
assertEquals(petstoreAuth.name, "petstore_auth");
assertEquals(petstoreAuth.type, "oauth2");
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");
@ -88,8 +99,17 @@ public class DefaultGeneratorTest {
// security of "getPetById": api_key
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
assertEquals(getPetById.authMethods.size(), 1);
apiKey = getPetById.authMethods.iterator().next();
assertEquals(getPetById.authMethods.size(), 2);
cs = getPetById.authMethods.get(0);
if ("api_key".equals(cs.name)) {
apiKey = cs;
petstoreAuth = getPetById.authMethods.get(1);
} else {
petstoreAuth = cs;
apiKey = getPetById.authMethods.get(1);
}
assertEquals(petstoreAuth.type, "oauth2");
assertEquals(petstoreAuth.name, "petstore_auth");
assertEquals(apiKey.name, "api_key");
assertEquals(apiKey.type, "apiKey");

View File

@ -559,7 +559,13 @@
"400": {
"description": "Invalid Order"
}
},
"security": [
{
"test_api_client_id": [],
"test_api_client_secret": []
}
]
}
},
"/store/order/{orderId}": {
@ -596,7 +602,15 @@
"400": {
"description": "Invalid ID supplied"
}
},
"security": [
{
"test_api_key_header": []
},
{
"test_api_key_query": []
}
]
},
"delete": {
"tags": [
@ -915,6 +929,26 @@
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
},
"test_api_client_id": {
"type": "apiKey",
"name": "x-test_api_client_id",
"in": "header"
},
"test_api_client_secret": {
"type": "apiKey",
"name": "x-test_api_client_secret",
"in": "header"
},
"test_api_key_header": {
"type": "apiKey",
"name": "test_api_key_header",
"in": "header"
},
"test_api_key_query": {
"type": "apiKey",
"name": "test_api_key_query",
"in": "query"
}
},
"definitions": {

View File

@ -527,6 +527,11 @@ class PetApi
}
// this endpoint requires OAuth (access token)
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
}
// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
@ -949,6 +954,11 @@ class PetApi
}
// this endpoint requires OAuth (access token)
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
}
// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(

View File

@ -240,6 +240,20 @@ class StoreApi
$httpBody = $formParams; // for HTTP post (form)
}
// this endpoint requires API key authentication
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_id');
if (strlen($apiKey) !== 0) {
$headerParams['x-test_api_client_id'] = $apiKey;
}
// this endpoint requires API key authentication
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_secret');
if (strlen($apiKey) !== 0) {
$headerParams['x-test_api_client_secret'] = $apiKey;
}
// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
@ -335,6 +349,20 @@ class StoreApi
$httpBody = $formParams; // for HTTP post (form)
}
// this endpoint requires API key authentication
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_header');
if (strlen($apiKey) !== 0) {
$headerParams['test_api_key_header'] = $apiKey;
}
// this endpoint requires API key authentication
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_query');
if (strlen($apiKey) !== 0) {
$queryParams['test_api_key_query'] = $apiKey;
}
// make the API Call
try {
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(