Skip to content

Commit e34deb7

Browse files
committed
Changes report: OpenAPIService is using ObjectMapper without configured modules since SpringDoc 1.6.7 springdoc#1655
1 parent 4d102cb commit e34deb7

File tree

5 files changed

+217
-2
lines changed

5 files changed

+217
-2
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/providers/ObjectMapperProvider.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.fasterxml.jackson.databind.ObjectMapper;
2828
import io.swagger.v3.core.util.Json;
2929
import io.swagger.v3.core.util.Json31;
30+
import io.swagger.v3.core.util.ObjectMapperFactory;
3031
import io.swagger.v3.core.util.Yaml;
3132
import io.swagger.v3.core.util.Yaml31;
3233
import org.springdoc.core.properties.SpringDocConfigProperties;
@@ -35,7 +36,7 @@
3536
/**
3637
* The type Spring doc object mapper provider.
3738
*/
38-
public class ObjectMapperProvider {
39+
public class ObjectMapperProvider extends ObjectMapperFactory {
3940

4041
/**
4142
* The Json mapper.
@@ -81,4 +82,19 @@ public ObjectMapper jsonMapper() {
8182
public ObjectMapper yamlMapper() {
8283
return yamlMapper;
8384
}
85+
86+
/**
87+
* Create json object mapper.
88+
*
89+
* @param springDocConfigProperties the spring doc config properties
90+
* @return the object mapper
91+
*/
92+
public static ObjectMapper createJson(SpringDocConfigProperties springDocConfigProperties) {
93+
OpenApiVersion openApiVersion = springDocConfigProperties.getApiDocs().getVersion();
94+
if (openApiVersion == OpenApiVersion.OPENAPI_3_1)
95+
return ObjectMapperProvider.createJson31();
96+
else
97+
return ObjectMapperProvider.createJson();
98+
}
99+
84100
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/OpenAPIService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
7070
import org.springdoc.core.properties.SpringDocConfigProperties;
7171
import org.springdoc.core.providers.JavadocProvider;
72+
import org.springdoc.core.providers.ObjectMapperProvider;
7273
import org.springdoc.core.utils.PropertyResolverUtils;
7374

7475
import org.springframework.beans.BeansException;
@@ -258,7 +259,7 @@ public OpenAPI build(Locale locale) {
258259
}
259260
else {
260261
try {
261-
ObjectMapper objectMapper = new ObjectMapper();
262+
ObjectMapper objectMapper = ObjectMapperProvider.createJson(springDocConfigProperties);
262263
calculatedOpenAPI = objectMapper.readValue(objectMapper.writeValueAsString(openAPI), OpenAPI.class);
263264
}
264265
catch (JsonProcessingException e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app188;
26+
27+
28+
import java.time.Instant;
29+
import java.util.Map;
30+
31+
import io.swagger.v3.oas.annotations.media.Schema;
32+
import io.swagger.v3.oas.models.Components;
33+
import io.swagger.v3.oas.models.OpenAPI;
34+
import io.swagger.v3.oas.models.Operation;
35+
import io.swagger.v3.oas.models.media.Content;
36+
import io.swagger.v3.oas.models.responses.ApiResponse;
37+
import org.springdoc.core.customizers.OperationCustomizer;
38+
import org.springdoc.core.utils.SpringDocAnnotationsUtils;
39+
40+
import org.springframework.context.annotation.Bean;
41+
import org.springframework.http.MediaType;
42+
import org.springframework.web.bind.annotation.GetMapping;
43+
import org.springframework.web.bind.annotation.RestController;
44+
import org.springframework.web.method.HandlerMethod;
45+
46+
@RestController
47+
public class HelloController {
48+
49+
@GetMapping("/test")
50+
public void test(){}
51+
52+
@Bean
53+
public OpenAPI openAPI(){return new OpenAPI().components(new Components());}
54+
55+
@Bean
56+
public OperationCustomizer operationCustomizer(OpenAPI api){
57+
io.swagger.v3.oas.models.media.Schema errorResponseSchema= SpringDocAnnotationsUtils.extractSchema(
58+
api.getComponents(),
59+
ErrorResponse.class,
60+
null,
61+
null
62+
);
63+
64+
ApiResponse errorApiResponse=new ApiResponse().content(new Content().addMediaType(
65+
MediaType.APPLICATION_JSON_VALUE,
66+
new io.swagger.v3.oas.models.media.MediaType().schema(errorResponseSchema)
67+
));
68+
69+
return(Operation operation, HandlerMethod handlerMethod)->{
70+
operation.getResponses().addApiResponse("5xx",errorApiResponse);
71+
return operation;
72+
};
73+
}
74+
75+
public class ErrorResponse {
76+
@Schema(example = "2022-05-09T00:00:00.000Z")
77+
Instant timestamp;
78+
@Schema(example = "{\"param1\":\"val1\",\"param2\":\"val2\"}")
79+
Map<String, Object> data;
80+
81+
public Instant getTimestamp() {
82+
return timestamp;
83+
}
84+
85+
public void setTimestamp(Instant timestamp) {
86+
this.timestamp = timestamp;
87+
}
88+
89+
public Map<String, Object> getData() {
90+
return data;
91+
}
92+
93+
public void setData(Map<String, Object> data) {
94+
this.data = data;
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
25+
package test.org.springdoc.api.v30.app188;
26+
27+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
31+
public class SpringDocApp188Test extends AbstractSpringDocV30Test {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
36+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "test",
20+
"responses": {
21+
"200": {
22+
"description": "OK"
23+
},
24+
"5xx": {
25+
"content": {
26+
"application/json": {
27+
"schema": {
28+
"$ref": "#/components/schemas/ErrorResponse"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
},
37+
"components": {
38+
"schemas": {
39+
"ErrorResponse": {
40+
"type": "object",
41+
"properties": {
42+
"timestamp": {
43+
"type": "string",
44+
"format": "date-time",
45+
"example": "2022-05-09T00:00:00Z"
46+
},
47+
"data": {
48+
"type": "object",
49+
"additionalProperties": {
50+
"type": "object",
51+
"example": {
52+
"param1": "val1",
53+
"param2": "val2"
54+
}
55+
},
56+
"example": {
57+
"param1": "val1",
58+
"param2": "val2"
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)