Skip to content

Commit 0c59a72

Browse files
authored
Path extension takes precedence over Accept Header (#454)
We have many users some access the API through their web browsers. They often wish to download a CSV file and use a link with a path extension (.csv for example) in their web browser. A web browser always sends its own Accept header, which includes at least `application/xml`. Since this takes precedence, the user is returned XML, instead of the CSV they wanted. By changing the order of the ContentNegotiation strategies we make sure the path extension takes precedence.
1 parent 2b2905c commit 0c59a72

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

klass-api/src/main/java/no/ssb/klass/api/config/WebConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
3636
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
3737
configurer.strategies(
3838
List.of(
39-
new HeaderContentNegotiationStrategy(),
4039
new PathExtensionContentNegotiationStrategy(
4140
// Only these path extensions are allowed.
4241
Map.of(
@@ -45,6 +44,7 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
4544
"json",
4645
MediaType.APPLICATION_JSON,
4746
"xml",
48-
MediaType.APPLICATION_XML))));
47+
MediaType.APPLICATION_XML)),
48+
new HeaderContentNegotiationStrategy()));
4949
}
5050
}

klass-api/src/test/java/no/ssb/klass/api/applicationtest/RestApiTechnicalDetailsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ private static Stream<Arguments> validPathExtensionContentTypes() {
7171

7272
@ParameterizedTest
7373
@MethodSource("validPathExtensionContentTypes")
74-
void pathExtensionContentNegotiation(String pathExtension, String expectedContentType) {
74+
void pathExtensionContentNegotiationTakesPrecedenceOverAcceptHeader(
75+
String pathExtension, String expectedContentType) {
7576
given().port(port)
76-
.noContentType() // Make sure the path extension is the sole specifier
77+
.contentType(ContentType.XML)
7778
.param("from", "2014-01-01")
7879
.param("to", "2015-01-01")
7980
.when()

0 commit comments

Comments
 (0)