Skip to content

Commit b274a0e

Browse files
committed
AdditionalModelsConverter Schema params rewriting. Fixes #1957
1 parent 94b29ba commit b274a0e

File tree

5 files changed

+184
-2
lines changed

5 files changed

+184
-2
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/AdditionalModelsConverter.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
import java.util.Iterator;
2727
import java.util.Map;
2828

29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.core.type.TypeReference;
2931
import com.fasterxml.jackson.databind.JavaType;
3032
import io.swagger.v3.core.converter.AnnotatedType;
3133
import io.swagger.v3.core.converter.ModelConverter;
3234
import io.swagger.v3.core.converter.ModelConverterContext;
3335
import io.swagger.v3.oas.models.media.Schema;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
3438
import org.springdoc.core.providers.ObjectMapperProvider;
3539

3640
/**
@@ -54,6 +58,11 @@ public class AdditionalModelsConverter implements ModelConverter {
5458
*/
5559
private static final Map<Class, Class> paramObjectReplacementMap = new HashMap<>();
5660

61+
/**
62+
* The constant LOGGER.
63+
*/
64+
private static final Logger LOGGER = LoggerFactory.getLogger(AdditionalModelsConverter.class);
65+
5766
/**
5867
* The Spring doc object mapper.
5968
*/
@@ -130,8 +139,15 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
130139
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
131140
if (javaType != null) {
132141
Class<?> cls = javaType.getRawClass();
133-
if (modelToSchemaMap.containsKey(cls))
134-
return modelToSchemaMap.get(cls);
142+
if (modelToSchemaMap.containsKey(cls)) {
143+
try {
144+
return springDocObjectMapper.jsonMapper()
145+
.readValue(springDocObjectMapper.jsonMapper().writeValueAsString(modelToSchemaMap.get(cls)), new TypeReference<Schema>() {});
146+
}
147+
catch (JsonProcessingException e) {
148+
LOGGER.warn("Json Processing Exception occurred: {}", e.getMessage());
149+
}
150+
}
135151
if (modelToClassMap.containsKey(cls))
136152
type = new AnnotatedType(modelToClassMap.get(cls)).resolveAsRef(true);
137153
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test.org.springdoc.api.v30.app198;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController("/")
9+
public class HelloController {
10+
11+
@GetMapping
12+
public Response test() {
13+
return new Response(BigDecimal.ONE, BigDecimal.ONE, BigDecimal.ONE);
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package test.org.springdoc.api.v30.app198;
2+
3+
import java.math.BigDecimal;
4+
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
7+
/**
8+
* @author bnasslahsen
9+
*/
10+
public class Response {
11+
12+
public Response(BigDecimal val1, BigDecimal val2, BigDecimal val3) {
13+
this.val1 = val1;
14+
this.val2 = val2;
15+
this.val3 = val3;
16+
}
17+
18+
@Schema(deprecated = true)
19+
@Deprecated
20+
private BigDecimal val1;
21+
22+
private BigDecimal val2;
23+
24+
private BigDecimal val3;
25+
26+
public BigDecimal getVal1() {
27+
return val1;
28+
}
29+
30+
public void setVal1(BigDecimal val1) {
31+
this.val1 = val1;
32+
}
33+
34+
public BigDecimal getVal2() {
35+
return val2;
36+
}
37+
38+
public void setVal2(BigDecimal val2) {
39+
this.val2 = val2;
40+
}
41+
42+
public BigDecimal getVal3() {
43+
return val3;
44+
}
45+
46+
public void setVal3(BigDecimal val3) {
47+
this.val3 = val3;
48+
}
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.app198;
24+
25+
import java.math.BigDecimal;
26+
27+
import io.swagger.v3.oas.models.media.Schema;
28+
import org.springdoc.core.SpringDocUtils;
29+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
30+
31+
import org.springframework.boot.autoconfigure.SpringBootApplication;
32+
33+
public class SpringDocApp198Test extends AbstractSpringDocV30Test {
34+
35+
static {
36+
SpringDocUtils.getConfig().replaceWithSchema(
37+
BigDecimal.class,
38+
new Schema<BigDecimal>().type("string").format("decimal")
39+
);
40+
}
41+
42+
@SpringBootApplication
43+
static class SpringDocTestApp {}
44+
45+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"/": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "test",
20+
"responses": {
21+
"200": {
22+
"description": "OK",
23+
"content": {
24+
"*/*": {
25+
"schema": {
26+
"$ref": "#/components/schemas/Response"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
},
35+
"components": {
36+
"schemas": {
37+
"Response": {
38+
"type": "object",
39+
"properties": {
40+
"val1": {
41+
"type": "string",
42+
"format": "decimal",
43+
"deprecated": true
44+
},
45+
"val2": {
46+
"type": "string",
47+
"format": "decimal"
48+
},
49+
"val3": {
50+
"type": "string",
51+
"format": "decimal"
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)