From 1966640520afb2a0c640f3cbcb59520a093336d3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 21 Nov 2018 15:03:48 +0000 Subject: [PATCH] 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 --- .../restdocs/RestDocumentationExtension.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationExtension.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationExtension.java index f384255a..2ad54a20 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationExtension.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/RestDocumentationExtension.java @@ -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(