Skip to content

Commit f0cffd6

Browse files
committed
[CXF-9175] added test, renamed attribute, fixed PMDM issues
1 parent e887a2e commit f0cffd6

File tree

4 files changed

+86
-34
lines changed

4 files changed

+86
-34
lines changed

rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfig.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class SwaggerUiConfig {
6868
private Boolean queryConfigEnabled;
6969
// Controls the OAuth config. If present, the SwaggerUIBundle initialization will contain a call to initOAuth
7070
// with the parameters contained here
71-
private SwaggerUiOAuth2Config oAuthConfig;
71+
private SwaggerUiOAuth2Config oAuth2Config;
7272

7373
public String getConfigUrl() {
7474
return configUrl;
@@ -340,16 +340,16 @@ public void setTryItOutEnabled(Boolean tryItOutEnabled) {
340340
setQueryConfigEnabledIfNeeded();
341341
}
342342

343-
public SwaggerUiOAuth2Config getOAuthConfig() {
344-
return oAuthConfig;
343+
public SwaggerUiOAuth2Config getOAuth2Config() {
344+
return oAuth2Config;
345345
}
346346

347-
public void setOAuthConfig(SwaggerUiOAuth2Config oAuthConfig) {
348-
this.oAuthConfig = oAuthConfig;
347+
public void setOAuth2Config(SwaggerUiOAuth2Config oauth) {
348+
this.oAuth2Config = oauth;
349349
}
350350

351-
public SwaggerUiConfig oAuthConfig(SwaggerUiOAuth2Config oAuthConfig) {
352-
setOAuthConfig(oAuthConfig);
351+
public SwaggerUiConfig oAuth2Config(SwaggerUiOAuth2Config oauth) {
352+
setOAuth2Config(oauth);
353353
return this;
354354
}
355355
}

rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiOAuth2Config.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
120
package org.apache.cxf.jaxrs.swagger.ui;
221

322
import java.util.List;
@@ -28,8 +47,8 @@ public void setClientId(String clientId) {
2847
this.clientId = clientId;
2948
}
3049

31-
public SwaggerUiOAuth2Config clientId(String clientId) {
32-
setClientId(clientId);
50+
public SwaggerUiOAuth2Config clientId(String cid) {
51+
setClientId(cid);
3352
return this;
3453
}
3554

@@ -41,8 +60,8 @@ public void setClientSecret(String clientSecret) {
4160
this.clientSecret = clientSecret;
4261
}
4362

44-
public SwaggerUiOAuth2Config clientSecret(String clientSecret) {
45-
setClientSecret(clientSecret);
63+
public SwaggerUiOAuth2Config clientSecret(String secret) {
64+
setClientSecret(secret);
4665
return this;
4766
}
4867

@@ -54,8 +73,8 @@ public void setRealm(String realm) {
5473
this.realm = realm;
5574
}
5675

57-
public SwaggerUiOAuth2Config realm(String realm) {
58-
setRealm(realm);
76+
public SwaggerUiOAuth2Config realm(String re) {
77+
setRealm(re);
5978
return this;
6079
}
6180

@@ -67,8 +86,8 @@ public void setAppName(String appName) {
6786
this.appName = appName;
6887
}
6988

70-
public SwaggerUiOAuth2Config appName(String appName) {
71-
setAppName(appName);
89+
public SwaggerUiOAuth2Config appName(String name) {
90+
setAppName(name);
7291
return this;
7392
}
7493

@@ -80,8 +99,8 @@ public void setScopes(List<String> scopes) {
8099
this.scopes = scopes;
81100
}
82101

83-
public SwaggerUiOAuth2Config scopes(List<String> scopes) {
84-
setScopes(scopes);
102+
public SwaggerUiOAuth2Config scopes(List<String> scopesList) {
103+
setScopes(scopesList);
85104
return this;
86105
}
87106

@@ -93,8 +112,8 @@ public void setAdditionalQueryStringParams(Map<String, String> additionalQuerySt
93112
this.additionalQueryStringParams = additionalQueryStringParams;
94113
}
95114

96-
public SwaggerUiOAuth2Config additionalQueryStringParams(Map<String, String> additionalQueryStringParams) {
97-
setAdditionalQueryStringParams(additionalQueryStringParams);
115+
public SwaggerUiOAuth2Config additionalQueryStringParams(Map<String, String> additionalParams) {
116+
setAdditionalQueryStringParams(additionalParams);
98117
return this;
99118
}
100119

@@ -106,8 +125,8 @@ public void setUseBasicAuthenticationWithAccessCodeGrant(Boolean useBasicAuthent
106125
this.useBasicAuthenticationWithAccessCodeGrant = useBasicAuthenticationWithAccessCodeGrant;
107126
}
108127

109-
public SwaggerUiOAuth2Config useBasicAuthenticationWithAccessCodeGrant(Boolean useBasicAuthenticationWithAccessCodeGrant) {
110-
setUseBasicAuthenticationWithAccessCodeGrant(useBasicAuthenticationWithAccessCodeGrant);
128+
public SwaggerUiOAuth2Config useBasicAuthenticationWithAccessCodeGrant(Boolean basicAuth) {
129+
setUseBasicAuthenticationWithAccessCodeGrant(basicAuth);
111130
return this;
112131
}
113132

@@ -119,8 +138,8 @@ public void setUsePkceWithAuthorizationCodeGrant(Boolean usePkceWithAuthorizatio
119138
this.usePkceWithAuthorizationCodeGrant = usePkceWithAuthorizationCodeGrant;
120139
}
121140

122-
public SwaggerUiOAuth2Config usePkceWithAuthorizationCodeGrant(Boolean usePkceWithAuthorizationCodeGrant) {
123-
setUsePkceWithAuthorizationCodeGrant(usePkceWithAuthorizationCodeGrant);
141+
public SwaggerUiOAuth2Config usePkceWithAuthorizationCodeGrant(Boolean pkce) {
142+
setUsePkceWithAuthorizationCodeGrant(pkce);
124143
return this;
125144
}
126145

@@ -143,18 +162,18 @@ public String toJsonString() {
143162
if (json.toString().endsWith(",")) {
144163
json.delete(json.length() - 1, json.length());
145164
}
146-
return json.append("}").toString();
165+
return json.append('}').toString();
147166
}
148167

149168
private void addStringField(StringBuilder json, String name, String value) {
150169
if (value != null) {
151-
json.append(quote(name)).append(':').append(quote(value)).append(",");
170+
json.append(quote(name)).append(':').append(quote(value)).append(',');
152171
}
153172
}
154173

155174
private void addBooleanField(StringBuilder json, String name, Boolean value) {
156175
if (value != null) {
157-
json.append(quote(name)).append(':').append(quote(value.toString())).append(",");
176+
json.append(quote(name)).append(':').append(value).append(',');
158177
}
159178
}
160179

@@ -163,7 +182,7 @@ private void addListField(StringBuilder json, String name, List<String> value) {
163182
json.append(quote(name)).append(':').append(
164183
value.stream().map(this::quote)
165184
.collect(Collectors.joining(",", "[", "]"))
166-
).append(",");
185+
).append(',');
167186
}
168187
}
169188

@@ -172,7 +191,7 @@ private void addMapField(StringBuilder json, String name, Map<String, String> va
172191
json.append(quote(name)).append(':').append(
173192
value.entrySet().stream().map(this::entryToString)
174193
.collect(Collectors.joining(",", "{", "}"))
175-
).append(",");
194+
).append(',');
176195
}
177196
}
178197

rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public Response getResource(@Context UriInfo uriInfo, @PathParam("resource") Str
129129
// Since Swagger UI 4.1.3, passing the default URL as query parameter,
130130
// e.g. `?url=swagger.json` is disabled by default due to security concerns.
131131
final String url = config.getUrl();
132-
final SwaggerUiOAuth2Config oAuthConfig = config.getOAuthConfig();
132+
final SwaggerUiOAuth2Config oAuthConfig = config.getOAuth2Config();
133133
if (!StringUtils.isEmpty(url) || oAuthConfig != null) {
134134
try (InputStream in = resourceURL.openStream()) {
135135
String resource = IOUtils.readStringFromStream(in);

systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/SwaggerUiConfigurationTest.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
package org.apache.cxf.systest.jaxrs.description.openapi;
2020

2121
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.Map;
2224

25+
import com.fasterxml.jackson.annotation.JsonInclude;
26+
import com.fasterxml.jackson.databind.json.JsonMapper;
2327
import com.fasterxml.jackson.jakarta.rs.json.JacksonJsonProvider;
2428

2529
import jakarta.ws.rs.core.MediaType;
@@ -31,12 +35,14 @@
3135
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
3236
import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
3337
import org.apache.cxf.jaxrs.swagger.ui.SwaggerUiConfig;
38+
import org.apache.cxf.jaxrs.swagger.ui.SwaggerUiOAuth2Config;
3439
import org.apache.cxf.testutil.common.AbstractClientServerTestBase;
3540
import org.apache.cxf.testutil.common.AbstractServerTestServerBase;
3641

3742
import org.junit.BeforeClass;
3843
import org.junit.Test;
3944

45+
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
4046
import static org.hamcrest.CoreMatchers.containsString;
4147
import static org.hamcrest.CoreMatchers.equalTo;
4248
import static org.hamcrest.MatcherAssert.assertThat;
@@ -45,7 +51,12 @@
4551

4652
public class SwaggerUiConfigurationTest extends AbstractClientServerTestBase {
4753
private static final String PORT = allocatePort(SwaggerUiConfigurationTest.class);
48-
54+
private static final SwaggerUiOAuth2Config OAUTH2_CONFIG = new SwaggerUiOAuth2Config()
55+
.usePkceWithAuthorizationCodeGrant(true)
56+
.appName("CXF Test App")
57+
.additionalQueryStringParams(Map.of("key1", "value1", "key2", "value2"))
58+
.scopes(List.of("scope1", "scope2"));
59+
4960
public static class Server extends AbstractServerTestServerBase {
5061

5162
@Override
@@ -57,7 +68,10 @@ protected org.apache.cxf.endpoint.Server createServer(Bus bus) throws Exception
5768
sf.setProvider(new JacksonJsonProvider());
5869
final OpenApiFeature feature = new OpenApiFeature();
5970
feature.setRunAsFilter(false);
60-
feature.setSwaggerUiConfig(new SwaggerUiConfig().url("/openapi.json").queryConfigEnabled(false));
71+
feature.setSwaggerUiConfig(new SwaggerUiConfig()
72+
.url("/openapi.json")
73+
.queryConfigEnabled(false)
74+
.oAuth2Config(OAUTH2_CONFIG));
6175
sf.setFeatures(Arrays.asList(feature));
6276
sf.setAddress("http://localhost:" + PORT + "/");
6377
return sf.create();
@@ -109,7 +123,7 @@ public void testUiRootResource() {
109123

110124
@Test
111125
public void testUiRootResourcePicksUrlFromConfigurationOnly() {
112-
// Test that Swagger UI URL is picked from configuration only and
126+
// Test that Swagger UI URL is picked from configuration only and
113127
// never from the query string (when query config is disabled).
114128
WebClient uiClient = WebClient
115129
.create("http://localhost:" + getPort() + "/api-docs")
@@ -130,7 +144,7 @@ public void testUiRootResourceReplacesUrlAsConfigured() {
130144
WebClient uiClient = WebClient
131145
.create("http://localhost:" + getPort() + "/api-docs")
132146
.path("/swagger-initializer.js")
133-
.query("another-openapi.json")
147+
.query("url", "another-openapi.json")
134148
.accept("*/*");
135149

136150
try (Response response = uiClient.get()) {
@@ -139,7 +153,26 @@ public void testUiRootResourceReplacesUrlAsConfigured() {
139153
assertFalse(jsCode.contains("another-openapi.json"));
140154
}
141155
}
142-
156+
157+
@Test
158+
public void testUiRootResourceAddsOAuth2ConfigAsConfigured() throws Exception {
159+
// With query config disabled or unset, we replace the url value in the Swagger resource with the one
160+
// configured in SwaggerUiConfig, and ignore the one in query parameter.
161+
WebClient uiClient = WebClient
162+
.create("http://localhost:" + getPort() + "/api-docs")
163+
.path("/swagger-initializer.js")
164+
.accept("*/*");
165+
166+
try (Response response = uiClient.get()) {
167+
String jsCode = response.readEntity(String.class);
168+
final JsonMapper mapper = JsonMapper.builder()
169+
.defaultPropertyInclusion(JsonInclude.Value.construct(NON_EMPTY, NON_EMPTY))
170+
.build();
171+
final String expectedConfigAsJson = mapper.writeValueAsString(OAUTH2_CONFIG);
172+
assertThat(jsCode, containsString("ui.initOAuth(" + expectedConfigAsJson + ")"));
173+
}
174+
}
175+
143176
public static String getPort() {
144177
return PORT;
145178
}

0 commit comments

Comments
 (0)