[Java] [Kotlin] ignore unsupported schemes instead of throwing exception (#15817)

This commit is contained in:
Tiffany Marrel 2023-06-15 10:50:49 +02:00 committed by GitHub
parent 882b9a8c50
commit 0e212f53f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 112 additions and 75 deletions

View File

@ -94,21 +94,14 @@ public class ApiClient {
for(String authName : authNames) {
log.log(Level.FINE, "Creating authentication {0}", authName);
{{#hasAuthMethods}}
RequestInterceptor auth;
RequestInterceptor auth = null;
{{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}}
{{#isBasicBasic}}
auth = new HttpBasicAuth();
{{/isBasicBasic}}
{{^isBasicBasic}}
{{#isBasicBearer}}
auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBearer}}
{{^isBasicBearer}}
throw new RuntimeException("auth name \"" + authName + "\" does not have a supported http scheme type");
{{/isBasicBearer}}
{{/isBasicBasic}}
{{/isBasic}}
{{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}");
{{/isApiKey}}
@ -118,7 +111,9 @@ public class ApiClient {
} else {{/authMethods}}{
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
{{/hasAuthMethods}}
{{^hasAuthMethods}}
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");

View File

@ -59,21 +59,14 @@ public class ApiClient {
this();
for(String authName : authNames) {
{{#hasAuthMethods}}
Interceptor auth;
Interceptor auth = null;
{{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}}
{{#isBasicBasic}}
auth = new HttpBasicAuth();
{{/isBasicBasic}}
{{^isBasicBasic}}
{{#isBasicBearer}}
auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBearer}}
{{^isBasicBearer}}
throw new RuntimeException("auth name \"" + authName + "\" does not have a supported http scheme type");
{{/isBasicBearer}}
{{/isBasicBasic}}
{{/isBasic}}
{{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}");
{{/isApiKey}}
@ -83,7 +76,9 @@ public class ApiClient {
} else {{/authMethods}}{
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
{{/hasAuthMethods}}
{{^hasAuthMethods}}
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");

View File

@ -66,15 +66,13 @@ public class ApiClient {
this();
for(String authName : authNames) {
{{#hasAuthMethods}}
Interceptor auth;
Interceptor auth = null;
{{#authMethods}}if ("{{name}}".equals(authName)) {
{{#isBasic}}{{#isBasicBasic}}
{{#isBasicBasic}}
auth = new HttpBasicAuth();
{{/isBasicBasic}}{{^isBasicBasic}}{{#isBasicBearer}}
{{/isBasicBasic}}{{#isBasicBearer}}
auth = new HttpBearerAuth("{{scheme}}");
{{/isBasicBearer}}{{^isBasicBearer}}
throw new RuntimeException("auth name \"" + authName + "\" does not have a supported http scheme type");
{{/isBasicBearer}}{{/isBasicBasic}}{{/isBasic}}
{{/isBasicBearer}}
{{#isApiKey}}
auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}");
{{/isApiKey}}
@ -84,8 +82,9 @@ public class ApiClient {
} else {{/authMethods}}{
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
{{/hasAuthMethods}}
{{^hasAuthMethods}}
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");

View File

@ -127,13 +127,19 @@ import okhttp3.MediaType.Companion.toMediaType
authNames: Array<String>
) : this(baseUrl, okHttpClientBuilder{{^kotlinx_serialization}}, serializerBuilder{{/kotlinx_serialization}}) {
authNames.forEach { authName ->
val auth = when (authName) {
{{#authMethods}}"{{name}}" -> {{#isBasic}}{{#isBasicBasic}}HttpBasicAuth(){{/isBasicBasic}}{{^isBasicBasic}}{{#isBasicBearer}}HttpBearerAuth("{{scheme}}"){{/isBasicBearer}}{{^isBasicBearer}}throw RuntimeException("auth name $authName does not have a supported http scheme type"){{/isBasicBearer}}{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"){{/isApiKey}}{{#isOAuth}}OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{^-last}}, {{/-last}}{{/scopes}}"){{/isOAuth}}{{/authMethods}}
val auth = when (authName) { {{#authMethods}}
{{#isBasicBasic}}"{{name}}" -> HttpBasicAuth()
{{/isBasicBasic}}{{#isBasicBearer}}"{{name}}" -> HttpBearerAuth("{{scheme}}")
{{/isBasicBearer}}{{#isApiKey}}"{{name}}" -> ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}")
{{/isApiKey}}{{#isOAuth}}"{{name}}" -> OAuth(OAuthFlow.{{flow}}, "{{authorizationUrl}}", "{{tokenUrl}}", "{{#scopes}}{{scope}}{{^-last}}, {{/-last}}{{/scopes}}")
{{/isOAuth}}{{^isBasicBasic}}{{^isBasicBearer}}{{^isApiKey}}{{^isOAuth}}"{{name}}" -> null{{/isOAuth}}{{/isApiKey}}{{/isBasicBearer}}{{/isBasicBasic}}{{/authMethods}}
else -> throw RuntimeException("auth name $authName not found in available auth names")
}
if (auth != null) {
addAuthorization(authName, auth)
}
}
}
{{#authMethods}}
{{#isBasic}}

View File

@ -1,11 +1,11 @@
{{#authMethods}}
{{#isBasicBasic}}
val principal = call.authentication.principal<UserIdPrincipal>()!!
{{/isBasicBasic}}
{{#isApiKey}}
{{/isBasicBasic}}{{^isBasicBasic}}{{#isApiKey}}
val principal = call.authentication.principal<ApiPrincipal>()!!
{{/isApiKey}}
{{#isOAuth}}
{{/isApiKey}}{{^isApiKey}}{{#isOAuth}}
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
{{/isOAuth}}
{{/isOAuth}}{{^isOAuth}}
val principal = null!!
{{/isOAuth}}{{/isApiKey}}{{/isBasicBasic}}
{{/authMethods}}

View File

@ -57,7 +57,7 @@ public class ApiClient {
this();
for(String authName : authNames) {
log.log(Level.FINE, "Creating authentication {0}", authName);
RequestInterceptor auth;
RequestInterceptor auth = null;
if ("petstore_auth".equals(authName)) {
auth = buildOauthRequestInterceptor(OAuthFlow.IMPLICIT, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else if ("api_key".equals(authName)) {
@ -69,9 +69,11 @@ public class ApiClient {
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
}
}
/**
* Basic constructor for single auth name

View File

@ -58,7 +58,7 @@ public class ApiClient {
this();
for(String authName : authNames) {
log.log(Level.FINE, "Creating authentication {0}", authName);
RequestInterceptor auth;
RequestInterceptor auth = null;
if ("petstore_auth".equals(authName)) {
auth = buildOauthRequestInterceptor(OAuthFlow.IMPLICIT, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else if ("api_key".equals(authName)) {
@ -70,13 +70,14 @@ public class ApiClient {
} else if ("bearer_test".equals(authName)) {
auth = new HttpBearerAuth("bearer");
} else if ("http_signature_test".equals(authName)) {
throw new RuntimeException("auth name \"" + authName + "\" does not have a supported http scheme type");
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
}
}
/**
* Basic constructor for single auth name

View File

@ -52,27 +52,23 @@ public class ApiClient {
public ApiClient(String[] authNames) {
this();
for(String authName : authNames) {
Interceptor auth;
Interceptor auth = null;
if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.IMPLICIT, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth();
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
}
}
/**
* Basic constructor for single auth name

View File

@ -53,27 +53,23 @@ public class ApiClient {
public ApiClient(String[] authNames) {
this();
for(String authName : authNames) {
Interceptor auth;
Interceptor auth = null;
if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.IMPLICIT, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth();
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
}
}
/**
* Basic constructor for single auth name

View File

@ -53,27 +53,23 @@ public class ApiClient {
public ApiClient(String[] authNames) {
this();
for(String authName : authNames) {
Interceptor auth;
Interceptor auth = null;
if ("petstore_auth".equals(authName)) {
auth = new OAuth(OAuthFlow.IMPLICIT, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets");
} else if ("api_key".equals(authName)) {
auth = new ApiKeyAuth("header", "api_key");
} else if ("api_key_query".equals(authName)) {
auth = new ApiKeyAuth("query", "api_key_query");
} else if ("http_basic_test".equals(authName)) {
auth = new HttpBasicAuth();
} else {
throw new RuntimeException("auth name \"" + authName + "\" not found in available auth names");
}
if (auth != null) {
addAuthorization(authName, auth);
}
}
}
/**
* Basic constructor for single auth name

View File

@ -73,12 +73,17 @@ class ApiClient(
) : this(baseUrl, okHttpClientBuilder) {
authNames.forEach { authName ->
val auth = when (authName) {
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")"api_key" -> ApiKeyAuth("header", "api_key")
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")
"api_key" -> ApiKeyAuth("header", "api_key")
else -> throw RuntimeException("auth name $authName not found in available auth names")
}
if (auth != null) {
addAuthorization(authName, auth)
}
}
}
constructor(
baseUrl: String = defaultBasePath,

View File

@ -75,12 +75,17 @@ class ApiClient(
) : this(baseUrl, okHttpClientBuilder, serializerBuilder) {
authNames.forEach { authName ->
val auth = when (authName) {
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")"api_key" -> ApiKeyAuth("header", "api_key")
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")
"api_key" -> ApiKeyAuth("header", "api_key")
else -> throw RuntimeException("auth name $authName not found in available auth names")
}
if (auth != null) {
addAuthorization(authName, auth)
}
}
}
constructor(
baseUrl: String = defaultBasePath,

View File

@ -73,12 +73,17 @@ class ApiClient(
) : this(baseUrl, okHttpClientBuilder, serializerBuilder) {
authNames.forEach { authName ->
val auth = when (authName) {
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")"api_key" -> ApiKeyAuth("header", "api_key")
"petstore_auth" -> OAuth(OAuthFlow.implicit, "http://petstore.swagger.io/api/oauth/dialog", "", "write:pets, read:pets")
"api_key" -> ApiKeyAuth("header", "api_key")
else -> throw RuntimeException("auth name $authName not found in available auth names")
}
if (auth != null) {
addAuthorization(authName, auth)
}
}
}
constructor(
baseUrl: String = defaultBasePath,

View File

@ -35,24 +35,30 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
post<Paths.addPet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
delete<Paths.deletePet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
get<Paths.findPetsByStatus> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """[ {
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -98,8 +104,10 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
get<Paths.findPetsByTags> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """[ {
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -145,8 +153,10 @@ fun Route.PetApi() {
authenticate("api_key") {
get<Paths.getPetById> {
val principal = call.authentication.principal<ApiPrincipal>()!!
val exampleContentType = "application/json"
val exampleContentString = """{
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -176,24 +186,30 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
put<Paths.updatePet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
post<Paths.updatePetWithForm> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
post<Paths.uploadFile> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """{
"code" : 0,

View File

@ -38,8 +38,10 @@ fun Route.StoreApi() {
authenticate("api_key") {
get<Paths.getInventory> {
val principal = call.authentication.principal<ApiPrincipal>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}

View File

@ -35,24 +35,30 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
post<Paths.addPet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
delete<Paths.deletePet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
get<Paths.findPetsByStatus> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """[ {
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -98,8 +104,10 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
get<Paths.findPetsByTags> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """[ {
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -145,8 +153,10 @@ fun Route.PetApi() {
authenticate("api_key") {
get<Paths.getPetById> {
val principal = call.authentication.principal<ApiPrincipal>()!!
val exampleContentType = "application/json"
val exampleContentString = """{
"photoUrls" : [ "photoUrls", "photoUrls" ],
@ -176,24 +186,30 @@ fun Route.PetApi() {
authenticate("petstore_auth") {
put<Paths.updatePet> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
post<Paths.updatePetWithForm> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}
authenticate("petstore_auth") {
post<Paths.uploadFile> {
val principal = call.authentication.principal<OAuthAccessTokenResponse>()!!
val exampleContentType = "application/json"
val exampleContentString = """{
"code" : 0,

View File

@ -38,8 +38,10 @@ fun Route.StoreApi() {
authenticate("api_key") {
get<Paths.getInventory> {
val principal = call.authentication.principal<ApiPrincipal>()!!
call.respond(HttpStatusCode.NotImplemented)
}
}