20
20
21
21
package org .springdoc .api ;
22
22
23
- import java .util .List ;
24
- import java .util .Locale ;
25
- import java .util .Map ;
26
- import java .util .Optional ;
23
+ import java .util .*;
27
24
28
25
import io .swagger .v3 .oas .annotations .enums .ParameterIn ;
29
26
import io .swagger .v3 .oas .models .OpenAPI ;
33
30
import io .swagger .v3 .oas .models .media .NumberSchema ;
34
31
import io .swagger .v3 .oas .models .media .StringSchema ;
35
32
import io .swagger .v3 .oas .models .parameters .Parameter ;
33
+ import io .swagger .v3 .oas .models .servers .Server ;
36
34
import org .junit .jupiter .api .BeforeEach ;
37
35
import org .junit .jupiter .api .Test ;
38
36
import org .junit .jupiter .api .extension .ExtendWith ;
39
37
import org .mockito .Mock ;
38
+ import org .mockito .internal .stubbing .answers .CallsRealMethods ;
40
39
import org .mockito .junit .jupiter .MockitoExtension ;
41
- import org .springdoc .core .AbstractRequestService ;
42
- import org .springdoc .core .GenericResponseService ;
43
- import org .springdoc .core .OpenAPIService ;
44
- import org .springdoc .core .OperationService ;
45
- import org .springdoc .core .SpringDocConfigProperties ;
40
+ import org .springdoc .core .*;
41
+ import org .springdoc .core .customizers .OpenApiCustomiser ;
42
+ import org .springdoc .core .customizers .OperationCustomizer ;
46
43
import org .springdoc .core .fn .RouterOperation ;
47
44
48
45
import org .springframework .beans .factory .ObjectFactory ;
49
46
import org .springframework .context .ApplicationContext ;
50
47
import org .springframework .web .bind .annotation .RequestMethod ;
51
48
52
49
import static java .util .Arrays .asList ;
50
+ import static java .util .Collections .singletonList ;
53
51
import static org .hamcrest .MatcherAssert .assertThat ;
54
52
import static org .hamcrest .Matchers .containsInAnyOrder ;
55
53
import static org .hamcrest .Matchers .is ;
56
54
import static org .hamcrest .Matchers .nullValue ;
55
+ import static org .mockito .ArgumentMatchers .any ;
56
+ import static org .mockito .Mockito .doAnswer ;
57
57
import static org .mockito .Mockito .when ;
58
58
import static org .springframework .web .bind .annotation .RequestMethod .GET ;
59
59
@@ -97,8 +97,11 @@ public void setUp() {
97
97
when (openAPIService .getContext ()).thenReturn (context );
98
98
99
99
when (openAPIBuilderObjectFactory .getObject ()).thenReturn (openAPIService );
100
+ }
100
101
101
- resource = new AbstractOpenApiResource (
102
+ @ Test
103
+ void calculatePathFromRouterOperation () {
104
+ resource = new EmptyPathsOpenApiResource (
102
105
GROUP_NAME ,
103
106
openAPIBuilderObjectFactory ,
104
107
requestBuilder ,
@@ -108,15 +111,8 @@ public void setUp() {
108
111
Optional .empty (),
109
112
new SpringDocConfigProperties (),
110
113
Optional .empty ()
111
- ) {
114
+ );
112
115
113
- @ Override
114
- protected void getPaths (final Map <String , Object > findRestControllers , Locale locale ) { }
115
- };
116
- }
117
-
118
- @ Test
119
- void calculatePathFromRouterOperation () {
120
116
final Parameter refParameter = new Parameter ().$ref (PARAMETER_REFERENCE );
121
117
122
118
final Parameter numberParameterInPath = new Parameter ()
@@ -160,4 +156,54 @@ void calculatePathFromRouterOperation() {
160
156
assertThat (parameterWithoutSchema .getSchema (), is (new StringSchema ()));
161
157
assertThat (parameterWithoutSchema .getIn (), is (ParameterIn .QUERY .toString ()));
162
158
}
159
+
160
+ @ Test
161
+ void preLoadingModeShouldNotOverwriteServers () throws InterruptedException {
162
+ when (openAPIService .updateServers (any ())).thenCallRealMethod ();
163
+ when (openAPIService .getCachedOpenAPI ()).thenCallRealMethod ();
164
+ doAnswer (new CallsRealMethods ()).when (openAPIService ).setServersPresent (true );
165
+ doAnswer (new CallsRealMethods ()).when (openAPIService ).setServerBaseUrl (any ());
166
+ doAnswer (new CallsRealMethods ()).when (openAPIService ).setCachedOpenAPI (any ());
167
+
168
+ String customUrl = "https://custom.com" ;
169
+ String generatedUrl = "https://generated.com" ;
170
+ OpenApiCustomiser openApiCustomiser = openApi -> openApi .setServers (singletonList (new Server ().url (customUrl )));
171
+ SpringDocConfigProperties properties = new SpringDocConfigProperties ();
172
+ properties .setPreLoadingEnabled (true );
173
+
174
+ resource = new EmptyPathsOpenApiResource (
175
+ GROUP_NAME ,
176
+ openAPIBuilderObjectFactory ,
177
+ requestBuilder ,
178
+ responseBuilder ,
179
+ operationParser ,
180
+ Optional .empty (),
181
+ Optional .of (singletonList (openApiCustomiser )),
182
+ properties ,
183
+ Optional .empty ()
184
+ );
185
+
186
+ // wait for executor to be done
187
+ Thread .sleep (1_000 );
188
+
189
+ // emulate generating base url
190
+ openAPIService .setServerBaseUrl (generatedUrl );
191
+ openAPIService .updateServers (openAPI );
192
+
193
+ OpenAPI after = resource .getOpenApi (Locale .getDefault ());
194
+
195
+ assertThat (after .getServers ().get (0 ).getUrl (), is (customUrl ));
196
+ }
197
+
198
+
199
+ private static class EmptyPathsOpenApiResource extends AbstractOpenApiResource {
200
+
201
+ EmptyPathsOpenApiResource (String groupName , ObjectFactory <OpenAPIService > openAPIBuilderObjectFactory , AbstractRequestService requestBuilder , GenericResponseService responseBuilder , OperationService operationParser , Optional <List <OperationCustomizer >> operationCustomizers , Optional <List <OpenApiCustomiser >> openApiCustomisers , SpringDocConfigProperties springDocConfigProperties , Optional <ActuatorProvider > actuatorProvider ) {
202
+ super (groupName , openAPIBuilderObjectFactory , requestBuilder , responseBuilder , operationParser , operationCustomizers , openApiCustomisers , springDocConfigProperties , actuatorProvider );
203
+ }
204
+
205
+ @ Override
206
+ public void getPaths (Map <String , Object > findRestControllers , Locale locale ) {
207
+ }
208
+ }
163
209
}
0 commit comments