Skip to content

Commit 71ef957

Browse files
committed
Introduce UntypedProperty
Fixes #2507
1 parent 65e6b3a commit 71ef957

File tree

5 files changed

+117
-8
lines changed

5 files changed

+117
-8
lines changed

modules/swagger-core/src/test/java/io/swagger/deserialization/JsonDeserializationTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import io.swagger.models.Swagger;
77
import io.swagger.models.properties.ObjectProperty;
8+
import io.swagger.models.properties.MapProperty;
9+
import io.swagger.models.properties.UntypedProperty;
810
import io.swagger.models.properties.Property;
911
import io.swagger.util.Json;
1012
import io.swagger.util.ResourceUtils;
@@ -98,4 +100,21 @@ public void testNestedObjectProperty() throws IOException {
98100
final Map<String, Property> secondLevelProperties = ((ObjectProperty) property3).getProperties();
99101
assertEquals(secondLevelProperties.size(), 1);
100102
}
103+
104+
@Test(description = "it should deserialize untyped additionalProperties")
105+
public void testNestedUntypedProperty() throws IOException {
106+
final String json = "{\n" +
107+
" \"type\":\"object\",\n" +
108+
" \"description\":\"top level object\",\n" +
109+
" \"additionalProperties\":{\n" +
110+
" \"description\":\"map value\"\n" +
111+
" }\n" +
112+
"}";
113+
final Property result = m.readValue(json, Property.class);
114+
assertTrue(result instanceof MapProperty);
115+
116+
final Property additionalProperties = ((MapProperty) result).getAdditionalProperties();
117+
assertTrue(additionalProperties instanceof UntypedProperty);
118+
assertEquals(additionalProperties.getDescription(), "map value");
119+
}
101120
}

modules/swagger-models/src/main/java/io/swagger/models/properties/ObjectProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public ObjectProperty xml(Xml xml) {
135135
return this;
136136
}
137137

138-
public ObjectProperty example(String example) {
138+
public ObjectProperty example(Object example) {
139139
this.setExample(example);
140140
return this;
141141
}

modules/swagger-models/src/main/java/io/swagger/models/properties/PropertyBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,17 @@ protected ObjectProperty create() {
557557
return new ObjectProperty();
558558
}
559559
},
560+
UNTYPED(UntypedProperty.class) {
561+
@Override
562+
protected boolean isType(String type, String format) {
563+
return UntypedProperty.isType(type, format);
564+
}
565+
566+
@Override
567+
protected UntypedProperty create() {
568+
return new UntypedProperty();
569+
}
570+
},
560571
ARRAY(ArrayProperty.class) {
561572
@Override
562573
protected boolean isType(String type, String format) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package io.swagger.models.properties;
2+
3+
import io.swagger.models.Xml;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class UntypedProperty extends AbstractProperty implements Property {
9+
public static final String TYPE = null;
10+
11+
public UntypedProperty() {
12+
super.type = TYPE;
13+
}
14+
15+
public UntypedProperty vendorExtension(String key, Object obj) {
16+
this.setVendorExtension(key, obj);
17+
return this;
18+
}
19+
20+
public static boolean isType(String type) {
21+
return TYPE == type;
22+
}
23+
24+
public static boolean isType(String type, String format) {
25+
return isType(type);
26+
}
27+
28+
public UntypedProperty access(String access) {
29+
this.setAccess(access);
30+
return this;
31+
}
32+
33+
public UntypedProperty description(String description) {
34+
this.setDescription(description);
35+
return this;
36+
}
37+
38+
public UntypedProperty name(String name) {
39+
this.setName(name);
40+
return this;
41+
}
42+
43+
public UntypedProperty title(String title) {
44+
this.setTitle(title);
45+
return this;
46+
}
47+
48+
public UntypedProperty _default(String _default) {
49+
this.setDefault(_default);
50+
return this;
51+
}
52+
53+
public UntypedProperty readOnly(boolean readOnly) {
54+
this.setReadOnly(readOnly);
55+
return this;
56+
}
57+
58+
public UntypedProperty required(boolean required) {
59+
this.setRequired(required);
60+
return this;
61+
}
62+
63+
public UntypedProperty readOnly() {
64+
this.setReadOnly(Boolean.TRUE);
65+
return this;
66+
}
67+
68+
public UntypedProperty xml(Xml xml) {
69+
this.setXml(xml);
70+
return this;
71+
}
72+
73+
public UntypedProperty example(Object example) {
74+
this.setExample(example);
75+
return this;
76+
}
77+
}

modules/swagger-models/src/test/java/io/swagger/models/properties/PropertyBuilderTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Iterator<Object[]> createPredefinedProperties() {
4040
new BaseIntegerProperty(), new IntegerProperty(), new LongProperty(), new StringProperty(),
4141
new UUIDProperty(), new BooleanProperty(), new ByteArrayProperty(), new ArrayProperty(),
4242
new ObjectProperty(), new DateTimeProperty(), new DateProperty(), new RefProperty(),
43-
new EmailProperty(),
43+
new EmailProperty(), new UntypedProperty()
4444
// new MapProperty() // MapProperty can't be distinguished from
4545
// ObjectProperty
4646
};
@@ -87,12 +87,14 @@ public Object[][] createCustomAndPlainData() {
8787
// these are the types without formats (as long as not already in the
8888
// table in the spec), and a "custom" format for each of the types.
8989
// we expect to get the same Property class back in both cases.
90-
return new Object[][]{{"integer", null, BaseIntegerProperty.class},
91-
{"integer", "custom", BaseIntegerProperty.class}, {"number", null, DecimalProperty.class},
92-
{"number", "custom", DecimalProperty.class}, {"string", "custom", StringProperty.class},
93-
{"boolean", "custom", BooleanProperty.class}, {"object", null, ObjectProperty.class},
94-
{"object", "custom", ObjectProperty.class}, {"array", null, ArrayProperty.class},
95-
{"array", "custom", ArrayProperty.class}};
90+
return new Object[][]{
91+
{"integer", null, BaseIntegerProperty.class}, {"integer", "custom", BaseIntegerProperty.class},
92+
{"number", null, DecimalProperty.class}, {"number", "custom", DecimalProperty.class},
93+
{"string", "custom", StringProperty.class}, {"boolean", "custom", BooleanProperty.class},
94+
{"object", null, ObjectProperty.class}, {"object", "custom", ObjectProperty.class},
95+
{"array", null, ArrayProperty.class}, {"array", "custom", ArrayProperty.class},
96+
{null, null, UntypedProperty.class}, {null, "custom", UntypedProperty.class}
97+
};
9698
}
9799

98100
@Test(dataProvider = BY_IMPLEMENTATION)

0 commit comments

Comments
 (0)