Skip to content

Commit 6cb2761

Browse files
committed
OpenAPIService is using ObjectMapper without configured modules since SpringDoc 1.6.7. Fixes #1655
1 parent 1224f54 commit 6cb2761

File tree

5 files changed

+213
-2
lines changed

5 files changed

+213
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
6161
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
6262
import org.springdoc.core.providers.JavadocProvider;
63+
import org.springdoc.core.providers.ObjectMapperProvider;
6364

6465
import org.springframework.beans.BeansException;
6566
import org.springframework.beans.factory.config.BeanDefinition;
@@ -246,7 +247,7 @@ public OpenAPI build(Locale locale) {
246247
}
247248
else {
248249
try {
249-
ObjectMapper objectMapper = new ObjectMapper();
250+
ObjectMapper objectMapper = ObjectMapperProvider.createJson(springDocConfigProperties);
250251
calculatedOpenAPI = objectMapper.readValue(objectMapper.writeValueAsString(openAPI), OpenAPI.class);
251252
}
252253
catch (JsonProcessingException e) {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import io.swagger.v3.core.util.Json;
2727
import io.swagger.v3.core.util.Json31;
28+
import io.swagger.v3.core.util.ObjectMapperFactory;
2829
import io.swagger.v3.core.util.Yaml;
2930
import io.swagger.v3.core.util.Yaml31;
3031
import org.springdoc.core.SpringDocConfigProperties;
@@ -33,7 +34,7 @@
3334
/**
3435
* The type Spring doc object mapper provider.
3536
*/
36-
public class ObjectMapperProvider {
37+
public class ObjectMapperProvider extends ObjectMapperFactory {
3738

3839
/**
3940
* The Json mapper.
@@ -79,4 +80,19 @@ public ObjectMapper jsonMapper() {
7980
public ObjectMapper yamlMapper() {
8081
return yamlMapper;
8182
}
83+
84+
/**
85+
* Create json object mapper.
86+
*
87+
* @param springDocConfigProperties the spring doc config properties
88+
* @return the object mapper
89+
*/
90+
public static ObjectMapper createJson(SpringDocConfigProperties springDocConfigProperties) {
91+
OpenApiVersion openApiVersion = springDocConfigProperties.getApiDocs().getVersion();
92+
if (openApiVersion == OpenApiVersion.OPENAPI_3_1)
93+
return ObjectMapperProvider.createJson31();
94+
else
95+
return ObjectMapperProvider.createJson();
96+
}
97+
8298
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app188;
24+
25+
26+
import java.time.Instant;
27+
import java.util.Map;
28+
29+
import io.swagger.v3.oas.annotations.media.Schema;
30+
import io.swagger.v3.oas.models.Components;
31+
import io.swagger.v3.oas.models.OpenAPI;
32+
import io.swagger.v3.oas.models.Operation;
33+
import io.swagger.v3.oas.models.media.Content;
34+
import io.swagger.v3.oas.models.responses.ApiResponse;
35+
import org.springdoc.core.SpringDocAnnotationsUtils;
36+
import org.springdoc.core.customizers.OperationCustomizer;
37+
38+
import org.springframework.context.annotation.Bean;
39+
import org.springframework.http.MediaType;
40+
import org.springframework.web.bind.annotation.GetMapping;
41+
import org.springframework.web.bind.annotation.RestController;
42+
import org.springframework.web.method.HandlerMethod;
43+
44+
@RestController
45+
public class HelloController {
46+
47+
@GetMapping("/test")
48+
public void test(){}
49+
50+
@Bean
51+
public OpenAPI openAPI(){return new OpenAPI().components(new Components());}
52+
53+
@Bean
54+
public OperationCustomizer operationCustomizer(OpenAPI api){
55+
io.swagger.v3.oas.models.media.Schema errorResponseSchema= SpringDocAnnotationsUtils.extractSchema(
56+
api.getComponents(),
57+
ErrorResponse.class,
58+
null,
59+
null
60+
);
61+
62+
ApiResponse errorApiResponse=new ApiResponse().content(new Content().addMediaType(
63+
MediaType.APPLICATION_JSON_VALUE,
64+
new io.swagger.v3.oas.models.media.MediaType().schema(errorResponseSchema)
65+
));
66+
67+
return(Operation operation, HandlerMethod handlerMethod)->{
68+
operation.getResponses().addApiResponse("5xx",errorApiResponse);
69+
return operation;
70+
};
71+
}
72+
73+
public class ErrorResponse {
74+
@Schema(example = "2022-05-09T00:00:00.000Z")
75+
Instant timestamp;
76+
@Schema(example = "{\"param1\":\"val1\",\"param2\":\"val2\"}")
77+
Map<String, Object> data;
78+
79+
public Instant getTimestamp() {
80+
return timestamp;
81+
}
82+
83+
public void setTimestamp(Instant timestamp) {
84+
this.timestamp = timestamp;
85+
}
86+
87+
public Map<String, Object> getData() {
88+
return data;
89+
}
90+
91+
public void setData(Map<String, Object> data) {
92+
this.data = data;
93+
}
94+
}
95+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app188;
24+
25+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp188Test extends AbstractSpringDocV30Test {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
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)