Skip to content

Commit 4c43e5c

Browse files
committed
Fixed the produces media type list as specified by the allowedHttpMethods
(6)
1 parent e8e07ca commit 4c43e5c

10 files changed

+72
-9
lines changed

springfox-grails/src/main/java/springfox/documentation/grails/Actions.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
package springfox.documentation.grails;
22

3+
import com.google.common.collect.ImmutableMap;
34
import grails.web.Action;
45
import groovy.lang.GroovyObject;
6+
import org.grails.core.DefaultGrailsControllerClass;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.web.bind.annotation.RequestMethod;
59
import org.springframework.web.method.HandlerMethod;
610

11+
import java.lang.reflect.Field;
712
import java.lang.reflect.Method;
813
import java.lang.reflect.Modifier;
914
import java.util.HashMap;
15+
import java.util.HashSet;
16+
import java.util.List;
1017
import java.util.Map;
18+
import java.util.Set;
19+
import java.util.stream.Collectors;
20+
21+
import static com.google.common.collect.Sets.*;
1122

1223
class Actions {
1324

25+
private static final String PROPERTY_RESPONSE_FORMATS = "responseFormats";
26+
private static final Map<String, MediaType> mediaTypes = ImmutableMap.<String, MediaType>builder()
27+
.put("json", MediaType.APPLICATION_JSON)
28+
.put("xml", MediaType.APPLICATION_XML)
29+
.build();
30+
1431
private Actions() {
1532
throw new UnsupportedOperationException();
1633
}
@@ -28,4 +45,49 @@ static Map<String, HandlerMethod> actionsToHandler(Class grailsController) {
2845
}
2946
return handlerLookup;
3047
}
48+
49+
static Set<RequestMethod> methodOverrides(GrailsActionContext context) {
50+
Set<RequestMethod> methods = new HashSet<>();
51+
Map<String, String> allowedMethods;
52+
try {
53+
Class clazz = context.getController().getClazz();
54+
Field allowedMethodsField = clazz
55+
.getDeclaredField(DefaultGrailsControllerClass.ALLOWED_HTTP_METHODS_PROPERTY);
56+
allowedMethodsField.setAccessible(true);
57+
allowedMethods = (Map<String, String>) allowedMethodsField.get(clazz);
58+
if (allowedMethods != null && allowedMethods.containsKey(context.getAction())) {
59+
methods.add(RequestMethod.valueOf(allowedMethods.get(context.getAction())));
60+
}
61+
} catch (NoSuchFieldException | IllegalAccessException ignored) {
62+
}
63+
return methods;
64+
}
65+
66+
private static void setFinalStatic(Field field, Object newValue) throws Exception {
67+
field.setAccessible(true);
68+
69+
Field modifiersField = Field.class.getDeclaredField("modifiers");
70+
modifiersField.setAccessible(true);
71+
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
72+
73+
field.set(null, newValue);
74+
}
75+
76+
static Set<MediaType> producesOverrides(GrailsActionContext context) {
77+
Set<MediaType> produces = newHashSet(MediaType.APPLICATION_JSON);
78+
List<String> responseFormats;
79+
try {
80+
Class clazz = context.getController().getClazz();
81+
Field responseFormatFields = clazz.getDeclaredField(PROPERTY_RESPONSE_FORMATS);
82+
responseFormatFields.setAccessible(true);
83+
responseFormats = (List<String>) responseFormatFields.get(clazz);
84+
if (responseFormats != null && !responseFormats.isEmpty()) {
85+
return responseFormats.stream()
86+
.map(format -> mediaTypes.getOrDefault(format, MediaType.ALL))
87+
.collect(Collectors.toSet());
88+
}
89+
} catch (NoSuchFieldException | IllegalAccessException ignored) {
90+
}
91+
return produces;
92+
}
3193
}

springfox-grails/src/main/java/springfox/documentation/grails/CreateActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ActionSpecification create(GrailsActionContext context) {
2626
HandlerMethod handlerMethod = actions.get(context.getAction());
2727
return new ActionSpecification(
2828
new HashSet<>(Arrays.asList(RequestMethod.POST, RequestMethod.PUT)),
29-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
29+
new HashSet<>(producesOverrides(context)),
3030
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3131
handlerMethod,
3232
new ArrayList<>(Collections.singletonList(

springfox-grails/src/main/java/springfox/documentation/grails/DeleteActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ActionSpecification create(GrailsActionContext context) {
2525
HandlerMethod handlerMethod = actions.get(context.getAction());
2626
return new ActionSpecification(
2727
new HashSet<>(Collections.singletonList(RequestMethod.POST)),
28-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
28+
new HashSet<>(producesOverrides(context)),
2929
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3030
handlerMethod,
3131
new ArrayList<>(Collections.singletonList(

springfox-grails/src/main/java/springfox/documentation/grails/EditActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ActionSpecification create(GrailsActionContext context) {
2626
HandlerMethod handlerMethod = actions.get(context.getAction());
2727
return new ActionSpecification(
2828
new HashSet<>(Arrays.asList(RequestMethod.POST, RequestMethod.PUT)),
29-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)), //May need text/html
29+
new HashSet<>(producesOverrides(context)),
3030
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3131
handlerMethod,
3232
new ArrayList<>(Arrays.asList(

springfox-grails/src/main/java/springfox/documentation/grails/IndexActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ActionSpecification create(GrailsActionContext context) {
2626
HandlerMethod handlerMethod = actions.get(context.getAction());
2727
return new ActionSpecification(
2828
new HashSet<>(Collections.singletonList(RequestMethod.GET)),
29-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
29+
new HashSet<>(producesOverrides(context)),
3030
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3131
handlerMethod,
3232
new ArrayList<>(Collections.singletonList(

springfox-grails/src/main/java/springfox/documentation/grails/MethodBackedActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public ActionSpecification create(GrailsActionContext context) {
2929
HandlerMethod handlerMethod = actions.get(context.getAction());
3030
return new ActionSpecification(
3131
urlProvider.httpMethod(context),
32-
new HashSet<>(),
32+
producesOverrides(context),
3333
new HashSet<>(),
3434
handlerMethod,
3535
handlerMethodResolver.methodParameters(handlerMethod),

springfox-grails/src/main/java/springfox/documentation/grails/PatchActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ActionSpecification create(GrailsActionContext context) {
2626
HandlerMethod handlerMethod = actions.get(context.getAction());
2727
return new ActionSpecification(
2828
new HashSet<>(Arrays.asList(RequestMethod.POST, RequestMethod.PUT)),
29-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
29+
new HashSet<>(producesOverrides(context)),
3030
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3131
handlerMethod,
3232
new ArrayList<>(Arrays.asList(

springfox-grails/src/main/java/springfox/documentation/grails/SaveActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public ActionSpecification create(GrailsActionContext context) {
2626
HandlerMethod handlerMethod = actions.get(context.getAction());
2727
return new ActionSpecification(
2828
new HashSet<>(Arrays.asList(RequestMethod.POST, RequestMethod.PUT)),
29-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
29+
new HashSet<>(producesOverrides(context)),
3030
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3131
handlerMethod,
3232
new ArrayList<>(Arrays.asList(

springfox-grails/src/main/java/springfox/documentation/grails/ShowActionSpecificationFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public ActionSpecification create(GrailsActionContext context) {
2525
HandlerMethod handlerMethod = actions.get(context.getAction());
2626
return new ActionSpecification(
2727
new HashSet<>(Collections.singletonList(RequestMethod.GET)),
28-
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
28+
new HashSet<>(producesOverrides(context)),
2929
new HashSet<>(Collections.singletonList(MediaType.APPLICATION_JSON)),
3030
handlerMethod,
3131
new ArrayList<>(Collections.singletonList(

springfox-grails/src/test/groovy/springfox/documentation/grails/MethodBackedActionSpecificationFactorySpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import grails.core.GrailsDomainClassProperty
77
import grails.web.mapping.LinkGenerator
88
import grails.web.mapping.UrlMapping
99
import grails.web.mapping.UrlMappings
10+
import org.springframework.http.MediaType
1011
import org.springframework.web.bind.annotation.RequestMethod
1112
import spock.lang.Specification
1213

@@ -37,7 +38,7 @@ class MethodBackedActionSpecificationFactorySpec extends Specification {
3738
def spec = sut.create(new GrailsActionContext(controller, domain, "other"))
3839
then:
3940
spec.consumes == [] as Set
40-
spec.produces == [] as Set
41+
spec.produces == [MediaType.APPLICATION_JSON] as Set
4142
spec.supportedMethods == [RequestMethod.POST] as Set
4243
spec.parameters.size() == 2
4344
spec.parameters[0].parameterType == resolver.resolve(Integer)

0 commit comments

Comments
 (0)