Skip to content

Commit 369fdbe

Browse files
committed
refs - swagger-api/swagger-parser/issues/1143 - adding support for properties in ComposedModel
1 parent 0f41e42 commit 369fdbe

File tree

5 files changed

+97
-18
lines changed

5 files changed

+97
-18
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,7 @@ public CodegenModel fromModel(String name, Model model, Map<String, Model> allDe
14131413
}
14141414
// parent model
14151415
Model parent = (Model) composed.getParent();
1416+
addProperties(properties, required, model, allDefinitions);
14161417

14171418
// interfaces (intermediate models)
14181419
if (composed.getInterfaces() != null) {
@@ -1556,9 +1557,14 @@ protected void addProperties(Map<String, Property> properties,
15561557
}
15571558
} else if (model instanceof RefModel) {
15581559
String interfaceRef = ((RefModel) model).getSimpleRef();
1559-
Model interfaceModel = allDefinitions.get(interfaceRef);
1560-
addProperties(properties, required, interfaceModel, allDefinitions);
1560+
if (allDefinitions != null) {
1561+
Model interfaceModel = allDefinitions.get(interfaceRef);
1562+
addProperties(properties, required, interfaceModel, allDefinitions);
1563+
}
15611564
} else if (model instanceof ComposedModel) {
1565+
if (model.getProperties() != null) {
1566+
properties.putAll(model.getProperties());
1567+
}
15621568
for (Model component :((ComposedModel) model).getAllOf()) {
15631569
addProperties(properties, required, component, allDefinitions);
15641570
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414

1515
public class CodegenTest {
1616

17+
@Test(description = "handle simple composition")
18+
public void propertiesInComposedModelTest() {
19+
final Swagger swagger = parseAndPrepareSwagger("src/test/resources/2_0/allOfProperties.yaml");
20+
final DefaultCodegen codegen = new DefaultCodegen();
21+
codegen.supportsInheritance = true;
22+
final Model model = swagger.getDefinitions().get("RedisResource");
23+
CodegenModel composed = codegen.fromModel("RedisResource", model, swagger.getDefinitions());
24+
25+
Assert.assertEquals(composed.vars.size(), 3);
26+
Assert.assertEquals(composed.vars.get(0).baseName, "properties");
27+
Assert.assertEquals(composed.vars.get(1).baseName, "zones");
28+
Assert.assertEquals(composed.vars.get(2).baseName, "modelOneProp");
29+
Assert.assertNull(composed.parent);
30+
}
31+
1732
@Test(description = "test sanitizeTag")
1833
public void sanitizeTagTest() {
1934
final DefaultCodegen codegen = new DefaultCodegen();
@@ -284,9 +299,9 @@ public void simpleInheritanceTest() {
284299
final Model model = swagger.getDefinitions().get("ChildOfSimpleParent");
285300
CodegenModel child = codegen.fromModel("ChildOfSimpleParent", model, swagger.getDefinitions());
286301

287-
Assert.assertEquals(child.vars.size(), 2);
302+
Assert.assertEquals(child.vars.size(), 4);
288303
Assert.assertEquals(child.vars.get(0).baseName, "modelOneProp");
289-
Assert.assertEquals(child.vars.get(1).baseName, "childOfSimpleParentProp");
304+
Assert.assertEquals(child.vars.get(3).baseName, "childOfSimpleParentProp");
290305
Assert.assertEquals(child.parent, "SimpleParent");
291306
}
292307

@@ -298,8 +313,8 @@ public void multiInheritanceTest() {
298313
final Model model = swagger.getDefinitions().get("ChildOfChildOfSimpleParent");
299314
CodegenModel child = codegen.fromModel("ChildOfChildOfSimpleParent", model, swagger.getDefinitions());
300315

301-
Assert.assertEquals(child.vars.size(), 1);
302-
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfSimpleParentProp");
316+
Assert.assertEquals(child.vars.size(), 5);
317+
Assert.assertEquals(child.vars.get(4).baseName, "childOfChildOfSimpleParentProp");
303318
Assert.assertEquals(child.parent, "ChildOfSimpleParent");
304319
}
305320

@@ -328,8 +343,8 @@ public void supportsMixinsTest() {
328343
final Model model = swagger.getDefinitions().get("ChildOfChildOfSimpleParent");
329344
CodegenModel child = codegen.fromModel("ChildOfChildOfSimpleParent", model, swagger.getDefinitions());
330345

331-
Assert.assertEquals(child.vars.size(), 1);
332-
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfSimpleParentProp");
346+
Assert.assertEquals(child.vars.size(), 5);
347+
Assert.assertEquals(child.vars.get(4).baseName, "childOfChildOfSimpleParentProp");
333348
Assert.assertEquals(child.allVars.size(), 5);
334349
Assert.assertEquals(child.allVars.get(0).baseName, "modelOneProp");
335350
Assert.assertEquals(child.allVars.get(1).baseName, "disc");
@@ -348,8 +363,8 @@ public void inheritanceOfComposedModelTest() {
348363
final Model model = swagger.getDefinitions().get("ChildOfComposedParent");
349364
CodegenModel child = codegen.fromModel("ChildOfComposedParent", model, swagger.getDefinitions());
350365

351-
Assert.assertEquals(child.vars.size(), 1);
352-
Assert.assertEquals(child.vars.get(0).baseName, "childOfComposedParentProp");
366+
Assert.assertEquals(child.vars.size(), 5);
367+
Assert.assertEquals(child.vars.get(4).baseName, "childOfComposedParentProp");
353368
Assert.assertEquals(child.parent, "ComposedParent");
354369
}
355370

@@ -361,8 +376,8 @@ public void multiInheritanceOfComposedModelTest() {
361376
final Model model = swagger.getDefinitions().get("ChildOfChildOfComposedParent");
362377
CodegenModel child = codegen.fromModel("ChildOfChildOfComposedParent", model, swagger.getDefinitions());
363378

364-
Assert.assertEquals(child.vars.size(), 1);
365-
Assert.assertEquals(child.vars.get(0).baseName, "childOfChildOfComposedParentProp");
379+
Assert.assertEquals(child.vars.size(), 6);
380+
Assert.assertEquals(child.vars.get(5).baseName, "childOfChildOfComposedParentProp");
366381
Assert.assertEquals(child.parent, "ChildOfComposedParent");
367382
}
368383

modules/swagger-codegen/src/test/java/io/swagger/codegen/javascript/JavaScriptInheritanceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public void javascriptInheritanceTest() {
4848
Assert.assertEquals(cm.parent, "Base");
4949
Assert.assertEquals(cm.interfaces, Arrays.asList("Interface1", "Interface2"));
5050
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
51-
Assert.assertEquals(cm.vars.size(), 1);
52-
Assert.assertEquals(cm.vars.get(0).name, "childProp");
51+
Assert.assertEquals(cm.vars.size(), 4);
52+
Assert.assertEquals(cm.vars.get(3).name, "childProp");
5353
Assert.assertEquals(cm.allVars.size(), 4);
5454
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
5555
for (int i = 0; i < allVars.length; i++) {
5656
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
5757
}
58-
Assert.assertEquals(cm.mandatory, Sets.newHashSet("childProp"));
58+
Assert.assertEquals(cm.mandatory, Sets.newHashSet("baseProp", "intf2Prop", "childProp"));
5959
Assert.assertEquals(cm.allMandatory, Sets.newHashSet("baseProp", "intf2Prop", "childProp"));
6060
}
6161

@@ -91,7 +91,7 @@ public void javascriptNoInheritanceTest() {
9191
Assert.assertEquals(cm.imports, Sets.newHashSet("Base", "Interface1", "Interface2"));
9292
Assert.assertEquals(cm.vars.size(), 4);
9393
Assert.assertEquals(cm.allVars.size(), 4);
94-
String[] allVars = {"intf1Prop", "intf2Prop", "baseProp", "childProp"};
94+
String[] allVars = {"baseProp", "intf1Prop", "intf2Prop", "childProp"};
9595
for (int i = 0; i < allVars.length; i++) {
9696
Assert.assertEquals(cm.vars.get(i).name, allVars[i]);
9797
Assert.assertEquals(cm.allVars.get(i).name, allVars[i]);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
swagger: '2.0'
2+
info:
3+
version: 0.0.0
4+
title: Simple API
5+
paths:
6+
/:
7+
get:
8+
responses:
9+
200:
10+
description: OK
11+
12+
definitions:
13+
14+
ModelOne:
15+
type: object
16+
properties:
17+
modelOneProp:
18+
type: string
19+
20+
ModelTwo:
21+
type: object
22+
properties:
23+
modelTwoProp:
24+
type: string
25+
26+
ModelThree:
27+
type: object
28+
properties:
29+
modelThreeProp:
30+
type: string
31+
32+
33+
User_Id_Resource:
34+
allOf:
35+
- "$ref": "#/definitions/ModelTwo"
36+
properties:
37+
aplicacion:
38+
type: string
39+
description: some description
40+
example: some example value
41+
42+
RedisResource:
43+
properties:
44+
properties:
45+
x-ms-client-flatten: true
46+
"$ref": "#/definitions/ModelTwo"
47+
description: Redis cache properties.
48+
zones:
49+
type: array
50+
items:
51+
type: string
52+
description: A list of availability zones denoting where the resource needs
53+
to come from.
54+
required:
55+
- properties
56+
allOf:
57+
- "$ref": "#/definitions/ModelOne"
58+
description: A single Redis item in List or Get Operation.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,10 @@
938938
</repository>
939939
</repositories>
940940
<properties>
941-
<swagger-parser-version>1.0.47</swagger-parser-version>
941+
<swagger-parser-version>1.0.48-SNAPSHOT</swagger-parser-version>
942942
<scala-version>2.11.1</scala-version>
943943
<felix-version>3.3.0</felix-version>
944-
<swagger-core-version>1.5.24</swagger-core-version>
944+
<swagger-core-version>1.5.25-SNAPSHOT</swagger-core-version>
945945
<commons-io-version>2.4</commons-io-version>
946946
<commons-cli-version>1.2</commons-cli-version>
947947
<junit-version>4.8.1</junit-version>

0 commit comments

Comments
 (0)