|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2019-2020 the original author or authors. |
| 4 | + * * |
| 5 | + * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * * you may not use this file except in compliance with the License. |
| 7 | + * * You may obtain a copy of the License at |
| 8 | + * * |
| 9 | + * * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * * |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * * See the License for the specific language governing permissions and |
| 15 | + * * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | +package test.org.springdoc.api.app92; |
| 19 | + |
| 20 | +import io.swagger.v3.oas.annotations.Operation; |
| 21 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 22 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 23 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 24 | +import org.apache.commons.lang3.RandomStringUtils; |
| 25 | +import org.springdoc.api.ActuatorProvider; |
| 26 | +import org.springdoc.api.OpenApiResource; |
| 27 | +import org.springdoc.core.AbstractRequestBuilder; |
| 28 | +import org.springdoc.core.GenericResponseBuilder; |
| 29 | +import org.springdoc.core.OpenAPIBuilder; |
| 30 | +import org.springdoc.core.OperationBuilder; |
| 31 | +import org.springdoc.core.SpringDocConfigProperties; |
| 32 | +import org.springdoc.core.customizers.OpenApiBuilderCustomiser; |
| 33 | +import org.springdoc.core.customizers.OpenApiCustomiser; |
| 34 | +import org.springframework.beans.BeansException; |
| 35 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 36 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 37 | +import org.springframework.context.ApplicationContext; |
| 38 | +import org.springframework.context.ApplicationContextAware; |
| 39 | +import org.springframework.context.annotation.Bean; |
| 40 | +import org.springframework.http.MediaType; |
| 41 | +import org.springframework.http.ResponseEntity; |
| 42 | +import org.springframework.test.context.TestPropertySource; |
| 43 | +import org.springframework.web.bind.annotation.GetMapping; |
| 44 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 45 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 46 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 47 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 48 | +import test.org.springdoc.api.AbstractSpringDocTest; |
| 49 | +import test.org.springdoc.api.app91.Greeting; |
| 50 | + |
| 51 | +import java.util.Collections; |
| 52 | +import java.util.List; |
| 53 | +import java.util.Optional; |
| 54 | + |
| 55 | +import static org.springdoc.core.Constants.DEFAULT_GROUP_NAME; |
| 56 | +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; |
| 57 | + |
| 58 | +@TestPropertySource(properties = "springdoc.default-produces-media-type=application/json") |
| 59 | +public class SpringDocApp92Test extends AbstractSpringDocTest { |
| 60 | + |
| 61 | + @SpringBootApplication |
| 62 | + static class SpringDocTestApp implements ApplicationContextAware { |
| 63 | + |
| 64 | + private ApplicationContext applicationContext; |
| 65 | + |
| 66 | + @Bean |
| 67 | + public GreetingController greetingController() { |
| 68 | + return new GreetingController(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + public OpenApiBuilderCustomiser customOpenAPI() { |
| 73 | + return openApiBuilder -> openApiBuilder.addMappings(Collections.singletonMap("greetingController", new GreetingController())); |
| 74 | + } |
| 75 | + |
| 76 | + @Bean |
| 77 | + public RequestMappingHandlerMapping defaultTestHandlerMapping(GreetingController greetingController) throws NoSuchMethodException { |
| 78 | + RequestMappingHandlerMapping result = new RequestMappingHandlerMapping(); |
| 79 | + RequestMappingInfo requestMappingInfo = |
| 80 | + RequestMappingInfo.paths("/test").methods(RequestMethod.GET).produces(MediaType.APPLICATION_JSON_VALUE).build(); |
| 81 | + |
| 82 | + result.setApplicationContext(this.applicationContext); |
| 83 | + result.registerMapping(requestMappingInfo, "greetingController", GreetingController.class.getDeclaredMethod("sayHello2")); |
| 84 | + //result.handlerme |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + @Bean(name = "mvcOpenApiResource") |
| 89 | + public OpenApiResource openApiResource(OpenAPIBuilder openAPIBuilder, AbstractRequestBuilder requestBuilder, GenericResponseBuilder responseBuilder, |
| 90 | + OperationBuilder operationParser, |
| 91 | + @Qualifier("defaultTestHandlerMapping") RequestMappingHandlerMapping requestMappingHandlerMapping, |
| 92 | + Optional<ActuatorProvider> servletContextProvider, SpringDocConfigProperties springDocConfigProperties, |
| 93 | + Optional<List<OpenApiCustomiser>> openApiCustomisers) { |
| 94 | + return new OpenApiResource(DEFAULT_GROUP_NAME, openAPIBuilder, requestBuilder, responseBuilder, operationParser, requestMappingHandlerMapping, |
| 95 | + servletContextProvider, openApiCustomisers, springDocConfigProperties); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 100 | + this.applicationContext = applicationContext; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @ResponseBody |
| 105 | + @Tag(name = "Demo", description = "The Demo API") |
| 106 | + public static class GreetingController { |
| 107 | + |
| 108 | + @GetMapping(produces = APPLICATION_JSON_VALUE) |
| 109 | + @Operation(summary = "This API will return a random greeting.") |
| 110 | + public ResponseEntity<Greeting> sayHello() { |
| 111 | + return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10))); |
| 112 | + } |
| 113 | + |
| 114 | + @GetMapping("/test") |
| 115 | + @ApiResponses(value = { @ApiResponse(responseCode = "201", description = "item created"), |
| 116 | + @ApiResponse(responseCode = "400", description = "invalid input, object invalid"), |
| 117 | + @ApiResponse(responseCode = "409", description = "an existing item already exists") }) |
| 118 | + public ResponseEntity<Greeting> sayHello2() { |
| 119 | + return ResponseEntity.ok(new Greeting(RandomStringUtils.randomAlphanumeric(10))); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | +} |
0 commit comments