-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSwaggerConfig.java
More file actions
148 lines (123 loc) · 5.32 KB
/
SwaggerConfig.java
File metadata and controls
148 lines (123 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package clap.server.config.swagger;
import clap.server.common.annotation.swagger.ApiErrorCodes;
import clap.server.exception.code.BaseErrorCode;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.customizers.OperationCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static clap.server.common.constants.AuthConstants.AUTHORIZATION;
import static java.util.stream.Collectors.groupingBy;
@Configuration
public class SwaggerConfig {
private static final String API_NAME = "TaskFlow API";
private static final String API_VERSION = "1.0.0";
@Value("${swagger.server.url}")
private String serverUrl;
@Bean
public OpenAPI getOpenAPI() {
return new OpenAPI()
.components(getComponents())
.servers(List.of(getServer()))
.security(getSecurity())
.info(getInfo());
}
private Info getInfo() {
return new Info()
.title(API_NAME)
.version(API_VERSION);
}
private static List<SecurityRequirement> getSecurity() {
SecurityRequirement securityRequirement = new SecurityRequirement()
.addList(AUTHORIZATION.getValue());
return Collections.singletonList(securityRequirement);
}
private Server getServer() {
return new Server()
.url(serverUrl);
}
private static Components getComponents() {
SecurityScheme securityScheme = new SecurityScheme()
.type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT").name(AUTHORIZATION.getValue())
.in(SecurityScheme.In.HEADER).name(AUTHORIZATION.getValue());
return new Components()
.addSecuritySchemes(AUTHORIZATION.getValue(), securityScheme);
}
@Bean
public OperationCustomizer customize() {
return (Operation operation, HandlerMethod handlerMethod) -> {
ApiErrorCodes apiErrorCodeExample =
handlerMethod.getMethodAnnotation(ApiErrorCodes.class);
if (apiErrorCodeExample != null) {
generateErrorCodeResponse(operation, apiErrorCodeExample.value());
}
return operation;
};
}
private void generateErrorCodeResponse(Operation operation, Class<? extends BaseErrorCode> type) {
ApiResponses responses = operation.getResponses();
BaseErrorCode[] errorCodes = type.getEnumConstants();
Map<Integer, List<ErrorExampleHolder>> statusWithExampleHolders = Arrays.stream(errorCodes)
.map(errorCode -> ErrorExampleHolder.builder()
.example(getSwaggerExample(errorCode))
.customCode(errorCode.getCustomCode())
.code(errorCode.getHttpStatus().value())
.build())
.collect(groupingBy(ErrorExampleHolder::getCode));
addExamplesToResponses(responses, statusWithExampleHolders);
}
/**
* {@code @ApiErrorCodes} 어노테이션이 존재할 경우 {@code ApiResponses}에 {@code Example}를 추가하는 메소드
*
* @param responses
* @param statusWithExampleHolders
*/
private void addExamplesToResponses(
ApiResponses responses,
Map<Integer, List<ErrorExampleHolder>> statusWithExampleHolders
) {
statusWithExampleHolders.forEach(
(status, v) -> {
Content content = new Content();
MediaType mediaType = new MediaType();
ApiResponse apiResponse = new ApiResponse();
v.forEach(
exampleHolder -> mediaType.addExamples(
exampleHolder.getCustomCode(),
exampleHolder.getExample()
)
);
content.addMediaType("application/json", mediaType);
apiResponse.setContent(content);
responses.addApiResponse(String.valueOf(status), apiResponse);
});
}
/**
* {@code BaseErrorCode}를 통해 {@code Example}를 생성하는 메소드
*
* @param errorCode
* @return
*/
private Example getSwaggerExample(BaseErrorCode errorCode) {
ErrorExample errorExample = new ErrorExample(errorCode.getHttpStatus().value(), errorCode.name(), errorCode.getMessage());
Example example = new Example();
example.setValue(errorExample);
return example;
}
}