Commit b154f1a4fd406e48c5e5972b5cf175255528754b
Committed by
GitHub
1 parent
e1dd8604
TB-50: Improve swagger configuration (#5)
Showing
3 changed files
with
34 additions
and
26 deletions
@@ -15,11 +15,15 @@ | @@ -15,11 +15,15 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.config; | 16 | package org.thingsboard.server.config; |
17 | 17 | ||
18 | +import com.fasterxml.classmate.ResolvedType; | ||
19 | +import com.fasterxml.classmate.TypeResolver; | ||
20 | +import com.fasterxml.jackson.databind.JsonNode; | ||
21 | +import com.google.common.base.Predicate; | ||
18 | import org.springframework.context.annotation.Bean; | 22 | import org.springframework.context.annotation.Bean; |
19 | import org.springframework.context.annotation.Configuration; | 23 | import org.springframework.context.annotation.Configuration; |
20 | import org.thingsboard.server.common.data.security.Authority; | 24 | import org.thingsboard.server.common.data.security.Authority; |
21 | import springfox.documentation.builders.ApiInfoBuilder; | 25 | import springfox.documentation.builders.ApiInfoBuilder; |
22 | -import springfox.documentation.builders.PathSelectors; | 26 | +import springfox.documentation.schema.AlternateTypeRule; |
23 | import springfox.documentation.service.*; | 27 | import springfox.documentation.service.*; |
24 | import springfox.documentation.spi.DocumentationType; | 28 | import springfox.documentation.spi.DocumentationType; |
25 | import springfox.documentation.spi.service.contexts.SecurityContext; | 29 | import springfox.documentation.spi.service.contexts.SecurityContext; |
@@ -27,18 +31,33 @@ import springfox.documentation.spring.web.plugins.Docket; | @@ -27,18 +31,33 @@ import springfox.documentation.spring.web.plugins.Docket; | ||
27 | 31 | ||
28 | import java.util.List; | 32 | import java.util.List; |
29 | 33 | ||
34 | +import static com.google.common.base.Predicates.and; | ||
35 | +import static com.google.common.base.Predicates.not; | ||
30 | import static com.google.common.collect.Lists.newArrayList; | 36 | import static com.google.common.collect.Lists.newArrayList; |
37 | +import static springfox.documentation.builders.PathSelectors.regex; | ||
31 | 38 | ||
32 | @Configuration | 39 | @Configuration |
33 | public class SwaggerConfiguration { | 40 | public class SwaggerConfiguration { |
34 | 41 | ||
35 | @Bean | 42 | @Bean |
36 | public Docket thingsboardApi() { | 43 | public Docket thingsboardApi() { |
44 | + TypeResolver typeResolver = new TypeResolver(); | ||
45 | + final ResolvedType jsonNodeType = | ||
46 | + typeResolver.resolve( | ||
47 | + JsonNode.class); | ||
48 | + final ResolvedType stringType = | ||
49 | + typeResolver.resolve( | ||
50 | + String.class); | ||
51 | + | ||
37 | return new Docket(DocumentationType.SWAGGER_2) | 52 | return new Docket(DocumentationType.SWAGGER_2) |
38 | .groupName("thingsboard") | 53 | .groupName("thingsboard") |
39 | .apiInfo(apiInfo()) | 54 | .apiInfo(apiInfo()) |
55 | + .alternateTypeRules( | ||
56 | + new AlternateTypeRule( | ||
57 | + jsonNodeType, | ||
58 | + stringType)) | ||
40 | .select() | 59 | .select() |
41 | - .paths(PathSelectors.any()) | 60 | + .paths(apiPaths()) |
42 | .build() | 61 | .build() |
43 | .securitySchemes(newArrayList(jwtTokenKey())) | 62 | .securitySchemes(newArrayList(jwtTokenKey())) |
44 | .securityContexts(newArrayList(securityContext())); | 63 | .securityContexts(newArrayList(securityContext())); |
@@ -51,10 +70,21 @@ public class SwaggerConfiguration { | @@ -51,10 +70,21 @@ public class SwaggerConfiguration { | ||
51 | private SecurityContext securityContext() { | 70 | private SecurityContext securityContext() { |
52 | return SecurityContext.builder() | 71 | return SecurityContext.builder() |
53 | .securityReferences(defaultAuth()) | 72 | .securityReferences(defaultAuth()) |
54 | - .forPaths(PathSelectors.regex("/api.*")) | 73 | + .forPaths(securityPaths()) |
55 | .build(); | 74 | .build(); |
56 | } | 75 | } |
57 | 76 | ||
77 | + private Predicate<String> apiPaths() { | ||
78 | + return regex("/api.*"); | ||
79 | + } | ||
80 | + | ||
81 | + private Predicate<String> securityPaths() { | ||
82 | + return and( | ||
83 | + regex("/api.*"), | ||
84 | + not(regex("/api/noauth.*")) | ||
85 | + ); | ||
86 | + } | ||
87 | + | ||
58 | List<SecurityReference> defaultAuth() { | 88 | List<SecurityReference> defaultAuth() { |
59 | AuthorizationScope[] authorizationScopes = new AuthorizationScope[3]; | 89 | AuthorizationScope[] authorizationScopes = new AuthorizationScope[3]; |
60 | authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator"); | 90 | authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator"); |
@@ -67,7 +97,7 @@ public class SwaggerConfiguration { | @@ -67,7 +97,7 @@ public class SwaggerConfiguration { | ||
67 | private ApiInfo apiInfo() { | 97 | private ApiInfo apiInfo() { |
68 | return new ApiInfoBuilder() | 98 | return new ApiInfoBuilder() |
69 | .title("Thingsboard REST API") | 99 | .title("Thingsboard REST API") |
70 | - .description("For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/rest-auth'>Documentation page</a>") | 100 | + .description("For instructions how to authorize requests please visit <a href='http://thingsboard.io/docs/reference/rest-api/'>REST API documentation page</a>.") |
71 | .contact(new Contact("Thingsboard team", "http://thingsboard.io", "info@thingsboard.io")) | 101 | .contact(new Contact("Thingsboard team", "http://thingsboard.io", "info@thingsboard.io")) |
72 | .license("Apache License Version 2.0") | 102 | .license("Apache License Version 2.0") |
73 | .licenseUrl("https://github.com/thingsboard/thingsboard/blob/master/LICENSE") | 103 | .licenseUrl("https://github.com/thingsboard/thingsboard/blob/master/LICENSE") |
@@ -15,7 +15,6 @@ | @@ -15,7 +15,6 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.controller; | 16 | package org.thingsboard.server.controller; |
17 | 17 | ||
18 | -import io.swagger.annotations.*; | ||
19 | import org.springframework.beans.factory.annotation.Autowired; | 18 | import org.springframework.beans.factory.annotation.Autowired; |
20 | import org.springframework.security.access.prepost.PreAuthorize; | 19 | import org.springframework.security.access.prepost.PreAuthorize; |
21 | import org.springframework.web.bind.annotation.*; | 20 | import org.springframework.web.bind.annotation.*; |
@@ -46,18 +45,6 @@ public class AdminController extends BaseController { | @@ -46,18 +45,6 @@ public class AdminController extends BaseController { | ||
46 | } | 45 | } |
47 | 46 | ||
48 | @PreAuthorize("hasAuthority('SYS_ADMIN')") | 47 | @PreAuthorize("hasAuthority('SYS_ADMIN')") |
49 | - @ApiOperation( | ||
50 | - value = "Save admin settings", notes = "Saves admin settings", | ||
51 | - response = AdminSettings.class, | ||
52 | - authorizations = { | ||
53 | - @Authorization(value = "X-Authorization", scopes = { | ||
54 | - @AuthorizationScope(scope = "SYS_ADMIN", description = "") | ||
55 | - })}) | ||
56 | - @ApiResponses(value = { | ||
57 | - @ApiResponse(code = 200, response = AdminSettings.class, message = "Admin settings successfully updated"), | ||
58 | - @ApiResponse(code = 400, message = "Invalid admin settings payload supplied"), | ||
59 | - @ApiResponse(code = 404, message = "Admin settings not found")} | ||
60 | - ) | ||
61 | @RequestMapping(value = "/settings", method = RequestMethod.POST) | 48 | @RequestMapping(value = "/settings", method = RequestMethod.POST) |
62 | @ResponseBody | 49 | @ResponseBody |
63 | public AdminSettings saveAdminSettings(@RequestBody AdminSettings adminSettings) throws ThingsboardException { | 50 | public AdminSettings saveAdminSettings(@RequestBody AdminSettings adminSettings) throws ThingsboardException { |
@@ -15,9 +15,6 @@ | @@ -15,9 +15,6 @@ | ||
15 | */ | 15 | */ |
16 | package org.thingsboard.server.controller; | 16 | package org.thingsboard.server.controller; |
17 | 17 | ||
18 | -import io.swagger.annotations.ApiOperation; | ||
19 | -import io.swagger.annotations.ApiResponse; | ||
20 | -import io.swagger.annotations.ApiResponses; | ||
21 | import org.springframework.http.HttpStatus; | 18 | import org.springframework.http.HttpStatus; |
22 | import org.springframework.security.access.prepost.PreAuthorize; | 19 | import org.springframework.security.access.prepost.PreAuthorize; |
23 | import org.springframework.web.bind.annotation.*; | 20 | import org.springframework.web.bind.annotation.*; |
@@ -37,12 +34,6 @@ import java.util.List; | @@ -37,12 +34,6 @@ import java.util.List; | ||
37 | @RequestMapping("/api") | 34 | @RequestMapping("/api") |
38 | public class RuleController extends BaseController { | 35 | public class RuleController extends BaseController { |
39 | 36 | ||
40 | - @ApiOperation(value = "getRuleById", nickname = "Get Rule By ID") | ||
41 | - @ApiResponses(value = { | ||
42 | - @ApiResponse(code = 200, message = "Success", response = RuleMetaData.class), | ||
43 | - @ApiResponse(code = 401, message = "Unauthorized"), | ||
44 | - @ApiResponse(code = 403, message = "Forbidden"), | ||
45 | - @ApiResponse(code = 404, message = "Not Found")}) | ||
46 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | 37 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
47 | @RequestMapping(value = "/rule/{ruleId}", method = RequestMethod.GET) | 38 | @RequestMapping(value = "/rule/{ruleId}", method = RequestMethod.GET) |
48 | @ResponseBody | 39 | @ResponseBody |