Skip to content

Commit 3114c10

Browse files
committed
code review
1 parent e1fff95 commit 3114c10

File tree

6 files changed

+192
-43
lines changed

6 files changed

+192
-43
lines changed

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,32 @@ else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSi
110110
@Override
111111
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
112112
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
113-
114-
if (javaType == null || !chain.hasNext()) {
115-
return null;
116-
}
117-
118-
for (Field field : FieldUtils.getAllFields(javaType.getRawClass())) {
119-
if (field.isAnnotationPresent(JsonUnwrapped.class)) {
120-
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
113+
if (javaType != null) {
114+
for (Field field : FieldUtils.getAllFields(javaType.getRawClass())) {
115+
if (field.isAnnotationPresent(JsonUnwrapped.class)) {
116+
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
117+
}
121118
}
122-
}
123-
124-
String parentName = type.getParent() == null ? null : type.getParent().getName();
125-
if (parentName != null && PARENT_TYPES_TO_IGNORE.stream().noneMatch(parentName::startsWith)){
126-
type.resolveAsRef(true);
127-
}
128-
129-
Schema<?> resolvedSchema = getResolvedSchema(javaType, chain.next().resolve(type, context, chain));
130-
if (resolvedSchema == null || resolvedSchema.get$ref() == null) {
131-
return resolvedSchema;
132-
}
133-
134-
if(resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
135-
String schemaName = resolvedSchema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
136-
Schema existingSchema = context.getDefinedModels().get(schemaName);
137-
if (existingSchema != null && (existingSchema.getOneOf() != null || existingSchema.getAllOf() != null || existingSchema.getAnyOf() != null)) {
138-
return resolvedSchema;
119+
if (chain.hasNext()) {
120+
if (!type.isResolveAsRef() && type.getParent() != null
121+
&& PARENT_TYPES_TO_IGNORE.stream().noneMatch(ignore -> type.getParent().getName().startsWith(ignore)))
122+
type.resolveAsRef(true);
123+
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
124+
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
125+
if (resolvedSchema == null || resolvedSchema.get$ref() == null) {
126+
return resolvedSchema;
127+
}
128+
if(resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
129+
String schemaName = resolvedSchema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
130+
Schema existingSchema = context.getDefinedModels().get(schemaName);
131+
if (existingSchema != null && (existingSchema.getOneOf() != null || existingSchema.getAllOf() != null)) {
132+
return resolvedSchema;
133+
}
134+
}
135+
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
139136
}
140137
}
141-
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
138+
return null;
142139
}
143140

144141
/**

springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app13/SpringDocApp13Test.kt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,22 @@ import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVe
2323
import org.springframework.boot.autoconfigure.SpringBootApplication
2424
import org.springframework.boot.test.context.SpringBootTest
2525
import org.springframework.context.annotation.Bean
26+
import org.springframework.context.annotation.ComponentScan
2627
import org.springframework.context.annotation.Configuration
2728
import test.org.springdoc.api.AbstractKotlinSpringDocMVCTest
2829

2930

30-
@SpringBootTest//(classes = [Config::class])
3131
class SpringDocApp13Test : AbstractKotlinSpringDocMVCTest() {
3232

33-
34-
@Configuration
35-
class Config{
36-
@Bean
37-
fun springDocConfigProperties():SpringDocConfigProperties{
38-
val x= SpringDocConfigProperties()
39-
x.apiDocs.version = OpenApiVersion.OPENAPI_3_1
40-
return x
41-
}
42-
43-
}
44-
45-
46-
@SpringBootApplication
47-
class DemoApplication
33+
@SpringBootApplication
34+
class DemoApplication {
35+
@Bean
36+
fun springDocConfigProperties():SpringDocConfigProperties{
37+
val x= SpringDocConfigProperties()
38+
x.apiDocs.version = OpenApiVersion.OPENAPI_3_1
39+
return x
40+
}
41+
}
42+
4843

4944
}

springdoc-openapi-tests/springdoc-openapi-kotlin-webmvc-tests/src/test/kotlin/test/org/springdoc/api/app13/TestController.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package test.org.springdoc.api.app13
2020

2121

22-
import io.swagger.v3.oas.annotations.OpenAPI31
2322
import io.swagger.v3.oas.annotations.media.Schema
2423
import org.springframework.web.bind.annotation.PostMapping
2524
import org.springframework.web.bind.annotation.RequestBody
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app14
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication
22+
import org.springframework.context.annotation.ComponentScan
23+
import test.org.springdoc.api.AbstractKotlinSpringDocMVCTest
24+
25+
class SpringDocApp14Test : AbstractKotlinSpringDocMVCTest() {
26+
27+
@SpringBootApplication
28+
class DemoApplication
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app14
20+
21+
import io.swagger.v3.oas.annotations.media.Schema
22+
import org.springframework.web.bind.annotation.PostMapping
23+
import org.springframework.web.bind.annotation.RequestBody
24+
import org.springframework.web.bind.annotation.RequestMapping
25+
import org.springframework.web.bind.annotation.RestController
26+
27+
@Schema(description = "Generic description")
28+
data class KeyValue(
29+
val key: String,
30+
val value: String,
31+
)
32+
33+
@Schema
34+
data class SomeDTO(
35+
@Schema(description = "Description A", allOf = [KeyValue::class]) val field_a: KeyValue,
36+
@Schema(description = "Description B", allOf = [KeyValue::class]) val field_b: KeyValue,
37+
)
38+
39+
@RestController
40+
@RequestMapping("/test")
41+
class TestController {
42+
43+
@PostMapping("/test")
44+
fun create(@RequestBody some: SomeDTO) {
45+
46+
}
47+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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/test": {
15+
"post": {
16+
"tags": [
17+
"test-controller"
18+
],
19+
"operationId": "create",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"$ref": "#/components/schemas/SomeDTO"
25+
}
26+
}
27+
},
28+
"required": true
29+
},
30+
"responses": {
31+
"200": {
32+
"description": "OK"
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"components": {
39+
"schemas": {
40+
"KeyValue": {
41+
"required": [
42+
"key",
43+
"value"
44+
],
45+
"type": "object",
46+
"description": "Generic description",
47+
"allOf": [
48+
{
49+
"$ref": "#/components/schemas/KeyValue"
50+
},
51+
{
52+
"type": "object",
53+
"properties": {
54+
"key": {
55+
"type": "string"
56+
},
57+
"value": {
58+
"type": "string"
59+
}
60+
}
61+
}
62+
]
63+
},
64+
"SomeDTO": {
65+
"required": [
66+
"field_a",
67+
"field_b"
68+
],
69+
"type": "object",
70+
"properties": {
71+
"field_a": {
72+
"$ref": "#/components/schemas/KeyValue"
73+
},
74+
"field_b": {
75+
"$ref": "#/components/schemas/KeyValue"
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)