Skip to content

Add OpenApiBuilderCustomiser #451

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

Merged
merged 4 commits into from
Mar 1, 2020
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 @@ -114,11 +114,7 @@ protected synchronized OpenAPI getOpenApi() {
if (!computeDone || springDocConfigProperties.getCache().isDisabled()) {
Instant start = Instant.now();
openAPIBuilder.build();
Map<String, Object> restControllersMap = openAPIBuilder.getRestControllersMap();
Map<String, Object> requestMappingMap = openAPIBuilder.getRequestMappingMap();
Map<String, Object> controllerMap = openAPIBuilder.getControllersMap();
Map<String, Object> restControllers = Stream.of(restControllersMap, requestMappingMap, controllerMap)
.flatMap(mapEl -> mapEl.entrySet().stream())
Map<String, Object> mappingsMap = openAPIBuilder.getMappingsMap().entrySet().stream()
.filter(controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(),
Hidden.class) == null))
.filter(controller -> !isHiddenRestControllers(controller.getValue().getClass()))
Expand All @@ -128,7 +124,7 @@ protected synchronized OpenAPI getOpenApi() {
// calculate generic responses
responseBuilder.buildGenericResponse(openAPIBuilder.getComponents(), findControllerAdvice);

getPaths(restControllers);
getPaths(mappingsMap);
openApi = openAPIBuilder.getCalculatedOpenAPI();

// run the optional customisers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springdoc.core.customizers.OpenApiBuilderCustomiser;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -81,18 +82,24 @@ public class OpenAPIBuilder {

private final SecurityParser securityParser;

private final Map<String, Object> mappingsMap = new HashMap<>();

private final Map<HandlerMethod, io.swagger.v3.oas.models.tags.Tag> springdocTags = new HashMap<>();

private final Optional<SecurityOAuth2Provider> springSecurityOAuth2Provider;

private final List<OpenApiBuilderCustomiser> openApiBuilderCustomisers;

private boolean isServersPresent;

private String serverBaseUrl;

private final SpringDocConfigProperties springDocConfigProperties;

@SuppressWarnings("WeakerAccess")
OpenAPIBuilder(Optional<OpenAPI> openAPI, ApplicationContext context, SecurityParser securityParser, Optional<SecurityOAuth2Provider> springSecurityOAuth2Provider, SpringDocConfigProperties springDocConfigProperties) {
OpenAPIBuilder(Optional<OpenAPI> openAPI, ApplicationContext context, SecurityParser securityParser,
Optional<SecurityOAuth2Provider> springSecurityOAuth2Provider, SpringDocConfigProperties springDocConfigProperties,
List<OpenApiBuilderCustomiser> openApiBuilderCustomisers) {
if (openAPI.isPresent()) {
this.openAPI = openAPI.get();
if (this.openAPI.getComponents() == null)
Expand All @@ -106,6 +113,7 @@ public class OpenAPIBuilder {
this.securityParser = securityParser;
this.springSecurityOAuth2Provider = springSecurityOAuth2Provider;
this.springDocConfigProperties = springDocConfigProperties;
this.openApiBuilderCustomisers = openApiBuilderCustomisers;
}

private static String splitCamelCase(String str) {
Expand Down Expand Up @@ -146,12 +154,18 @@ else if (calculatedOpenAPI.getInfo() == null) {
Info infos = new Info().title(DEFAULT_TITLE).version(DEFAULT_VERSION);
calculatedOpenAPI.setInfo(infos);
}
// Set default mappings
this.mappingsMap.putAll(context.getBeansWithAnnotation(RestController.class));
this.mappingsMap.putAll(context.getBeansWithAnnotation(RequestMapping.class));
this.mappingsMap.putAll(context.getBeansWithAnnotation(Controller.class));

// default server value
if (CollectionUtils.isEmpty(calculatedOpenAPI.getServers()) || !isServersPresent) {
this.updateServers(calculatedOpenAPI);
}
// add security schemes
this.calculateSecuritySchemes(calculatedOpenAPI.getComponents());
Optional.ofNullable(this.openApiBuilderCustomisers).ifPresent(customisers -> customisers.forEach(customiser -> customiser.customise(this)));
}

public void updateServers(OpenAPI openAPI) {
Expand Down Expand Up @@ -397,16 +411,12 @@ public void addTag(Set<HandlerMethod> handlerMethods, io.swagger.v3.oas.models.t
handlerMethods.forEach(handlerMethod -> springdocTags.put(handlerMethod, tag));
}

public Map<String, Object> getRestControllersMap() {
return context.getBeansWithAnnotation(RestController.class);
}

public Map<String, Object> getRequestMappingMap() {
return context.getBeansWithAnnotation(RequestMapping.class);
public Map<String, Object> getMappingsMap() {
return this.mappingsMap;
}

public Map<String, Object> getControllersMap() {
return context.getBeansWithAnnotation(Controller.class);
public void addMappings(Map<String, Object> mappings) {
this.mappingsMap.putAll(mappings);
}

public Map<String, Object> getControllerAdviceMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springdoc.core.converters.ModelConverterRegistrar;
import org.springdoc.core.converters.PropertyCustomizingConverter;
import org.springdoc.core.converters.ResponseSupportConverter;
import org.springdoc.core.customizers.OpenApiBuilderCustomiser;
import org.springdoc.core.customizers.PropertyCustomizer;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
Expand Down Expand Up @@ -82,8 +83,10 @@ IgnoredParameterAnnotationsDefault ignoredParameterAnnotationsDefault() {
}

@Bean
public OpenAPIBuilder openAPIBuilder(Optional<OpenAPI> openAPI, ApplicationContext context, SecurityParser securityParser, Optional<SecurityOAuth2Provider> springSecurityOAuth2Provider,SpringDocConfigProperties springDocConfigProperties) {
return new OpenAPIBuilder(openAPI, context, securityParser, springSecurityOAuth2Provider,springDocConfigProperties);
public OpenAPIBuilder openAPIBuilder(Optional<OpenAPI> openAPI, ApplicationContext context, SecurityParser securityParser,
Optional<SecurityOAuth2Provider> springSecurityOAuth2Provider, SpringDocConfigProperties springDocConfigProperties,
List<OpenApiBuilderCustomiser> openApiBuilderCustomisers) {
return new OpenAPIBuilder(openAPI, context, securityParser, springSecurityOAuth2Provider,springDocConfigProperties, openApiBuilderCustomisers);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package org.springdoc.core.customizers;

import org.springdoc.core.OpenAPIBuilder;

public interface OpenApiBuilderCustomiser {
void customise(OpenAPIBuilder openApiBuilder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,31 @@
@AutoConfigureMockMvc
public abstract class AbstractSpringDocTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractSpringDocTest.class);

public static String className;
public static String className;

@Autowired
protected MockMvc mockMvc;
@Autowired
protected MockMvc mockMvc;

public static String getContent(String fileName) throws Exception {
try {
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
}
catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
}
}
public static String getContent(String fileName) throws Exception {
try {
Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
byte[] fileBytes = Files.readAllBytes(path);
return new String(fileBytes, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException("Failed to read file: " + fileName, e);
}
}

@Test
public void testApp() throws Exception {
className = getClass().getSimpleName();
String testNumber = className.replaceAll("[^0-9]", "");
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
String result = mockMvcResult.getResponse().getContentAsString();
String expected = getContent("results/app" + testNumber + ".json");
assertEquals(expected, result, true);
}
@Test
public void testApp() throws Exception {
className = getClass().getSimpleName();
String testNumber = className.replaceAll("[^0-9]", "");
MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk()).andExpect(jsonPath("$.openapi", is("3.0.1")))
.andReturn();
String result = mockMvcResult.getResponse().getContentAsString();
String expected = getContent("results/app" + testNumber + ".json");
assertEquals(expected, result, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package test.org.springdoc.api.app92;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.RandomStringUtils;
import org.springdoc.api.ActuatorProvider;
import org.springdoc.api.OpenApiResource;
import org.springdoc.core.AbstractRequestBuilder;
import org.springdoc.core.GenericResponseBuilder;
import org.springdoc.core.OpenAPIBuilder;
import org.springdoc.core.OperationBuilder;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.customizers.OpenApiBuilderCustomiser;
import org.springdoc.core.customizers.OpenApiCustomiser;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import test.org.springdoc.api.AbstractSpringDocTest;
import test.org.springdoc.api.app91.Greeting;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.springdoc.core.Constants.DEFAULT_GROUP_NAME;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@TestPropertySource(properties = "springdoc.default-produces-media-type=application/json")
public class SpringDocApp92Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp implements ApplicationContextAware {

private ApplicationContext applicationContext;

@Bean
public GreetingController greetingController() {
return new GreetingController();
}

@Bean
public OpenApiBuilderCustomiser customOpenAPI() {
return openApiBuilder -> openApiBuilder.addMappings(Collections.singletonMap("greetingController", new GreetingController()));
}

@Bean
public RequestMappingHandlerMapping defaultTestHandlerMapping(GreetingController greetingController) throws NoSuchMethodException {
RequestMappingHandlerMapping result = new RequestMappingHandlerMapping();
RequestMappingInfo requestMappingInfo =
RequestMappingInfo.paths("/test").methods(RequestMethod.GET).produces(MediaType.APPLICATION_JSON_VALUE).build();

result.setApplicationContext(this.applicationContext);
result.registerMapping(requestMappingInfo, "greetingController", GreetingController.class.getDeclaredMethod("sayHello2"));
//result.handlerme
return result;
}

@Bean(name = "mvcOpenApiResource")
public OpenApiResource openApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder requestBuilder, GenericResponseBuilder responseBuilder,
OperationBuilder operationParser,
@Qualifier("defaultTestHandlerMapping") RequestMappingHandlerMapping requestMappingHandlerMapping,
Optional<ActuatorProvider> servletContextProvider, SpringDocConfigProperties springDocConfigProperties,
Optional<List<OpenApiCustomiser>> openApiCustomisers) {
return new OpenApiResource(DEFAULT_GROUP_NAME, openAPIBuilder, requestBuilder, responseBuilder, operationParser, requestMappingHandlerMapping,
servletContextProvider, openApiCustomisers, springDocConfigProperties);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

@ResponseBody
@Tag(name = "Demo", description = "The Demo API")
public static class GreetingController {

@GetMapping(produces = APPLICATION_JSON_VALUE)
@Operation(summary = "This API will return a random greeting.")
public ResponseEntity<Greeting> sayHello() {
return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10)));
}

@GetMapping("/test")
@ApiResponses(value = { @ApiResponse(responseCode = "201", description = "item created"),
@ApiResponse(responseCode = "400", description = "invalid input, object invalid"),
@ApiResponse(responseCode = "409", description = "an existing item already exists") })
public ResponseEntity<Greeting> sayHello2() {
return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10)));
}

}
}
Loading