Skip to content

Commit

Permalink
Removed deprecated api in PathProvider
Browse files Browse the repository at this point in the history
Also simplified the usages as a result of no longer needing servlet context to resolve the paths

(1773)
  • Loading branch information
dilipkrish committed Aug 18, 2018
1 parent 81170d3 commit 33aa1d3
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 122 deletions.
Expand Up @@ -21,13 +21,6 @@

public interface PathProvider {

/**
* Gets the application base path
* @return application base path
*/
@Deprecated
String getApplicationBasePath();

/**
* Gets the sanitized and prepended with a "/" operation path
* @param operationPath - raw operation path
Expand Down
Expand Up @@ -26,20 +26,10 @@


public abstract class AbstractPathProvider implements PathProvider {
/**
* For relative PathProviders this is typically '/' meaning relative to the swagger ui page serving the
* documentation. The swagger specification recommends that this should be an absolute URL.
*
* Corresponds to the base path attribute of a swagger api declaration.
* This is the actual base path serving the api (not the swagger documentation)
*
* @return the applications base uri
*/
protected abstract String applicationPath();

/**
* The base path to the swagger api documentation.
*
* <p>
* Typically docs are served from &lt;yourApp&gt;/api-docs so a relative resourceListing path will omit the api-docs
* segment.
* E.g.
Expand All @@ -48,22 +38,18 @@ public abstract class AbstractPathProvider implements PathProvider {
*
* @return the documentation base path
*/
protected abstract String getDocumentationPath();

@Override
public String getApplicationBasePath() {
return applicationPath();
protected String getDocumentationPath() {
return ROOT;
}

/**
* The relative path to the operation, from the basePath, which this operation describes.
* The value SHOULD be in a relative (URL) path format.
*
* <p>
* Includes the apiResourcePrefix
*
* @param operationPath operation path
* @return the relative path to the api operation
* @see AbstractPathProvider#getApplicationBasePath()
*/
@Override
public String getOperationPath(String operationPath) {
Expand All @@ -73,7 +59,7 @@ public String getOperationPath(String operationPath) {

/**
* Corresponds to the path attribute of a swagger Resource Object (within a Resource Listing).
*
* <p>
* This method builds a URL based off of
*
* @param groupName the group name for this Resource Object e.g. 'default'
Expand Down
Expand Up @@ -62,15 +62,5 @@ class DocumentationContextSpec extends Specification {
}

class DummyPathProvider extends AbstractPathProvider {

@Override
protected String applicationPath() {
return ROOT
}

@Override
protected String getDocumentationPath() {
return ROOT
}
}
}
Expand Up @@ -22,14 +22,4 @@
import static springfox.documentation.spring.web.paths.Paths.*;

public class WebFluxRelativePathProvider extends AbstractPathProvider {

@Override
protected String applicationPath() {
return ROOT;
}

@Override
protected String getDocumentationPath() {
return ROOT;
}
}
Expand Up @@ -19,26 +19,5 @@

package springfox.documentation.spring.web.paths;

import javax.servlet.ServletContext;

import static org.springframework.util.StringUtils.*;
import static springfox.documentation.spring.web.paths.Paths.*;

public class WebMvcRelativePathProvider extends AbstractPathProvider {
private final ServletContext servletContext;

public WebMvcRelativePathProvider(ServletContext servletContext) {
super();
this.servletContext = servletContext;
}

@Override
protected String applicationPath() {
return isEmpty(servletContext.getContextPath()) ? ROOT : servletContext.getContextPath();
}

@Override
protected String getDocumentationPath() {
return ROOT;
}
}
Expand Up @@ -18,23 +18,13 @@
*/
package springfox.documentation.spring.web.paths;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.PathProvider;

import javax.servlet.ServletContext;

@Component
public class WebMvcRelativePathProviderFactory implements PathProviderFactory {
private final ServletContext servletContext;

@Autowired
public WebMvcRelativePathProviderFactory(ServletContext servletContext) {
this.servletContext = servletContext;
}

@Override
public PathProvider getInstance() {
return new WebMvcRelativePathProvider(servletContext);
return new WebMvcRelativePathProvider();
}
}
Expand Up @@ -30,11 +30,10 @@ class RelativeSwaggerPathProviderSpec extends Specification {
def "assert urls"(){
given:
ServletContext servletContext = Mock()
WebMvcRelativePathProvider provider = new WebMvcRelativePathProvider(servletContext)
WebMvcRelativePathProvider provider = new WebMvcRelativePathProvider()
servletContext.contextPath >> "/"

expect:
provider.applicationPath() == "/"
provider.getDocumentationPath() == "/"
}

Expand Down
Expand Up @@ -31,17 +31,16 @@ class WebMvcRelativePathProviderSpec extends Specification {
given:
ServletContext servletContext = Mock(ServletContext)
servletContext.contextPath >> "/"
AbstractPathProvider provider = new WebMvcRelativePathProvider(servletContext)
AbstractPathProvider provider = new WebMvcRelativePathProvider()

expect:
provider.getApplicationBasePath() == "/"
provider.getResourceListingPath('default', 'api-declaration') == "/default/api-declaration"
}


def "should never return a path with duplicate slash"() {
setup:
WebMvcRelativePathProvider swaggerPathProvider = new WebMvcRelativePathProvider(servletContext())
WebMvcRelativePathProvider swaggerPathProvider = new WebMvcRelativePathProvider()

when:
String path = swaggerPathProvider.getResourceListingPath('/a', '/b')
Expand Down
Expand Up @@ -40,7 +40,6 @@ import springfox.documentation.spi.service.contexts.Defaults
import springfox.documentation.spi.service.contexts.SecurityContext
import springfox.documentation.spring.web.paths.WebMvcRelativePathProvider

import javax.servlet.ServletContext
import javax.servlet.ServletRequest

import static java.util.Collections.*
Expand Down Expand Up @@ -183,23 +182,23 @@ class DocketSpec extends DocumentationContextSpec {
documentationContext()."$property" == object || (documentationContext()."$property" == [object] as Set)

where:
builderMethod | object | property
'pathProvider' | new WebMvcRelativePathProvider(Mock(ServletContext)) | 'pathProvider'
'securitySchemes' | new ArrayList<SecurityScheme>() | 'securitySchemes'
'securityContexts' | validContexts() | 'securityContexts'
'groupName' | 'someGroup' | 'groupName'
'apiInfo' | new ApiInfo('', '', "", '', '', '', '') | 'apiInfo'
'apiDescriptionOrdering' | apiDescriptionOrdering() | 'apiDescriptionOrdering'
'operationOrdering' | operationOrdering() | 'operationOrdering'
'produces' | ['application/json'] as Set | 'produces'
'consumes' | ['application/json'] as Set | 'consumes'
'host' | 'someHost' | 'host'
'protocols' | ['application/json'] as Set | 'protocols'
'additionalModels' | Mock(ResolvedType) | 'additionalModels'
'enableUrlTemplating' | true | 'isUriTemplatesEnabled'
'tags' | new Tag("test", "test") | 'tags'
'globalOperationParameters' | [Mock(Parameter)] | 'globalOperationParameters'
'extensions' | extensions() | 'vendorExtensions'
builderMethod | object | property
'pathProvider' | new WebMvcRelativePathProvider() | 'pathProvider'
'securitySchemes' | new ArrayList<SecurityScheme>() | 'securitySchemes'
'securityContexts' | validContexts() | 'securityContexts'
'groupName' | 'someGroup' | 'groupName'
'apiInfo' | new ApiInfo('', '', "", '', '', '', '') | 'apiInfo'
'apiDescriptionOrdering' | apiDescriptionOrdering() | 'apiDescriptionOrdering'
'operationOrdering' | operationOrdering() | 'operationOrdering'
'produces' | ['application/json'] as Set | 'produces'
'consumes' | ['application/json'] as Set | 'consumes'
'host' | 'someHost' | 'host'
'protocols' | ['application/json'] as Set | 'protocols'
'additionalModels' | Mock(ResolvedType) | 'additionalModels'
'enableUrlTemplating' | true | 'isUriTemplatesEnabled'
'tags' | new Tag("test", "test") | 'tags'
'globalOperationParameters' | [Mock(Parameter)] | 'globalOperationParameters'
'extensions' | extensions() | 'vendorExtensions'
}

List<VendorExtension> extensions() {
Expand Down
Expand Up @@ -36,8 +36,6 @@ import springfox.documentation.spring.web.mixins.ServicePluginsSupport
import springfox.documentation.spring.web.paths.WebMvcRelativePathProvider
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator

import javax.servlet.ServletContext

import static java.util.Optional.*

@Mixin(ServicePluginsSupport)
Expand Down Expand Up @@ -139,7 +137,7 @@ class DocumentationPluginsManagerSpec extends Specification {
def pathContext = Mock(PathContext)
def context = Mock(DocumentationContext)
and:
pathContext.pathProvider() >> new WebMvcRelativePathProvider(Mock(ServletContext))
pathContext.pathProvider() >> new WebMvcRelativePathProvider()
pathContext.documentationContext() >> context
context.getPathMapping() >> empty()
pathContext.parameters >> []
Expand Down
Expand Up @@ -34,8 +34,6 @@ import springfox.documentation.spring.web.plugins.DocumentationContextSpec
import springfox.documentation.spring.web.readers.operation.ApiOperationReader
import springfox.documentation.spring.web.readers.operation.HandlerMethodResolver

import javax.servlet.ServletContext

@Mixin([RequestMappingSupport, ServicePluginsSupport])
class ApiDescriptionReaderSpec extends DocumentationContextSpec {

Expand Down Expand Up @@ -77,8 +75,8 @@ class ApiDescriptionReaderSpec extends DocumentationContextSpec {
!secondApiDescription.isHidden()

where:
pathProvider | prefix
new WebMvcRelativePathProvider(Mock(ServletContext)) | ""
pathProvider | prefix
new WebMvcRelativePathProvider() | ""
}

def "should handle exceptions gracefully"() {
Expand Down
Expand Up @@ -50,7 +50,7 @@ class ApiListingReferenceScannerSpec extends DocumentationContextSpec {
contextBuilder.requestHandlers(requestHandlers)
.withResourceGroupingStrategy(new SpringGroupingStrategy())
plugin
.pathProvider(new WebMvcRelativePathProvider(servletContext()))
.pathProvider(new WebMvcRelativePathProvider())
.select()
.apis(withClassAnnotation(ApiIgnore).negate())
.paths(regex(".*?"))
Expand Down
Expand Up @@ -127,7 +127,7 @@ class SwaggerApiDocumentationScannerSpec extends DocumentationContextSpec {

def "resource with mocked apis"() {
given:
AbstractPathProvider pathProvider = new WebMvcRelativePathProvider(servletContext())
AbstractPathProvider pathProvider = new WebMvcRelativePathProvider()
plugin
.groupName("groupName")
.select()
Expand Down
Expand Up @@ -94,7 +94,7 @@ class ModelAttributeParameterExpanderSpec extends DocumentationContextSpec {
SwaggerDefaults(Defaults defaults, TypeResolver typeResolver, ServletContext servletContext) {
this.typeResolver = typeResolver
defaultConfiguration = new DefaultConfiguration(defaults, typeResolver,
new WebMvcRelativePathProviderFactory(servletContext))
new WebMvcRelativePathProviderFactory())
}

@Override
Expand Down
Expand Up @@ -53,7 +53,7 @@ class SwaggerApiListingReferenceScannerSpec extends DocumentationContextSpec {
contextBuilder.requestHandlers(requestHandlers)
.withResourceGroupingStrategy(new ClassOrApiAnnotationResourceGrouping())
plugin
.pathProvider(new WebMvcRelativePathProvider(servletContext()))
.pathProvider(new WebMvcRelativePathProvider())
.select()
.apis(withClassAnnotation(ApiIgnore).negate())
.paths(regex(".*?"))
Expand Down
Expand Up @@ -36,7 +36,6 @@ import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.spring.web.plugins.DocumentationContextSpec
import springfox.documentation.swagger1.web.SwaggerDefaultConfiguration

import javax.servlet.ServletContext
import javax.servlet.ServletRequest

import static java.util.Collections.*
Expand Down Expand Up @@ -168,13 +167,13 @@ class DocketSpec extends DocumentationContextSpec {
documentationContext()."$property" == object

where:
builderMethod | object | property
'pathProvider' | new WebMvcRelativePathProvider(Mock(ServletContext)) | 'pathProvider'
'securitySchemes' | new ArrayList<SecurityScheme>() | 'securitySchemes'
'securityContexts'| validContexts() | 'securityContexts'
'groupName' | 'someGroup' | 'groupName'
'apiInfo' | new ApiInfo('', '', "", '', '', '', '') | 'apiInfo'
'apiInfo' | ApiInfo.DEFAULT | 'apiInfo'
builderMethod | object | property
'pathProvider' | new WebMvcRelativePathProvider() | 'pathProvider'
'securitySchemes' | new ArrayList<SecurityScheme>() | 'securitySchemes'
'securityContexts'| validContexts() | 'securityContexts'
'groupName' | 'someGroup' | 'groupName'
'apiInfo' | new ApiInfo('', '', "", '', '', '', '') | 'apiInfo'
'apiInfo' | ApiInfo.DEFAULT | 'apiInfo'
}

def validContexts() {
Expand Down
Expand Up @@ -26,6 +26,6 @@ import javax.servlet.ServletContext
@SuppressWarnings("GrMethodMayBeStatic")
class SwaggerPathProviderSupport {
WebMvcRelativePathProvider relativeSwaggerPathProvider(ServletContext servletContext) {
new WebMvcRelativePathProvider(servletContext)
new WebMvcRelativePathProvider()
}
}
Expand Up @@ -84,7 +84,7 @@ class Swagger2ControllerSpec extends DocumentationContextSpec
def req = servletRequestWithXHeaders(prefix)
def defaultConfiguration = new DefaultConfiguration(new Defaults(), new TypeResolver(),
new WebMvcRelativePathProviderFactory(req.servletContext))
new WebMvcRelativePathProviderFactory())
this.contextBuilder = defaultConfiguration.create(DocumentationType.SWAGGER_12)
.requestHandlers([])
.operationOrdering(nickNameComparator())
Expand Down

0 comments on commit 33aa1d3

Please sign in to comment.