-
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.
- Loading branch information
Showing
11 changed files
with
248 additions
and
7 deletions.
There are no files selected for viewing
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
4 changes: 2 additions & 2 deletions
4
...boutbits/springboot/toolbox/autoconfiguration/swagger/RegisterCustomTypesWithSwagger.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
12 changes: 12 additions & 0 deletions
12
src/main/java/it/aboutbits/springboot/toolbox/swagger/annotations/SwaggerScopedAuth.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 it.aboutbits.springboot.toolbox.swagger.annotations; | ||
|
||
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 SwaggerScopedAuth { | ||
String value(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...ingboot/toolbox/swagger/customization/alphabetical_model_order/OrderModelsCustomizer.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,15 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.alphabetical_model_order; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import org.springdoc.core.customizers.OpenApiCustomizer; | ||
|
||
import java.util.TreeMap; | ||
|
||
public class OrderModelsCustomizer implements OpenApiCustomizer { | ||
@Override | ||
public void customise(OpenAPI openApi) { | ||
var components = openApi.getComponents(); | ||
|
||
components.schemas(new TreeMap<>(components.getSchemas())); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../springboot/toolbox/swagger/customization/authorization_docs/AuthorizationDescriptor.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,50 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.authorization_docs; | ||
|
||
import io.swagger.v3.oas.models.Operation; | ||
import it.aboutbits.springboot.toolbox.swagger.annotations.SwaggerScopedAuth; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springdoc.core.customizers.OperationCustomizer; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.method.HandlerMethod; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
public class AuthorizationDescriptor implements OperationCustomizer { | ||
@Override | ||
public Operation customize(Operation operation, HandlerMethod handlerMethod) { | ||
try { | ||
var additionalDescription = new ArrayList<String>(); | ||
|
||
var maybeAnnotation = Optional.ofNullable(handlerMethod.getMethodAnnotation(PreAuthorize.class)); | ||
if (maybeAnnotation.isPresent()) { | ||
var annotation = maybeAnnotation.get(); | ||
additionalDescription.add("<b>Authorization:</b> " + annotation.value()); | ||
} | ||
|
||
var maybeAnnotation2 = Optional.ofNullable(handlerMethod.getMethodAnnotation(SwaggerScopedAuth.class)); | ||
if (maybeAnnotation2.isPresent()) { | ||
var annotation = maybeAnnotation2.get(); | ||
additionalDescription.add("<b>Scoped Authorization:</b> " + annotation.value()); | ||
} | ||
|
||
if (!additionalDescription.isEmpty()) { | ||
|
||
var currentDescription = Optional.ofNullable(operation.getDescription()); | ||
|
||
var description = String.join("<br />", additionalDescription); | ||
if (currentDescription.isPresent()) { | ||
description = "<p>" + description + "</p>" + currentDescription.get(); | ||
} | ||
|
||
operation.description( | ||
description | ||
); | ||
} | ||
} catch (Exception e) { | ||
log.error("Error when creating swagger documentation for authorities.", e); | ||
} | ||
return operation; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...outbits/springboot/toolbox/swagger/customization/default_not_null/NullableCustomizer.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,24 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.default_not_null; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import org.springdoc.core.customizers.OpenApiCustomizer; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class NullableCustomizer implements OpenApiCustomizer { | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void customise(OpenAPI openApi) { | ||
openApi.getComponents().getSchemas().values() | ||
.forEach(schema -> { | ||
var requiredProperties = new ArrayList<String>(); | ||
((Schema<?>) schema).getProperties().forEach((propertyName, property) -> { | ||
if (property.getNullable() == null || !property.getNullable()) { | ||
requiredProperties.add(propertyName); | ||
} | ||
}); | ||
schema.setRequired(requiredProperties); | ||
}); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...springboot/toolbox/swagger/customization/default_not_null/NullablePropertyCustomizer.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,39 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.default_not_null; | ||
|
||
import io.swagger.v3.core.converter.AnnotatedType; | ||
import io.swagger.v3.core.jackson.ModelResolver; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import org.springdoc.core.customizers.PropertyCustomizer; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
@Component | ||
public class NullablePropertyCustomizer implements PropertyCustomizer { | ||
static { | ||
/* | ||
We need this because the ModelResolver will only process these whitelisted annotations. | ||
We will then be able to manipulate each property based on the set annotations. | ||
*/ | ||
|
||
var list = new ArrayList<>(ModelResolver.NOT_NULL_ANNOTATIONS); | ||
list.add("Nullable"); | ||
|
||
ModelResolver.NOT_NULL_ANNOTATIONS = list; | ||
} | ||
|
||
@Override | ||
public Schema<?> customize(Schema property, AnnotatedType annotatedType) { | ||
/* | ||
Mark the nullable ones as nullable. | ||
*/ | ||
|
||
if (annotatedType.getCtxAnnotations() != null && Arrays.stream(annotatedType.getCtxAnnotations()) | ||
.anyMatch(a -> "Nullable".equals(a.annotationType().getSimpleName()))) { | ||
property.setNullable(true); | ||
} | ||
|
||
return property; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...it/aboutbits/springboot/toolbox/swagger/customization/error_response/ErrorCustomizer.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,66 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.error_response; | ||
|
||
import io.swagger.v3.core.converter.ModelConverters; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.media.Content; | ||
import io.swagger.v3.oas.models.media.MediaType; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import io.swagger.v3.oas.models.responses.ApiResponse; | ||
import io.swagger.v3.oas.models.responses.ApiResponses; | ||
import it.aboutbits.springboot.toolbox.mvc.response.ErrorResponse; | ||
import org.springdoc.core.customizers.OpenApiCustomizer; | ||
|
||
import java.util.Map; | ||
|
||
public class ErrorCustomizer implements OpenApiCustomizer { | ||
@Override | ||
public void customise(OpenAPI openApi) { | ||
openApi.getComponents() | ||
.getSchemas() | ||
.putAll( | ||
ModelConverters.getInstance().read(ErrorResponse.class) | ||
); | ||
|
||
var errorResponseSchema = openApi.getComponents().getSchemas().get("ErrorResponse"); | ||
@SuppressWarnings("unchecked") | ||
Map<String, Schema<?>> props = errorResponseSchema.getProperties(); | ||
for (var prop : props.values()) { | ||
prop.nullable(true); | ||
} | ||
|
||
openApi.getPaths() | ||
.values() | ||
.forEach( | ||
pathItem -> pathItem.readOperations() | ||
.forEach( | ||
operation -> { | ||
ApiResponses apiResponses = operation.getResponses(); | ||
apiResponses.addApiResponse( | ||
"400", | ||
createApiResponse( | ||
"Bad Request", | ||
errorResponseSchema | ||
) | ||
); | ||
apiResponses.addApiResponse( | ||
"404", | ||
createApiResponse( | ||
"Not Found", | ||
errorResponseSchema | ||
) | ||
); | ||
} | ||
) | ||
); | ||
} | ||
|
||
private ApiResponse createApiResponse(String message, Schema<?> schema) { | ||
var mediaType = new MediaType(); | ||
mediaType.schema(schema); | ||
return new ApiResponse().description(message) | ||
.content(new Content().addMediaType( | ||
org.springframework.http.MediaType.APPLICATION_JSON_VALUE, | ||
mediaType | ||
)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../it/aboutbits/springboot/toolbox/swagger/customization/logout_route/LogoutCustomizer.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,26 @@ | ||
package it.aboutbits.springboot.toolbox.swagger.customization.logout_route; | ||
|
||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.Operation; | ||
import io.swagger.v3.oas.models.PathItem; | ||
import io.swagger.v3.oas.models.responses.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springdoc.core.customizers.OpenApiCustomizer; | ||
|
||
@RequiredArgsConstructor | ||
public class LogoutCustomizer implements OpenApiCustomizer { | ||
private final String logoutUrl; | ||
|
||
@Override | ||
public void customise(OpenAPI openApi) { | ||
var operation = new Operation(); | ||
operation.addTagsItem("Authentication API"); | ||
operation.summary("Logout the current user"); | ||
operation.responses(new ApiResponses()); | ||
|
||
var pathItem = new PathItem(); | ||
pathItem.setPost(operation); | ||
|
||
openApi.getPaths().addPathItem(logoutUrl, pathItem); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...box/swagger/CustomTypeModelConverter.java → ...wagger/type/CustomTypeModelConverter.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
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