Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a33fe7c

Browse files
committed
Added support for schema operations
1 parent 55d7ed8 commit a33fe7c

4 files changed

Lines changed: 256 additions & 1 deletion

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
*
3+
* Copyright 2016 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.data.rest;
20+
21+
import com.fasterxml.classmate.MemberResolver;
22+
import com.fasterxml.classmate.ResolvedType;
23+
import com.fasterxml.classmate.ResolvedTypeWithMembers;
24+
import com.fasterxml.classmate.TypeResolver;
25+
import com.fasterxml.classmate.members.ResolvedMethod;
26+
import com.google.common.base.Optional;
27+
import org.springframework.core.annotation.AnnotationUtils;
28+
import org.springframework.data.rest.core.mapping.ResourceMetadata;
29+
import org.springframework.http.MediaType;
30+
import org.springframework.web.bind.annotation.RequestMethod;
31+
import org.springframework.web.method.HandlerMethod;
32+
import org.springframework.web.servlet.mvc.condition.NameValueExpression;
33+
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
34+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
35+
import springfox.documentation.RequestHandler;
36+
import springfox.documentation.RequestHandlerKey;
37+
import springfox.documentation.service.ResolvedMethodParameter;
38+
39+
import java.lang.annotation.Annotation;
40+
import java.util.List;
41+
import java.util.Set;
42+
43+
import static com.google.common.collect.Lists.*;
44+
import static com.google.common.collect.Sets.*;
45+
46+
class EntitySchemaHandler implements RequestHandler {
47+
private final TypeResolver resolver;
48+
private final ResourceMetadata resource;
49+
private final RequestMappingInfo requestMapping;
50+
private final HandlerMethod handlerMethod;
51+
52+
EntitySchemaHandler(
53+
TypeResolver resolver,
54+
ResourceMetadata resource,
55+
RequestMappingInfo requestMapping,
56+
HandlerMethod handlerMethod) {
57+
this.resolver = resolver;
58+
this.resource = resource;
59+
this.requestMapping = requestMapping;
60+
this.handlerMethod = handlerMethod;
61+
}
62+
63+
@Override
64+
public Class<?> declaringClass() {
65+
return handlerMethod.getMethod().getDeclaringClass();
66+
}
67+
68+
@Override
69+
public boolean isAnnotatedWith(Class<? extends Annotation> annotation) {
70+
return null != AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation);
71+
}
72+
73+
@Override
74+
public PatternsRequestCondition getPatternsCondition() {
75+
Set<String> patterns = newHashSet();
76+
for (String each : requestMapping.getPatternsCondition().getPatterns()) {
77+
String replaced = each
78+
.replace("/{repository}", resource.getPath().toString());
79+
patterns.add(replaced);
80+
}
81+
return new PatternsRequestCondition(patterns.toArray(new String[patterns.size()]));
82+
}
83+
84+
@Override
85+
public String groupName() {
86+
return "Entity Schemas";
87+
}
88+
89+
@Override
90+
public String getName() {
91+
return handlerMethod.getMethod().getName();
92+
}
93+
94+
@Override
95+
public Set<RequestMethod> supportedMethods() {
96+
return requestMapping.getMethodsCondition().getMethods();
97+
}
98+
99+
@Override
100+
public Set<? extends MediaType> produces() {
101+
return requestMapping.getProducesCondition().getProducibleMediaTypes();
102+
}
103+
104+
@Override
105+
public Set<? extends MediaType> consumes() {
106+
return requestMapping.getConsumesCondition().getConsumableMediaTypes();
107+
}
108+
109+
@Override
110+
public Set<NameValueExpression<String>> headers() {
111+
return requestMapping.getHeadersCondition().getExpressions();
112+
}
113+
114+
@Override
115+
public Set<NameValueExpression<String>> params() {
116+
return requestMapping.getParamsCondition().getExpressions();
117+
}
118+
119+
@Override
120+
public <T extends Annotation> Optional<T> findAnnotation(Class<T> annotation) {
121+
return Optional.fromNullable(AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation));
122+
}
123+
124+
@Override
125+
public RequestHandlerKey key() {
126+
return new RequestHandlerKey(
127+
getPatternsCondition().getPatterns(),
128+
supportedMethods(),
129+
produces(),
130+
consumes());
131+
}
132+
133+
@Override
134+
public List<ResolvedMethodParameter> getParameters() {
135+
return newArrayList();
136+
}
137+
138+
@Override
139+
public ResolvedType getReturnType() {
140+
MemberResolver memberResolver = new MemberResolver(resolver);
141+
ResolvedTypeWithMembers members = memberResolver.resolve(
142+
resolver.resolve(handlerMethod.getMethod().getDeclaringClass()), null, null);
143+
for (ResolvedMethod resolvedMethod : members.getMemberMethods()) {
144+
if (resolvedMethod.getRawMember().equals(handlerMethod.getMethod())) {
145+
return resolvedMethod.getReturnType();
146+
}
147+
}
148+
return resolver.resolve(Void.TYPE);
149+
}
150+
151+
@Override
152+
public <T extends Annotation> Optional<T> findControllerAnnotation(Class<T> annotation) {
153+
return Optional.fromNullable(AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), annotation));
154+
}
155+
156+
@Override
157+
public RequestMappingInfo getRequestMapping() {
158+
return requestMapping;
159+
}
160+
161+
@Override
162+
public HandlerMethod getHandlerMethod() {
163+
return handlerMethod;
164+
}
165+
166+
@Override
167+
public RequestHandler combine(RequestHandler other) {
168+
return this;
169+
}
170+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* Copyright 2016 the original author or authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*
18+
*/
19+
package springfox.documentation.spring.data.rest;
20+
21+
import com.fasterxml.classmate.TypeResolver;
22+
import org.springframework.data.rest.core.mapping.ResourceMappings;
23+
import org.springframework.data.rest.core.mapping.ResourceMetadata;
24+
import org.springframework.web.method.HandlerMethod;
25+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
26+
import springfox.documentation.RequestHandler;
27+
28+
import java.util.Collection;
29+
import java.util.List;
30+
31+
import static com.google.common.collect.Lists.*;
32+
33+
class EntitySchemaTemplate {
34+
private final TypeResolver resolver;
35+
private final ResourceMappings mappings;
36+
private final RequestMappingInfo requestMapping;
37+
private final HandlerMethod handlerMethod;
38+
39+
EntitySchemaTemplate(
40+
TypeResolver resolver,
41+
ResourceMappings mappings,
42+
RequestMappingInfo requestMapping,
43+
HandlerMethod handlerMethod) {
44+
this.resolver = resolver;
45+
this.mappings = mappings;
46+
this.requestMapping = requestMapping;
47+
this.handlerMethod = handlerMethod;
48+
}
49+
50+
51+
Collection<RequestHandler> operations() {
52+
List<RequestHandler> requestHandlers = newArrayList();
53+
for (ResourceMetadata resource : mappings) {
54+
55+
requestHandlers.add(
56+
new EntitySchemaHandler(
57+
resolver,
58+
resource,
59+
requestMapping,
60+
handlerMethod));
61+
62+
}
63+
return requestHandlers;
64+
}
65+
}

springfox-data-rest/src/main/java/springfox/documentation/spring/data/rest/EntityServices.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,13 @@ public boolean apply(Map.Entry<RequestMappingInfo, HandlerMethod> input) {
7171
}
7272
};
7373
}
74+
75+
static Predicate<Map.Entry<RequestMappingInfo, HandlerMethod>> entitySchemaService() {
76+
return new Predicate<Map.Entry<RequestMappingInfo, HandlerMethod>>() {
77+
@Override
78+
public boolean apply(Map.Entry<RequestMappingInfo, HandlerMethod> input) {
79+
return input.getValue().getBeanType().getSimpleName().equals("RepositorySchemaController");
80+
}
81+
};
82+
}
7483
}

springfox-data-rest/src/main/java/springfox/documentation/spring/data/rest/EntityServicesProvider.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,19 @@ public List<RequestHandler> requestHandlers() {
103103
searchHandlers.addAll(entityRequestHandlers.operations());
104104
}
105105
requestHandlers.addAll(maybeCombine(searchHandlers));
106+
106107
for (Map.Entry<RequestMappingInfo, HandlerMethod> each : basePathAwareMappings.getHandlerMethods().entrySet()) {
107-
requestHandlers.add(new WebMvcRequestHandler(each.getKey(), each.getValue()));
108+
boolean schemaService = entitySchemaService().apply(each);
109+
if (schemaService) {
110+
requestHandlers.addAll(
111+
new EntitySchemaTemplate(
112+
typeResolver,
113+
mappings,
114+
each.getKey(),
115+
each.getValue()).operations());
116+
} else {
117+
requestHandlers.add(new WebMvcRequestHandler(each.getKey(), each.getValue()));
118+
}
108119
}
109120
return requestHandlers;
110121
}

0 commit comments

Comments
 (0)