33import de .sonallux .spotify .core .model .SpotifyWebApi ;
44import de .sonallux .spotify .core .model .SpotifyWebApiEndpoint ;
55
6+ import java .util .ArrayList ;
67import java .util .List ;
78import java .util .Map ;
9+ import java .util .Optional ;
810import java .util .stream .Collectors ;
911
1012import static de .sonallux .spotify .core .model .SpotifyWebApiEndpoint .ParameterLocation .*;
@@ -17,8 +19,8 @@ public static void splitEndpoints(SpotifyWebApi spotifyWebApi) throws IllegalArg
1719
1820 /**
1921 * 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+ * Some endpoints allow passing data either via query argument or via body. As the url has a length limit,
23+ * passing too much data in the query string might result in an error response. Therefore, this method removes
2224 * the option to pass the data via query argument and makes the body parameter mandatory.
2325 * @param spotifyWebApi the spotify web api documentation
2426 */
@@ -30,33 +32,43 @@ public static void fixDuplicateEndpointParameters(SpotifyWebApi spotifyWebApi) {
3032
3133 /**
3234 * 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+ * Some endpoints allow passing data either via query argument or via body. As the url has a length limit,
36+ * passing too much data in the query string might result in an error response. Therefore, this method removes
3537 * the option to pass the data via query argument and makes the body parameter mandatory.
3638 * @param endpoint the spotify api endpoint to fix
3739 */
3840 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 ;
41+ if (endpoint .getRequestBody () == null || !(endpoint .getRequestBody () instanceof SpotifyWebApiEndpoint .JsonRequestBody )) {
42+ return ;
43+ }
44+ var requestBody = ((SpotifyWebApiEndpoint .JsonRequestBody ) endpoint .getRequestBody ());
45+
46+ var iterator = endpoint .getParameters ().iterator ();
47+ while (iterator .hasNext ()) {
48+ var parameter = iterator .next ();
49+ var bodyParameter = getBodyParameter (requestBody , parameter .getName ());
50+ if (bodyParameter .isEmpty ()) {
51+ continue ;
4952 }
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- }
53+
54+ //Remove the query parameter
55+ iterator .remove ();
56+
57+ // Parameter position in endpoint-add-tracks-to-playlist is optional
58+ if ("endpoint-add-tracks-to-playlist" .equals (endpoint .getId ()) && "position" .equals (parameter .getName ())) {
59+ continue ;
5960 }
60- });
61+
62+ // Mark body parameter and Content-Type header as required
63+ bodyParameter .get ().setRequired (true );
64+ endpoint .getParameters ().stream ()
65+ .filter (p -> p .getLocation () == HEADER && "Content-Type" .equals (p .getName ()))
66+ .findFirst ().ifPresent (p -> p .setRequired (true ));
67+
68+ }
69+ }
70+
71+ private static Optional <SpotifyWebApiEndpoint .Parameter > getBodyParameter (SpotifyWebApiEndpoint .JsonRequestBody requestBody , String name ) {
72+ return requestBody .getParameters ().stream ().filter (p -> p .getName ().equals (name )).findFirst ();
6173 }
6274}
0 commit comments