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