Skip to content

Commit a74b326

Browse files
committed
Add helper to fix duplicate endpoint parameter
1 parent f1f0362 commit a74b326

File tree

6 files changed

+72
-10
lines changed

6 files changed

+72
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.sonallux.spotify</groupId>
88
<artifactId>spotify-web-api-parent</artifactId>
9-
<version>2021.4.5</version>
9+
<version>2021.4.10</version>
1010
<packaging>pom</packaging>
1111

1212
<name>spotify-web-api-parent</name>

spotify-web-api-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<parent>
88
<groupId>de.sonallux.spotify</groupId>
99
<artifactId>spotify-web-api-parent</artifactId>
10-
<version>2021.4.5</version>
10+
<version>2021.4.10</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>spotify-web-api-core</artifactId>
15-
<version>2021.4.5</version>
15+
<version>2021.4.10</version>
1616
<packaging>jar</packaging>
1717

1818
<name>spotify-web-api-core</name>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package de.sonallux.spotify.core;
2+
3+
import de.sonallux.spotify.core.model.SpotifyWebApi;
4+
import de.sonallux.spotify.core.model.SpotifyWebApiEndpoint;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.stream.Collectors;
9+
10+
import static de.sonallux.spotify.core.model.SpotifyWebApiEndpoint.ParameterLocation.*;
11+
12+
public class EndpointHelper {
13+
14+
public static void splitEndpoints(SpotifyWebApi spotifyWebApi) throws IllegalArgumentException {
15+
EndpointSplitter.splitEndpoints(spotifyWebApi);
16+
}
17+
18+
/**
19+
* Fixes duplicated endpoint parameters.
20+
* Some endpoints allow to pass data either via query argument or via body. As the url has a length limit,
21+
* passing to much data in the query string might result in an error response. Therefore this method removes
22+
* the option to pass the data via query argument and makes the body parameter mandatory.
23+
* @param spotifyWebApi the spotify web api documentation
24+
*/
25+
public static void fixDuplicateEndpointParameters(SpotifyWebApi spotifyWebApi) {
26+
spotifyWebApi.getCategoryList().stream()
27+
.flatMap(c -> c.getEndpointList().stream())
28+
.forEach(EndpointHelper::fixDuplicateEndpointParameters);
29+
}
30+
31+
/**
32+
* Fixes duplicated endpoint parameters.
33+
* Some endpoints allow to pass data either via query argument or via body. As the url has a length limit,
34+
* passing to much data in the query string might result in an error response. Therefore this method removes
35+
* the option to pass the data via query argument and makes the body parameter mandatory.
36+
* @param endpoint the spotify api endpoint to fix
37+
*/
38+
public static void fixDuplicateEndpointParameters(SpotifyWebApiEndpoint endpoint) {
39+
var duplicates = endpoint.getParameters().stream()
40+
.collect(Collectors.groupingBy(SpotifyWebApiEndpoint.Parameter::getName))
41+
.entrySet().stream()
42+
.filter(e -> e.getValue().size() > 1)
43+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
44+
45+
duplicates.forEach((paramName, parameters) -> {
46+
if (!parameters.stream().map(SpotifyWebApiEndpoint.Parameter::getLocation).sorted().collect(Collectors.toList()).equals(List.of(QUERY, BODY))) {
47+
System.err.println("Endpoint " + endpoint.getName() + " has unfixable duplicate parameters");
48+
return;
49+
}
50+
endpoint.getParameters().removeIf(p -> p.getLocation() == QUERY && paramName.equals(p.getName()));
51+
for (var param : endpoint.getParameters()) {
52+
if (param.getLocation() == BODY && paramName.equals(param.getName())) {
53+
if (!("endpoint-add-tracks-to-playlist".equals(endpoint.getId()) && "position".equals(param.getName()))) {
54+
param.setRequired(true);
55+
}
56+
} else if (param.getLocation() == HEADER && "Content-Type".equals(param.getName())) {
57+
param.setRequired(true);
58+
}
59+
}
60+
});
61+
}
62+
}

spotify-web-api-generator-open-api/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<parent>
88
<groupId>de.sonallux.spotify</groupId>
99
<artifactId>spotify-web-api-parent</artifactId>
10-
<version>2021.4.5</version>
10+
<version>2021.4.10</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>spotify-web-api-generator-open-api</artifactId>
15-
<version>2021.4.5</version>
15+
<version>2021.4.10</version>
1616
<packaging>jar</packaging>
1717

1818
<name>spotify-web-api-generator-open-api</name>
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>de.sonallux.spotify</groupId>
3131
<artifactId>spotify-web-api-core</artifactId>
32-
<version>2021.4.5</version>
32+
<version>2021.4.10</version>
3333
</dependency>
3434
<dependency>
3535
<groupId>io.swagger.core.v3</groupId>

spotify-web-api-generator-open-api/spotify-web-api-openapi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ info:
44
contact:
55
name: sonallux
66
url: https://github.com/sonallux/spotify-web-api
7-
version: 2021.4.5
7+
version: 2021.4.10
88
externalDocs:
99
description: Find more info on the official Spotify Web API Reference
1010
url: https://developer.spotify.com/documentation/web-api/reference

spotify-web-api-parser/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<parent>
88
<groupId>de.sonallux.spotify</groupId>
99
<artifactId>spotify-web-api-parent</artifactId>
10-
<version>2021.4.5</version>
10+
<version>2021.4.10</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

1414
<artifactId>spotify-web-api-parser</artifactId>
15-
<version>2021.4.5</version>
15+
<version>2021.4.10</version>
1616
<packaging>jar</packaging>
1717

1818
<name>spotify-web-api-parser</name>
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>de.sonallux.spotify</groupId>
2929
<artifactId>spotify-web-api-core</artifactId>
30-
<version>2021.4.5</version>
30+
<version>2021.4.10</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.jsoup</groupId>

0 commit comments

Comments
 (0)