Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring: Make sure produces on method mappings are used (master) #723

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -366,25 +367,32 @@ private void processControllerMethod(final AnnotationScannerContext context,
}

static Optional<String[]> getMediaTypes(MethodInfo resourceMethod, String property, String[] defaultValue) {
Set<DotName> annotationNames = SpringConstants.HTTP_METHODS;
Set<DotName> annotationNames = new HashSet<>(SpringConstants.HTTP_METHODS);
annotationNames.add(SpringConstants.REQUEST_MAPPING);

// Check methods
for (DotName annotationName : annotationNames) {
AnnotationInstance annotation = resourceMethod.annotation(annotationName);

if (annotation == null || annotation.value(property) == null) {
annotation = JandexUtil.getClassAnnotation(resourceMethod.declaringClass(), SpringConstants.REQUEST_MAPPING);
}

if (annotation != null) {
AnnotationValue annotationValue = annotation.value(property);

if (annotationValue != null) {
return Optional.of(annotationValue.asStringArray());
}
}
}

return Optional.of(defaultValue);
// Check class
AnnotationInstance annotation = JandexUtil.getClassAnnotation(resourceMethod.declaringClass(),
SpringConstants.REQUEST_MAPPING);
if (annotation != null) {
AnnotationValue annotationValue = annotation.value(property);
if (annotationValue != null) {
return Optional.of(annotationValue.asStringArray());
}

return Optional.of(defaultValue);
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name")
public ResponseEntity<Greeting> helloPathVariableWithResponseTyped(@PathVariable(name = "name") String name) {
return ResponseEntity.ok(new Greeting("Hello " + name));
}

// 7) Test override of produces
@GetMapping(value = "/overrideProduces/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
public String overrideProduces(@PathVariable(name = "name") String name) {
return "Hello " + name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name")
public ResponseEntity<Greeting> helloPathVariableWithResponseTyped(@PathVariable(name = "name") String name) {
return ResponseEntity.ok(new Greeting("Hello " + name));
}

// 7) Test override of produces
@RequestMapping(value = "/overrideProduces/{name}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public String overrideProduces(@PathVariable(name = "name") String name) {
return "Hello " + name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ public ResponseEntity helloPathVariableWithResponse(@PathVariable(name = "name")
public ResponseEntity<Greeting> helloPathVariableWithResponseTyped(@PathVariable(name = "name") String name) {
return ResponseEntity.ok(new Greeting("Hello " + name));
}

// 7) Test override of produces
@RequestMapping(path = "/overrideProduces/{name}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public String overrideProduces(@PathVariable(name = "name") String name) {
return "Hello " + name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@
}
}
}
},
"/greeting/overrideProduces/{name}" : {
"get" : {
"parameters" : [ {
"name" : "name",
"in" : "path",
"required" : true,
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"text/plain" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}

}
},
"components" : {
Expand Down