Skip to content

Commit

Permalink
added support for vendor extensions with the use of @Info, removed th…
Browse files Browse the repository at this point in the history
…e skipTags query param from SpringfoxLoaderController
  • Loading branch information
jarlehansen committed May 30, 2017
1 parent 4efe2e7 commit 64be230
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -154,8 +154,7 @@ public void init() {

`GET /springfox-loader/api-docs`

Returns the swagger api-docs with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) enabled.
Also has the option to remove the tags from the json response by adding `?skipTags=true` as a query param.
Returns the swagger api-docs with [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) enabled.

### References
* [Springfox Reference Documentation](http://springfox.github.io/springfox/docs/current/)
Expand Down
@@ -1,5 +1,6 @@
package com.github.springfox.loader;

import io.swagger.annotations.Extension;
import io.swagger.annotations.License;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotationUtils;
Expand Down Expand Up @@ -118,6 +119,10 @@ Class<?>[] includeControllers() {
return annotation.includeControllers();
}

Extension[] extensions() {
return annotation.value().extensions();
}

String val(String annotation, String prop) {
String annotationValue = annotation;
if (annotation.matches("\\$\\{(.+)}")) {
Expand Down
Expand Up @@ -4,6 +4,8 @@
import com.github.springfox.loader.plugins.LoaderTagProvider;
import com.github.springfox.loader.valueproperties.ValuePropertiesController;
import com.github.springfox.loader.valueproperties.ValuePropertiesLocator;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -20,14 +22,20 @@
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ObjectVendorExtension;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.readers.operation.DefaultTagsProvider;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@EnableConfigurationProperties
@Configuration
Expand Down Expand Up @@ -89,7 +97,25 @@ public Docket api() {

private ApiInfo apiInfo() {
return new ApiInfo(springfoxLoader.getTitle(), springfoxLoader.getDescription(), springfoxLoader.getVersion(),
springfoxLoader.getTermsOfServiceUrl(), springfoxLoader.getContact(), springfoxLoader.getLicense(), springfoxLoader.getLicenseUrl(), Collections.emptyList());
springfoxLoader.getTermsOfServiceUrl(), springfoxLoader.getContact(), springfoxLoader.getLicense(), springfoxLoader.getLicenseUrl(), getVendorExtensions());
}

private List<VendorExtension> getVendorExtensions() {
Extension[] extensions = springfoxLoader.extensions();
if (extensions.length == 1 && StringUtils.isEmpty(extensions[0].name())) {
return Collections.emptyList();
}

return Arrays.stream(extensions).map(extension -> {
ExtensionProperty[] extensionProperties = extension.properties();
List<StringVendorExtension> vendorExtensions = Arrays.stream(extensionProperties).map(property -> new StringVendorExtension(property.name(), property.value())).collect(Collectors.toList());

ObjectVendorExtension vendorExtension = new ObjectVendorExtension(extension.name());
for (StringVendorExtension stringVendorExtension : vendorExtensions) {
vendorExtension.addProperty(stringVendorExtension);
}
return vendorExtension;
}).collect(Collectors.toList());
}

@Bean
Expand Down
Expand Up @@ -2,7 +2,6 @@

import com.github.springfox.loader.SpringfoxLoaderProps;
import com.google.common.collect.Lists;
import com.jayway.jsonpath.JsonPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpEntity;
Expand All @@ -11,7 +10,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
Expand All @@ -35,15 +33,11 @@ public SpringfoxLoaderController() {
}

@GetMapping("/api-docs")
public ResponseEntity getApiDocs(HttpServletRequest request, @RequestParam(required = false) Boolean skipTags) throws IOException {
public ResponseEntity getApiDocs(HttpServletRequest request) throws IOException {
UriComponents uri = ServletUriComponentsBuilder.fromServletMapping(request).path("/v2/api-docs").build();
ResponseEntity<String> response = restTemplate.exchange(uri.toString(), HttpMethod.GET, new HttpEntity<>(null), String.class);

String json = response.getBody();
if (skipTags != null && skipTags) {
json = JsonPath.parse(json).set("$.tags", new String[]{}).jsonString();
}

return new ResponseEntity<>(json, headers, response.getStatusCode());
}
}
Expand Up @@ -8,9 +8,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.client.RestTemplate
import spock.lang.Specification

import static org.hamcrest.Matchers.hasSize
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

class SpringfoxLoaderControllerSpec extends Specification {
Expand All @@ -32,14 +30,4 @@ class SpringfoxLoaderControllerSpec extends Specification {
1 * restTemplate.exchange('http://localhost/v2/api-docs', HttpMethod.GET, _ as HttpEntity, String) >> ResponseEntity.ok('')
response.andExpect(status().isOk())
}

def "GET api-docs and skip tags"() {
when:
def response = mockMvc.perform(get('/springfox-loader/api-docs').param('skipTags', 'true'))

then:
1 * restTemplate.exchange('http://localhost/v2/api-docs', HttpMethod.GET, _ as HttpEntity, String) >> ResponseEntity.ok('{ "tags": [ "test123", "test234" ] }')
response.andExpect(status().isOk())
.andExpect(jsonPath('$.tags', hasSize(0)))
}
}

0 comments on commit 64be230

Please sign in to comment.