Skip to content

Commit 8b79e0b

Browse files
committed
Set additional_types parameter by default
1 parent 9323828 commit 8b79e0b

File tree

7 files changed

+29
-13
lines changed

7 files changed

+29
-13
lines changed

spotify-web-api-generator-java/src/main/java/de/sonallux/spotify/generator/java/templates/ApiTemplate.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22-
import java.util.stream.Collectors;
2322
import java.util.stream.Stream;
2423

2524
import static java.nio.file.StandardOpenOption.*;
@@ -160,6 +159,13 @@ private List<Parameter> generateEndpointRequest(SpotifyWebApiEndpoint endpoint,
160159

161160
context.put("requiredJavaDocParameters", requiredParameterList.stream().map(Parameter::asJavaDoc).collect(toList()));
162161

162+
optionalQueryParameters.stream()
163+
.filter(p -> "additional_types".equals(p.getName()))
164+
.findFirst().ifPresent(additionalTypesParameter -> {
165+
additionalTypesParameter.setDefaultValue("\"track,episode\"");
166+
context.put("parametersWithDefaultValue", List.of(additionalTypesParameter));
167+
});
168+
163169
var packageFolder = getPackageFolder(outputFolder, javaPackage);
164170
var outputFile = packageFolder.resolve(JavaUtils.getFileName(JavaUtils.getEndpointRequestBuilderName(endpoint)));
165171
generateFile(outputFile, requestTemplate, context);
@@ -193,16 +199,18 @@ private String getResponseType(SpotifyWebApiEndpoint endpoint) {
193199
@Getter
194200
@Setter
195201
private static class Parameter {
202+
private String name;
196203
private String javaName;
197-
private String jsonName;
198204
private String type;
199205
private String description;
206+
private String defaultValue;
200207

201-
public Parameter(String jsonName, String type, String description) {
202-
this.javaName = JavaUtils.escapeFieldName(jsonName);
203-
this.jsonName = jsonName;
208+
public Parameter(String name, String type, String description) {
209+
this.name = name;
210+
this.javaName = JavaUtils.escapeFieldName(name);
204211
this.type = JavaUtils.mapToPrimitiveJavaType(type);
205212
this.description = Markdown2Html.convertToSingleLine(description);
213+
this.defaultValue = null;
206214
}
207215

208216
public String asMethodParameter() {

spotify-web-api-generator-java/src/main/resources/templates/request.mustache

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,26 @@ public class {{className}} {
4141
this.apiClient = apiClient;
4242
this.request = new Request("{{httpMethod}}", "{{path}}")
4343
{{#requiredPathParameters}}
44-
.addPathParameter("{{jsonName}}", String.valueOf({{javaName}}))
44+
.addPathParameter("{{name}}", String.valueOf({{javaName}}))
4545
{{/requiredPathParameters}}
4646
{{#requiredQueryParameters}}
47-
.addQueryParameter("{{jsonName}}", String.valueOf({{javaName}}))
47+
.addQueryParameter("{{name}}", String.valueOf({{javaName}}))
4848
{{/requiredQueryParameters}}
4949
{{#requiredBodyParameters}}
50-
.addBodyParameter("{{jsonName}}", {{javaName}})
50+
.addBodyParameter("{{name}}", {{javaName}})
5151
{{/requiredBodyParameters}}
5252
;
53+
{{#parametersWithDefaultValue}}
54+
this.{{javaName}}({{defaultValue}});
55+
{{/parametersWithDefaultValue}}
5356
}
5457

5558
{{#optionalPathParameters}}
5659
/**
5760
* {{description}}
5861
*/
5962
public {{className}} {{javaName}}({{type}} {{javaName}}) {
60-
this.request.addPathParameter("{{jsonName}}", String.valueOf({{javaName}}));
63+
this.request.addPathParameter("{{name}}", String.valueOf({{javaName}}));
6164
return this;
6265
}
6366

@@ -67,7 +70,7 @@ public class {{className}} {
6770
* {{description}}
6871
*/
6972
public {{className}} {{javaName}}({{type}} {{javaName}}) {
70-
this.request.addQueryParameter("{{jsonName}}", String.valueOf({{javaName}}));
73+
this.request.addQueryParameter("{{name}}", String.valueOf({{javaName}}));
7174
return this;
7275
}
7376

@@ -77,7 +80,7 @@ public class {{className}} {
7780
* {{description}}
7881
*/
7982
public {{className}} {{javaName}}({{type}} {{javaName}}) {
80-
this.request.addBodyParameter("{{jsonName}}", {{javaName}});
83+
this.request.addBodyParameter("{{name}}", {{javaName}});
8184
return this;
8285
}
8386

spotify-web-api-java/src/main/generated/de/sonallux/spotify/api/apis/player/GetInformationAboutUsersCurrentPlaybackRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public GetInformationAboutUsersCurrentPlaybackRequest(ApiClient apiClient) {
2929
this.apiClient = apiClient;
3030
this.request = new Request("GET", "/me/player")
3131
;
32+
this.additionalTypes("track,episode");
3233
}
3334

3435
/**

spotify-web-api-java/src/main/generated/de/sonallux/spotify/api/apis/player/GetUsersCurrentlyPlayingTrackRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public GetUsersCurrentlyPlayingTrackRequest(ApiClient apiClient, String market)
3333
this.request = new Request("GET", "/me/player/currently-playing")
3434
.addQueryParameter("market", String.valueOf(market))
3535
;
36+
this.additionalTypes("track,episode");
3637
}
3738

3839
/**

spotify-web-api-java/src/main/generated/de/sonallux/spotify/api/apis/playlists/GetPlaylistRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public GetPlaylistRequest(ApiClient apiClient, String playlistId) {
2727
this.request = new Request("GET", "/playlists/{playlist_id}")
2828
.addPathParameter("playlist_id", String.valueOf(playlistId))
2929
;
30+
this.additionalTypes("track,episode");
3031
}
3132

3233
/**

spotify-web-api-java/src/main/generated/de/sonallux/spotify/api/apis/playlists/GetPlaylistsTracksRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public GetPlaylistsTracksRequest(ApiClient apiClient, String playlistId, String
2929
.addPathParameter("playlist_id", String.valueOf(playlistId))
3030
.addQueryParameter("market", String.valueOf(market))
3131
;
32+
this.additionalTypes("track,episode");
3233
}
3334

3435
/**

spotify-web-api-java/src/test/java/de/sonallux/spotify/api/ConversionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ void testUnionTypeHandlingWithAdditionalTypesParameter() throws Exception {
107107
assertNotNull(episode);
108108
assertEquals("episode", episode.getType());
109109
assertTrue(episode instanceof Episode);
110+
assertNotNull(((Episode) episode).getShow());
110111

111112
var request = webServer.takeRequest();
112-
// TODO: adjust when additional_types is set by default
113-
assertEquals("/playlists/foo/tracks?market=DE", request.getPath());
113+
assertEquals("/playlists/foo/tracks?market=DE&additional_types=track%2Cepisode", request.getPath());
114114
}
115115

116116
@Test
@@ -129,6 +129,7 @@ void testUnionTypeHandlingWithoutAdditionalTypesParameter() throws Exception {
129129
assertNotNull(episode);
130130
assertEquals("episode", episode.getType());
131131
assertTrue(episode instanceof Episode);
132+
assertNull(((Episode) episode).getShow());//show is not set, because episode is returned with track format
132133

133134
var request = webServer.takeRequest();
134135
assertEquals("/playlists/foo/tracks?market=DE&additional_types=track", request.getPath());

0 commit comments

Comments
 (0)