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 | 15 | */ |
16 | 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 | 22 | import org.springframework.context.annotation.Bean; |
19 | 23 | import org.springframework.context.annotation.Configuration; |
20 | 24 | import org.thingsboard.server.common.data.security.Authority; |
21 | 25 | import springfox.documentation.builders.ApiInfoBuilder; |
22 | -import springfox.documentation.builders.PathSelectors; | |
26 | +import springfox.documentation.schema.AlternateTypeRule; | |
23 | 27 | import springfox.documentation.service.*; |
24 | 28 | import springfox.documentation.spi.DocumentationType; |
25 | 29 | import springfox.documentation.spi.service.contexts.SecurityContext; |
... | ... | @@ -27,18 +31,33 @@ import springfox.documentation.spring.web.plugins.Docket; |
27 | 31 | |
28 | 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 | 36 | import static com.google.common.collect.Lists.newArrayList; |
37 | +import static springfox.documentation.builders.PathSelectors.regex; | |
31 | 38 | |
32 | 39 | @Configuration |
33 | 40 | public class SwaggerConfiguration { |
34 | 41 | |
35 | 42 | @Bean |
36 | 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 | 52 | return new Docket(DocumentationType.SWAGGER_2) |
38 | 53 | .groupName("thingsboard") |
39 | 54 | .apiInfo(apiInfo()) |
55 | + .alternateTypeRules( | |
56 | + new AlternateTypeRule( | |
57 | + jsonNodeType, | |
58 | + stringType)) | |
40 | 59 | .select() |
41 | - .paths(PathSelectors.any()) | |
60 | + .paths(apiPaths()) | |
42 | 61 | .build() |
43 | 62 | .securitySchemes(newArrayList(jwtTokenKey())) |
44 | 63 | .securityContexts(newArrayList(securityContext())); |
... | ... | @@ -51,10 +70,21 @@ public class SwaggerConfiguration { |
51 | 70 | private SecurityContext securityContext() { |
52 | 71 | return SecurityContext.builder() |
53 | 72 | .securityReferences(defaultAuth()) |
54 | - .forPaths(PathSelectors.regex("/api.*")) | |
73 | + .forPaths(securityPaths()) | |
55 | 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 | 88 | List<SecurityReference> defaultAuth() { |
59 | 89 | AuthorizationScope[] authorizationScopes = new AuthorizationScope[3]; |
60 | 90 | authorizationScopes[0] = new AuthorizationScope(Authority.SYS_ADMIN.name(), "System administrator"); |
... | ... | @@ -67,7 +97,7 @@ public class SwaggerConfiguration { |
67 | 97 | private ApiInfo apiInfo() { |
68 | 98 | return new ApiInfoBuilder() |
69 | 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 | 101 | .contact(new Contact("Thingsboard team", "http://thingsboard.io", "info@thingsboard.io")) |
72 | 102 | .license("Apache License Version 2.0") |
73 | 103 | .licenseUrl("https://github.com/thingsboard/thingsboard/blob/master/LICENSE") | ... | ... |
... | ... | @@ -15,7 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.controller; |
17 | 17 | |
18 | -import io.swagger.annotations.*; | |
19 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
20 | 19 | import org.springframework.security.access.prepost.PreAuthorize; |
21 | 20 | import org.springframework.web.bind.annotation.*; |
... | ... | @@ -46,18 +45,6 @@ public class AdminController extends BaseController { |
46 | 45 | } |
47 | 46 | |
48 | 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 | 48 | @RequestMapping(value = "/settings", method = RequestMethod.POST) |
62 | 49 | @ResponseBody |
63 | 50 | public AdminSettings saveAdminSettings(@RequestBody AdminSettings adminSettings) throws ThingsboardException { | ... | ... |
... | ... | @@ -15,9 +15,6 @@ |
15 | 15 | */ |
16 | 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 | 18 | import org.springframework.http.HttpStatus; |
22 | 19 | import org.springframework.security.access.prepost.PreAuthorize; |
23 | 20 | import org.springframework.web.bind.annotation.*; |
... | ... | @@ -37,12 +34,6 @@ import java.util.List; |
37 | 34 | @RequestMapping("/api") |
38 | 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 | 37 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
47 | 38 | @RequestMapping(value = "/rule/{ruleId}", method = RequestMethod.GET) |
48 | 39 | @ResponseBody | ... | ... |