-
-
Notifications
You must be signed in to change notification settings - Fork 467
/
SpringDocDataRestConfiguration.java
261 lines (241 loc) · 11 KB
/
SpringDocDataRestConfiguration.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
*
* *
* * *
* * * * Copyright 2019-2022 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/
package org.springdoc.data.rest;
import java.util.Optional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.Predicate;
import org.springdoc.core.AbstractRequestService;
import org.springdoc.core.GenericParameterService;
import org.springdoc.core.GenericResponseService;
import org.springdoc.core.OpenAPIService;
import org.springdoc.core.OperationService;
import org.springdoc.core.RequestBodyService;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.SpringDocConfiguration;
import org.springdoc.core.converters.models.DefaultPageable;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springdoc.data.rest.core.DataRestOperationService;
import org.springdoc.data.rest.core.DataRestRequestService;
import org.springdoc.data.rest.core.DataRestResponseService;
import org.springdoc.data.rest.core.DataRestRouterOperationService;
import org.springdoc.data.rest.core.DataRestTagsService;
import org.springdoc.data.rest.customisers.QuerydslPredicateOperationCustomizer;
import org.springdoc.data.rest.utils.SpringDocDataRestUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.hateoas.HateoasProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.querydsl.binding.QuerydslBindingsFactory;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping;
import org.springframework.data.rest.webmvc.RootResourceInformation;
import org.springframework.data.rest.webmvc.mapping.Associations;
import org.springframework.data.rest.webmvc.support.DefaultedPageable;
import org.springframework.data.rest.webmvc.support.ETag;
import org.springframework.hateoas.server.LinkRelationProvider;
import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
import static org.springdoc.core.SpringDocUtils.getConfig;
/**
* The type Spring doc data rest configuration.
* @author bnasslashen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
@ConditionalOnBean(SpringDocConfiguration.class)
public class SpringDocDataRestConfiguration {
/**
* Hal provider data rest hal provider.
*
* @param repositoryRestConfiguration the repository rest configuration
* @param hateoasPropertiesOptional the hateoas properties optional
* @param objectMapperProvider the object mapper provider
* @return the data rest hal provider
*/
@Bean
@ConditionalOnMissingBean
@Primary
@Lazy(false)
DataRestHalProvider halProvider(Optional<RepositoryRestConfiguration> repositoryRestConfiguration, Optional<HateoasProperties> hateoasPropertiesOptional, ObjectMapperProvider objectMapperProvider) {
return new DataRestHalProvider(repositoryRestConfiguration, hateoasPropertiesOptional, objectMapperProvider);
}
/**
* The type Querydsl provider.
* @author bnasslashen
*/
@ConditionalOnClass(value = { QuerydslBindingsFactory.class })
class QuerydslProvider {
/**
* Query dsl querydsl predicate operation customizer querydsl predicate operation customizer.
*
* @param querydslBindingsFactory the querydsl bindings factory
* @return the querydsl predicate operation customizer
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
QuerydslPredicateOperationCustomizer queryDslQuerydslPredicateOperationCustomizer(Optional<QuerydslBindingsFactory> querydslBindingsFactory) {
if (querydslBindingsFactory.isPresent()) {
getConfig().addRequestWrapperToIgnore(Predicate.class);
return new QuerydslPredicateOperationCustomizer(querydslBindingsFactory.get());
}
return null;
}
}
/**
* The type Spring repository rest resource provider configuration.
* @author bnasslashen
*/
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RepositoryRestHandlerMapping.class)
static class SpringRepositoryRestResourceProviderConfiguration {
static {
getConfig().replaceParameterObjectWithClass(DefaultedPageable.class, DefaultPageable.class)
.addRequestWrapperToIgnore(RootResourceInformation.class, PersistentEntityResourceAssembler.class, ETag.class, Sort.class)
.addResponseWrapperToIgnore(RootResourceInformation.class);
}
/**
* Spring repository rest resource provider spring repository rest resource provider.
*
* @param mappings the mappings
* @param repositories the repositories
* @param associations the associations
* @param applicationContext the application context
* @param dataRestRouterOperationService the data rest router operation service
* @param persistentEntities the persistent entities
* @param mapper the mapper
* @param springDocDataRestUtils the spring doc data rest utils
* @return the spring repository rest resource provider
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
SpringRepositoryRestResourceProvider springRepositoryRestResourceProvider(ResourceMappings mappings,
Repositories repositories, Associations associations, ApplicationContext applicationContext,
DataRestRouterOperationService dataRestRouterOperationService, PersistentEntities persistentEntities,
ObjectMapper mapper, SpringDocDataRestUtils springDocDataRestUtils) {
return new SpringRepositoryRestResourceProvider(mappings, repositories, associations, applicationContext,
dataRestRouterOperationService, persistentEntities, mapper, springDocDataRestUtils);
}
/**
* Data rest router operation builder data rest router operation service.
*
* @param dataRestOperationService the data rest operation service
* @param springDocConfigProperties the spring doc config properties
* @param repositoryRestConfiguration the repository rest configuration
* @param dataRestHalProvider the data rest hal provider
* @return the data rest router operation service
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DataRestRouterOperationService dataRestRouterOperationBuilder(DataRestOperationService dataRestOperationService,
SpringDocConfigProperties springDocConfigProperties, RepositoryRestConfiguration repositoryRestConfiguration, DataRestHalProvider dataRestHalProvider) {
return new DataRestRouterOperationService(dataRestOperationService, springDocConfigProperties, repositoryRestConfiguration, dataRestHalProvider);
}
/**
* Data rest operation builder data rest operation builder.
*
* @param dataRestRequestService the data rest request builder
* @param tagsBuilder the tags builder
* @param dataRestResponseService the data rest response builder
* @param operationService the operation service
* @return the data rest operation builder
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DataRestOperationService dataRestOperationBuilder(DataRestRequestService dataRestRequestService, DataRestTagsService tagsBuilder,
DataRestResponseService dataRestResponseService, OperationService operationService) {
return new DataRestOperationService(dataRestRequestService, tagsBuilder, dataRestResponseService, operationService);
}
/**
* Data rest request builder data rest request builder.
*
* @param localSpringDocParameterNameDiscoverer the local spring doc parameter name discoverer
* @param parameterBuilder the parameter builder
* @param requestBodyService the request body builder
* @param requestBuilder the request builder
* @param springDocDataRestUtils the spring doc data rest utils
* @return the data rest request builder
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DataRestRequestService dataRestRequestBuilder(LocalVariableTableParameterNameDiscoverer localSpringDocParameterNameDiscoverer, GenericParameterService parameterBuilder,
RequestBodyService requestBodyService, AbstractRequestService requestBuilder, SpringDocDataRestUtils springDocDataRestUtils) {
return new DataRestRequestService(localSpringDocParameterNameDiscoverer, parameterBuilder,
requestBodyService, requestBuilder, springDocDataRestUtils);
}
/**
* Data rest response builder data rest response builder.
*
* @param genericResponseService the generic response builder
* @param springDocDataRestUtils the spring doc data rest utils
* @return the data rest response builder
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DataRestResponseService dataRestResponseBuilder(GenericResponseService genericResponseService, SpringDocDataRestUtils springDocDataRestUtils) {
return new DataRestResponseService(genericResponseService, springDocDataRestUtils);
}
/**
* Data rest tags builder data rest tags builder.
*
* @param openAPIService the open api builder
* @return the data rest tags builder
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
DataRestTagsService dataRestTagsBuilder(OpenAPIService openAPIService) {
return new DataRestTagsService(openAPIService);
}
/**
* Spring doc data rest utils spring doc data rest utils.
*
* @param linkRelationProvider the link relation provider
* @param repositoryRestConfiguration the repository rest configuration
* @return the spring doc data rest utils
*/
@Bean
@ConditionalOnMissingBean
@Lazy(false)
SpringDocDataRestUtils springDocDataRestUtils(LinkRelationProvider linkRelationProvider, RepositoryRestConfiguration repositoryRestConfiguration) {
return new SpringDocDataRestUtils(linkRelationProvider, repositoryRestConfiguration);
}
}
}