Skip to content

Commit 833310e

Browse files
committed
allow to filter authorization by URL
1 parent 97865f6 commit 833310e

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/AuthorizationValue.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package io.swagger.v3.parser.core.models;
22

3+
import java.net.URL;
4+
import java.util.List;
5+
36
public class AuthorizationValue {
47
private String value, type, keyName;
8+
private List<URL> urls;
59

610
public AuthorizationValue() {
711
}
812

9-
public AuthorizationValue(String keyName, String value, String type) {
13+
public AuthorizationValue(String keyName, String value, String type, List<URL> urls) {
1014
this.setKeyName(keyName);
1115
this.setValue(value);
1216
this.setType(type);
17+
this.setUrls(urls);
18+
}
19+
20+
public AuthorizationValue(String keyName, String value, String type) {
21+
this(keyName, value, type, null);
1322
}
1423

1524
public AuthorizationValue value(String value) {
@@ -27,6 +36,11 @@ public AuthorizationValue keyName(String keyName) {
2736
return this;
2837
}
2938

39+
public AuthorizationValue urls(List<URL> urls) {
40+
this.urls = urls;
41+
return this;
42+
}
43+
3044
public String getValue() {
3145
return value;
3246
}
@@ -51,13 +65,22 @@ public void setKeyName(String keyName) {
5165
this.keyName = keyName;
5266
}
5367

68+
public List<URL> getUrls() {
69+
return urls;
70+
}
71+
72+
public void setUrls(List<URL> urls) {
73+
this.urls = urls;
74+
}
75+
5476
@Override
5577
public int hashCode() {
5678
final int prime = 31;
5779
int result = 1;
5880
result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
5981
result = prime * result + ((type == null) ? 0 : type.hashCode());
6082
result = prime * result + ((value == null) ? 0 : value.hashCode());
83+
result = prime * result + ((urls == null) ? 0 : urls.hashCode());
6184
return result;
6285
}
6386

@@ -94,6 +117,13 @@ public boolean equals(Object obj) {
94117
} else if (!value.equals(other.value)) {
95118
return false;
96119
}
120+
if (urls == null) {
121+
if (other.urls != null) {
122+
return false;
123+
}
124+
} else if (!urls.equals(other.urls)) {
125+
return false;
126+
}
97127
return true;
98128
}
99129
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import io.swagger.v3.parser.util.RemoteUrl;
1414
import io.swagger.v3.parser.util.ResolverFully;
1515
import org.apache.commons.io.FileUtils;
16-
import javax.net.ssl.SSLHandshakeException;
1716
import org.slf4j.Logger;
1817
import org.slf4j.LoggerFactory;
1918

19+
import javax.net.ssl.SSLHandshakeException;
2020
import java.net.URI;
2121
import java.nio.charset.Charset;
2222
import java.nio.file.Files;
@@ -127,7 +127,7 @@ private ObjectMapper getRightMapper(String data) {
127127
}
128128
return mapper;
129129
}
130-
130+
131131
public SwaggerParseResult readWithInfo(String location, List<AuthorizationValue> auths) {
132132
String data;
133133

@@ -175,7 +175,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
175175
SwaggerParseResult result = new SwaggerParseResult();
176176
if(swaggerAsString != null && !"".equals(swaggerAsString.trim())) {
177177
ObjectMapper mapper = getRightMapper(swaggerAsString);
178-
178+
179179
if(auth == null) {
180180
auth = new ArrayList<>();
181181
}
@@ -217,7 +217,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
217217
* Locates extensions on the current thread class loader and then, if it differs
218218
* from this class classloader (as in OSGi), locates extensions from this
219219
* class classloader as well.
220-
*
220+
*
221221
* @return a list of extensions
222222
*/
223223
public static List<SwaggerParserExtension> getExtensions() {
@@ -262,6 +262,7 @@ protected List<AuthorizationValue> transform(List<AuthorizationValue> input) {
262262
v.setKeyName(value.getKeyName());
263263
v.setValue(value.getValue());
264264
v.setType(value.getType());
265+
v.setUrls(value.getUrls());
265266

266267
output.add(v);
267268
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RemoteUrl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ public static String urlToString(String url, List<AuthorizationValue> auths) thr
115115
final List<AuthorizationValue> header = new ArrayList<>();
116116
if (auths != null && auths.size() > 0) {
117117
for (AuthorizationValue auth : auths) {
118-
if ("query".equals(auth.getType())) {
119-
appendValue(inUrl, auth, query);
120-
} else if ("header".equals(auth.getType())) {
121-
appendValue(inUrl, auth, header);
118+
if (auth.getUrls() == null || auth.getUrls().contains(inUrl)) {
119+
if ("query".equals(auth.getType())) {
120+
appendValue(inUrl, auth, query);
121+
} else if ("header".equals(auth.getType())) {
122+
appendValue(inUrl, auth, header);
123+
}
122124
}
123125
}
124126
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/RemoteUrlTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import com.github.tomakehurst.wiremock.WireMockServer;
44
import com.github.tomakehurst.wiremock.client.WireMock;
5+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
56
import io.swagger.v3.parser.core.models.AuthorizationValue;
67
import org.testng.annotations.AfterMethod;
78
import org.testng.annotations.BeforeMethod;
89
import org.testng.annotations.Test;
910

11+
import java.net.URL;
1012
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.List;
1115

1216
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
1317
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
1418
import static com.github.tomakehurst.wiremock.client.WireMock.get;
1519
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
1620
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
1721
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
18-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
1922
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
2023
import static org.testng.Assert.assertEquals;
24+
import static org.testng.Assert.assertFalse;
2125

2226
public class RemoteUrlTest {
2327

@@ -76,6 +80,45 @@ public void testAuthorizationHeader() throws Exception {
7680
);
7781
}
7882

83+
@Test
84+
public void testAuthorizationHeaderWithMatchingUrl() throws Exception {
85+
86+
final String expectedBody = setupStub();
87+
88+
final String headerName = "Authorization";
89+
final String headerValue = "foobar";
90+
String url = getUrl();
91+
final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header",
92+
Collections.singletonList(new URL(url)));
93+
final String actualBody = RemoteUrl.urlToString(url, Arrays.asList(authorizationValue));
94+
95+
assertEquals(actualBody, expectedBody);
96+
97+
verify(getRequestedFor(urlEqualTo("/v2/pet/1"))
98+
.withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER))
99+
.withHeader(headerName, equalTo(headerValue))
100+
);
101+
}
102+
103+
@Test
104+
public void testAuthorizationHeaderWithNonMatchingUrl() throws Exception {
105+
106+
final String expectedBody = setupStub();
107+
108+
final String headerValue = "foobar";
109+
String url = getUrl();
110+
String authorization = "Authorization";
111+
final AuthorizationValue authorizationValue = new AuthorizationValue(authorization,
112+
headerValue, "header", Collections.singletonList(new URL("http://foo.com")));
113+
final String actualBody = RemoteUrl.urlToString(url, Arrays.asList(authorizationValue));
114+
115+
assertEquals(actualBody, expectedBody);
116+
117+
List<LoggedRequest> requests = WireMock.findAll(getRequestedFor(urlEqualTo("/v2/pet/1")));
118+
assertEquals(1, requests.size());
119+
assertFalse(requests.get(0).containsHeader(authorization));
120+
}
121+
79122
private String getUrl() {
80123
return String.format("http://%s:%d/v2/pet/1", LOCALHOST, WIRE_MOCK_PORT);
81124
}

0 commit comments

Comments
 (0)