Skip to content

Commit ec31c3b

Browse files
authored
Klass API continues to support path extensions for content negotiation (#408)
* Add failing test * Configure path extension content negotiation - This is still an actively used API feature so it must be supported - This is deprecated in Spring so it's likely to be removed in the future - Limit the accepted file extensions for security * Also support header content negotiation * Correctly document content types for operations * Correct content types for error responses * API documentation covers all error formats * Remove dot from file extensions
1 parent 30979b2 commit ec31c3b

19 files changed

+1139
-550
lines changed

klass-api/src/main/asciidoc/api-guide.adoc

Lines changed: 250 additions & 60 deletions
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package no.ssb.klass.api.config;
2+
3+
import no.ssb.klass.api.util.RestConstants;
4+
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
8+
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
9+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
10+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* Prior to Spring Framework v3, path extension mapping was enabled by default.
18+
*
19+
* <p>This has been a central way for the Klass API to perform content negotiation and is currently
20+
* in use by klass-web and presumably other clients.
21+
*
22+
* <p>This functionality is now deprecated in Spring and will be removed in a future release. We
23+
* need to encourage clients to transition to header-based content negotiation if we want to keep
24+
* pace with future Spring releases.
25+
*/
26+
@Configuration
27+
public class WebConfiguration implements WebMvcConfigurer {
28+
29+
@Override
30+
public void configurePathMatch(PathMatchConfigurer configurer) {
31+
configurer.setUseSuffixPatternMatch(true);
32+
configurer.setUseRegisteredSuffixPatternMatch(true);
33+
}
34+
35+
@Override
36+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
37+
configurer.strategies(
38+
List.of(
39+
new HeaderContentNegotiationStrategy(),
40+
new PathExtensionContentNegotiationStrategy(
41+
// Only these path extensions are allowed.
42+
Map.of(
43+
"csv",
44+
MediaType.parseMediaType(RestConstants.CONTENT_TYPE_CSV),
45+
"json",
46+
MediaType.APPLICATION_JSON,
47+
"xml",
48+
MediaType.APPLICATION_XML))));
49+
}
50+
}

0 commit comments

Comments
 (0)