Skip to content

Commit b03813f

Browse files
nitinjavakidfrantuma
authored andcommitted
Fix inheritance related code for allOf
1 parent 5ea42ce commit b03813f

File tree

5 files changed

+75
-61
lines changed

5 files changed

+75
-61
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/jackson/ModelResolver.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,7 @@
6565
import java.lang.annotation.Annotation;
6666
import java.lang.reflect.Type;
6767
import java.math.BigDecimal;
68-
import java.util.ArrayList;
69-
import java.util.Arrays;
70-
import java.util.Collection;
71-
import java.util.Collections;
72-
import java.util.HashMap;
73-
import java.util.HashSet;
74-
import java.util.Iterator;
75-
import java.util.LinkedHashMap;
76-
import java.util.List;
77-
import java.util.Map;
78-
import java.util.Set;
68+
import java.util.*;
7969
import java.util.stream.Collectors;
8070
import java.util.stream.Stream;
8171

@@ -795,8 +785,27 @@ public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context
795785

796786
});
797787

788+
if(composedSchema.getAllOf() != null && composedSchema.getAllOf().size() == 1)
789+
{
790+
ComposedSchema newSchema = new ComposedSchema();
791+
newSchema.setName(composedSchema.getName());
792+
composedSchema.setName(null);
793+
newSchema.addAllOfItem(composedSchema.getAllOf().get(0));
794+
composedSchema.setAllOf(null);
795+
newSchema.addAllOfItem(composedSchema);
796+
model = newSchema;
797+
}
798798
}
799-
if (model != null && annotatedType.isResolveAsRef() && "object".equals(model.getType()) && StringUtils.isNotBlank(model.getName())) {
799+
800+
if (!type.isContainerType() && StringUtils.isNotBlank(name)) {
801+
// define the model here to support self/cyclic referencing of models
802+
context.defineModel(name, model, annotatedType, null);
803+
}
804+
805+
if (model != null && annotatedType.isResolveAsRef() &&
806+
(isComposedSchema || "object".equals(model.getType())) &&
807+
StringUtils.isNotBlank(model.getName()))
808+
{
800809
if (context.getDefinedModels().containsKey(model.getName())) {
801810
model = new Schema().$ref(constructRef(model.getName()));
802811
}

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/ComposedSchemaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,4 @@ public void readComposedSchema_ticket2616() {
123123
model = schemas.get("objects");
124124
Assert.assertNull(model);
125125
}
126-
127126
}

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/InheritedBeanTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void testInheritedBean() throws Exception {
4747
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/BaseBean");
4848

4949
// make sure parent properties are filtered out of subclass
50-
assertSub1PropertiesValid(cm.getProperties());
50+
assertSub1PropertiesValid(cm.getAllOf().get(1).getProperties());
5151
}
5252

5353
@Test
@@ -60,7 +60,7 @@ public void testInheritedChildBean() throws Exception {
6060
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/BaseBean");
6161

6262
// make sure parent properties are filtered out of subclass
63-
assertSub1PropertiesValid(cm.getProperties());
63+
assertSub1PropertiesValid(cm.getAllOf().get(1).getProperties());
6464

6565
final Schema baseModel = context.getDefinedModels().get("BaseBean");
6666
assertNotNull(baseModel);
@@ -77,7 +77,7 @@ public void testComposedChildBean() throws Exception {
7777
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/BaseBean2");
7878

7979
// make sure parent properties are filtered out of subclass
80-
assertSub1PropertiesValid(cm.getProperties());
80+
assertSub1PropertiesValid(cm.getAllOf().get(1).getProperties());
8181

8282
final Schema baseModel = context.getDefinedModels().get("BaseBean2");
8383
assertNotNull(baseModel);
@@ -119,7 +119,7 @@ public void testHierarchy() throws Exception {
119119
ComposedSchema cm = (ComposedSchema) subModel;
120120
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/BaseBean3");
121121
// make sure parent properties are filtered out of subclass
122-
assertSub1PropertiesValid(cm.getProperties());
122+
assertSub1PropertiesValid(cm.getAllOf().get(1).getProperties());
123123

124124
// assert grandchild
125125
final Schema subSubModel = context.getDefinedModels().get("GrandChildBean3");
@@ -129,7 +129,7 @@ public void testHierarchy() throws Exception {
129129
cm = (ComposedSchema) subSubModel;
130130
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/ChildBean3");
131131
// make sure parent properties are filtered out of subclass
132-
assertSub2PropertiesValid(cm.getProperties());
132+
assertSub2PropertiesValid(cm.getAllOf().get(1).getProperties());
133133

134134
}
135135

@@ -310,15 +310,15 @@ public void testMultipleInheritedBean() throws Exception {
310310
ComposedSchema cm1 = (ComposedSchema) sub1Model;
311311
assertEquals(cm1.getAllOf().get(0).get$ref(), "#/components/schemas/MultipleBaseBean");
312312
// make sure parent properties are filtered out of subclass
313-
assertSub1PropertiesValid(cm1.getProperties());
313+
assertSub1PropertiesValid(cm1.getAllOf().get(1).getProperties());
314314

315315
final Schema sub2Model = context.getDefinedModels().get("MultipleSub2Bean");
316316
assertNotNull(sub2Model);
317317
assertTrue(sub2Model instanceof ComposedSchema);
318318
ComposedSchema cm2 = (ComposedSchema) sub2Model;
319319
assertEquals(cm2.getAllOf().get(0).get$ref(), "#/components/schemas/MultipleBaseBean");
320320
// make sure parent properties are filtered out of subclass
321-
assertSub2PropertiesValid(cm2.getProperties());
321+
assertSub2PropertiesValid(cm2.getAllOf().get(1).getProperties());
322322
}
323323

324324
@Test
@@ -330,7 +330,7 @@ public void testMultipleInheritedChildBean() throws Exception {
330330
ComposedSchema cm = (ComposedSchema) subModel;
331331
assertEquals(cm.getAllOf().get(0).get$ref(), "#/components/schemas/MultipleBaseBean");
332332
// make sure parent properties are filtered out of subclass
333-
assertSub1PropertiesValid(cm.getProperties());
333+
assertSub1PropertiesValid(cm.getAllOf().get(1).getProperties());
334334

335335
final Schema baseModel = context.getDefinedModels().get("MultipleBaseBean");
336336
assertNotNull(baseModel);
@@ -343,15 +343,15 @@ public void testMultipleInheritedChildBean() throws Exception {
343343
ComposedSchema cm1 = (ComposedSchema) sub1Model;
344344
assertEquals(cm1.getAllOf().get(0).get$ref(), "#/components/schemas/MultipleBaseBean");
345345
// make sure parent properties are filtered out of subclass
346-
assertSub1PropertiesValid(cm1.getProperties());
346+
assertSub1PropertiesValid(cm1.getAllOf().get(1).getProperties());
347347

348348
final Schema sub2Model = context.getDefinedModels().get("MultipleSub2Bean");
349349
assertNotNull(sub2Model);
350350
assertTrue(sub2Model instanceof ComposedSchema);
351351
ComposedSchema cm2 = (ComposedSchema) sub2Model;
352352
assertEquals(cm2.getAllOf().get(0).get$ref(), "#/components/schemas/MultipleBaseBean");
353353
// make sure parent properties are filtered out of subclass
354-
assertSub2PropertiesValid(cm2.getProperties());
354+
assertSub2PropertiesValid(cm2.getAllOf().get(1).getProperties());
355355
}
356356

357357
private void assertSub2PropertiesValid(Map<String, Schema> subProperties) {

modules/swagger-core/src/test/resources/AbstractBaseModelWithoutFields.json

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
"allOf": [
88
{
99
"$ref": "#/components/schemas/AbstractBaseModelWithoutFields"
10-
}
11-
],
12-
"type": "object",
13-
"properties": {
14-
"a": {
15-
"type": "string",
16-
"description": "Additional field a"
1710
},
18-
"x": {
19-
"type": "integer",
20-
"format": "int32",
21-
"description": "Additional field x"
11+
{
12+
"type": "object",
13+
"properties": {
14+
"a": {
15+
"type": "string",
16+
"description": "Additional field a"
17+
},
18+
"x": {
19+
"type": "integer",
20+
"format": "int32",
21+
"description": "Additional field x"
22+
}
23+
},
24+
"description": "Thing3"
2225
}
23-
},
24-
"description": "Thing3"
26+
]
2527
}
2628
}

modules/swagger-core/src/test/resources/ModelWithFieldWithSubTypes.json

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,46 @@
3030
"description": "Class that has a field that is the AbstractBaseModelWithSubTypes"
3131
},
3232
"Thing1": {
33-
"type": "object",
3433
"allOf": [
3534
{
3635
"$ref": "#/components/schemas/AbstractBaseModelWithSubTypes"
37-
}
38-
],
39-
"properties": {
40-
"a": {
41-
"type": "string",
42-
"description": "Override the abstract a"
4336
},
44-
"x": {
45-
"type": "integer",
46-
"format": "int32",
47-
"description": "Thing1 has an additional field"
37+
{
38+
"type": "object",
39+
"properties": {
40+
"a": {
41+
"type": "string",
42+
"description": "Override the abstract a"
43+
},
44+
"x": {
45+
"type": "integer",
46+
"format": "int32",
47+
"description": "Thing1 has an additional field"
48+
}
49+
},
50+
"description": "Shake hands with Thing1"
4851
}
49-
},
50-
"description": "Shake hands with Thing1"
52+
]
5153
},
5254
"Thing2": {
53-
"type": "object",
5455
"allOf": [
5556
{
5657
"$ref": "#/components/schemas/AbstractBaseModelWithSubTypes"
57-
}
58-
],
59-
"properties": {
60-
"a": {
61-
"type": "string",
62-
"description": "Override the abstract a"
6358
},
64-
"s": {
65-
"type": "string",
66-
"description": "Thing2 has an additional field"
59+
{
60+
"type": "object",
61+
"properties": {
62+
"a": {
63+
"type": "string",
64+
"description": "Override the abstract a"
65+
},
66+
"s": {
67+
"type": "string",
68+
"description": "Thing2 has an additional field"
69+
}
70+
},
71+
"description": "and Thing2"
6772
}
68-
},
69-
"description": "and Thing2"
73+
]
7074
}
7175
}

0 commit comments

Comments
 (0)