-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom annotation @UserRoleDescription to print roles in swagger …
…doc.
- Loading branch information
1 parent
516ab86
commit a63c700
Showing
8 changed files
with
121 additions
and
26 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/carsharing/annotation/UserRoleDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.carsharing.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserRoleDescription { | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/mate/academy/carsharing/annotation/UserRoleDescriptionCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mate.academy.carsharing.annotation; | ||
|
||
import io.swagger.v3.oas.models.Operation; | ||
import org.springdoc.core.customizers.OperationCustomizer; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
|
||
@Component | ||
public class UserRoleDescriptionCustomizer implements OperationCustomizer { | ||
@Override | ||
public Operation customize(Operation operation, HandlerMethod handlerMethod) { | ||
UserRoleDescription userRoleAnnotation = | ||
handlerMethod.getMethodAnnotation(UserRoleDescription.class); | ||
if (userRoleAnnotation != null) { | ||
PreAuthorize preAuthorizeAnnotation = | ||
handlerMethod.getMethodAnnotation(PreAuthorize.class); | ||
if (preAuthorizeAnnotation != null | ||
&& preAuthorizeAnnotation.value().contains("ROLE_")) { | ||
String description = operation.getDescription() == null | ||
? "" : (operation.getDescription()/* + "\n"*/); | ||
int firstBraceIndex = preAuthorizeAnnotation.value().indexOf('('); | ||
int lastBraceIndex = preAuthorizeAnnotation.value().lastIndexOf(')'); | ||
String rolesString = preAuthorizeAnnotation.value() | ||
.substring(firstBraceIndex + 1, lastBraceIndex); | ||
operation.setDescription(description + " Required roles: " + rolesString + '.'); | ||
} | ||
} | ||
return operation; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/mate/academy/carsharing/config/OpenApiConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mate.academy.carsharing.config; | ||
|
||
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | ||
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; | ||
import io.swagger.v3.oas.annotations.info.Info; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.security.SecurityScheme; | ||
import io.swagger.v3.oas.annotations.security.SecuritySchemes; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@OpenAPIDefinition(info = @Info(title = "Car-Sharing-App REST API", version = "1.0", | ||
description = "REST API for Car-Sharing-App. Register, Login and use JWT token."), | ||
security = {@SecurityRequirement(name = "bearerToken")} | ||
) | ||
@SecuritySchemes({@SecurityScheme(name = "bearerToken", type = SecuritySchemeType.HTTP, | ||
scheme = "bearer", bearerFormat = "JWT")} | ||
) | ||
public class OpenApiConfig { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters