Only attempt parameter resolution in a test method context

Previously, RestDocumenationExtension would attempt parameter resolution
for any use RestDocumentationContextProvider. Due to the provider
being method-scoped, this could lead to a NullPointerException when
it was resolved in the context of a class.

This commit updates RestDocumentationExtension so that it only supports
parameter resolution for RestDocumentationContextProvider in the context
of a test method.

Closes gh-538
This commit is contained in:
Andy Wilkinson
2018-11-21 15:03:48 +00:00
parent d7cb857d61
commit 1966640520

View File

@@ -47,8 +47,11 @@ public class RestDocumentationExtension
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
return RestDocumentationContextProvider.class
.isAssignableFrom(parameterContext.getParameter().getType());
if (isTestMethodContext(extensionContext)) {
return RestDocumentationContextProvider.class
.isAssignableFrom(parameterContext.getParameter().getType());
}
return false;
}
@Override
@@ -58,6 +61,10 @@ public class RestDocumentationExtension
.beforeOperation();
}
private boolean isTestMethodContext(ExtensionContext context) {
return context.getTestClass().isPresent() && context.getTestMethod().isPresent();
}
private ManualRestDocumentation getDelegate(ExtensionContext context) {
Namespace namespace = Namespace.create(getClass(), context.getUniqueId());
return context.getStore(namespace).getOrComputeIfAbsent(