Skip to content

Commit d92b92d

Browse files
Fiks ObjectMapperResolver caching, swagger X-Json-Serializer-Option select (#194)
* Fiks ObjectMapper caching problem. Problem med at resultat frå ObjectMapperResolver vart cacha i server hadde gjenoppstått etter innføring av openapi-spec-utils. Flytter registreringa av DynamicJacksonJsonProvider til før alt anna for å unngå dette problemet. * Legger til swagger ui plugin for å kunne velge X-Json-Serializer-Option som skal brukast. Ein kan då velge om ein vil bruke ObjectMapper kompatibel med vist openapi spesifikasjon, eller andre varianter. * Oppdaterer openapi-spec-utils til versjon 1.2 * ung-sak.openapi.json updated by build pipeline skip-checks:true --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 9b31852 commit d92b92d

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

web/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
<dependency>
209209
<groupId>no.nav.openapi.spec.utils</groupId>
210210
<artifactId>openapi-spec-utils</artifactId>
211-
<version>1.0.0-beta</version>
211+
<version>1.2.0</version>
212212
</dependency>
213213

214214
<!-- CDI -->

web/src/main/java/no/nav/ung/sak/web/app/ApplicationConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public OpenAPI resolveOpenAPI() {
4040
}
4141

4242
public ApplicationConfig() {
43+
register(DynamicJacksonJsonProvider.class); // Denne må registrerast før anna OpenAPI oppsett for å fungere.
4344
final var resolvedOpenAPI = resolveOpenAPI();
4445
register(new no.nav.openapi.spec.utils.openapi.OpenApiResource(resolvedOpenAPI));
4546

@@ -48,7 +49,6 @@ public ApplicationConfig() {
4849
registerClasses(new LinkedHashSet<>(new RestImplementationClasses().getImplementationClasses()));
4950

5051
register(ObjectMapperResolver.class);
51-
register(DynamicJacksonJsonProvider.class);
5252

5353
registerInstances(new LinkedHashSet<>(new KnownExceptionMappers().getExceptionMappers()));
5454
register(CacheControlFeature.class);

web/src/main/resources/openapi-ts-client/ung-sak.openapi.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6657,7 +6657,7 @@
66576657
},
66586658
"/api/formidling/vedtaksbrev/forhaandsvis" : {
66596659
"post" : {
6660-
"description" : "Forhåndsvise vedtaksbrev for en behandling",
6660+
"description" : "Forhåndsvise vedtaksbrev for en behandling. Bruk application/octet-stream fra swagger for å laste ned pdf ",
66616661
"operationId" : "forhåndsvisVedtaksbrev",
66626662
"requestBody" : {
66636663
"content" : {

web/src/main/resources/web/swagger/local-swagger.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
const defaultValue = "default";
2+
const optionValues = [defaultValue, "openapi-compat", "kodeverdi-string", "kodeverdi-kalkulus-string"]
3+
4+
// JsonSerializerOptionPlugin adds a select input for choosing a value for the X-Json-Serializer-Option header value to be used for subsequent requests
5+
// from the swagger ui, and state to keep the selected value.
6+
const JsonSerializerOptionPlugin = () => {
7+
return {
8+
statePlugins: {
9+
jsonSerializerOption: {
10+
actions: {
11+
updateJsonSerializerOption: (option) => {
12+
return {
13+
type: "UPDATE_JSON_SERIALIZER_OPTION",
14+
payload: option,
15+
}
16+
}
17+
},
18+
reducers: {
19+
"UPDATE_JSON_SERIALIZER_OPTION": (state, action) => {
20+
return state.set("xJsonSerializerOption", action.payload)
21+
}
22+
},
23+
selectors: {
24+
xJsonSerializerOption: (state) => state.get("xJsonSerializerOption")
25+
},
26+
}
27+
},
28+
wrapComponents: {
29+
ServersContainer: (Original, system) => props => {
30+
const React = system.React;
31+
const options = optionValues.map(opt => React.createElement("option", {value: opt}, `${opt}`))
32+
const selectId = "JsonSerializerOptionSelect"
33+
const onChange = (ev) => {
34+
system.jsonSerializerOptionActions.updateJsonSerializerOption(ev.target.value);
35+
}
36+
const select = React.createElement("select", {id: selectId, onChange}, ...options)
37+
const label = React.createElement("label", {for: selectId, style: {display: "block", marginBottom: "0", paddingTop: "4px"}}, "X-Json-Serializer-Option")
38+
const original = React.createElement(Original, props)
39+
const div = React.createElement("div", null, label, select)
40+
return [original, div]
41+
}
42+
}
43+
}
44+
}
45+
46+
147
window.onload = function() {
248
// Begin Swagger UI call region
349
const ui = SwaggerUIBundle({
@@ -9,9 +55,22 @@ window.onload = function() {
955
SwaggerUIStandalonePreset
1056
],
1157
plugins: [
58+
JsonSerializerOptionPlugin,
1259
SwaggerUIBundle.plugins.DownloadUrl
1360
],
14-
layout: "StandaloneLayout"
61+
layout: "StandaloneLayout",
62+
// requestInterceptor adds chosen X-Json-Serializer-Option header value to all requests, if it has been changed from default (undefined).
63+
requestInterceptor: (req) => {
64+
// On first request after load (to get openapi.yaml), this.ui is null.
65+
// serializerOptionVal would always be undefined (default value) anyway at that point, so doesn't matter.
66+
if(this.ui != null) {
67+
const serializerOptionVal = this.ui.jsonSerializerOptionSelectors.xJsonSerializerOption()
68+
if(serializerOptionVal != null && serializerOptionVal !== defaultValue) {
69+
req.headers["X-Json-Serializer-Option"] = serializerOptionVal
70+
}
71+
}
72+
return req
73+
}
1574
});
1675
// End Swagger UI call region
1776

0 commit comments

Comments
 (0)