Skip to content

Commit

Permalink
[java-client][okhttp-gson] support bearer authentication (OpenAPITool…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini authored and wing328 committed Jun 28, 2019
1 parent 6578cef commit 23e63dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public class ApiClient {
public ApiClient() {
init();
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}}
authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
// Prevent the authentications from being modified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenResponse;
import org.openapitools.codegen.CodegenSecurity;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.MockDefaultGenerator;
import org.openapitools.codegen.MockDefaultGenerator.WrittenTemplateBasedFile;
Expand Down Expand Up @@ -409,6 +410,18 @@ public void testFreeFormObjects() {
Assert.assertEquals(cm.getClassname(), "OtherObj");
}

@Test
public void testBearerAuth() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/pingBearerAuth.yaml");
JavaClientCodegen codegen = new JavaClientCodegen();

List<CodegenSecurity> security = codegen.fromSecurity(openAPI.getComponents().getSecuritySchemes());
Assert.assertEquals(security.size(), 1);
Assert.assertEquals(security.get(0).isBasic, Boolean.TRUE);
Assert.assertEquals(security.get(0).isBasicBasic, Boolean.FALSE);
Assert.assertEquals(security.get(0).isBasicBearer, Boolean.TRUE);
}

private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
CodegenProperty array = new CodegenProperty();
final CodegenProperty items = new CodegenProperty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.1
info:
title: ping test
version: '1.0'
servers:
- url: 'http://localhost:8080/'
paths:
/ping:
get:
operationId: pingGet
responses:
'201':
description: OK
components:
securitySchemes:
bearerAuth:
scheme: bearer
bearerFormat: token
type: http
security:
- bearerAuth: []

0 comments on commit 23e63dc

Please sign in to comment.