Skip to content

Commit 298e8ee

Browse files
authored
Merge pull request #2058 from swagger-api/issue-1743
Added boolean enums
2 parents 03b7ad4 + 30fc2e1 commit 298e8ee

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-23
lines changed

modules/swagger-core/src/test/java/io/swagger/ModelPropertyTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import io.swagger.models.Model;
66
import io.swagger.models.Model1979;
77
import io.swagger.models.ModelImpl;
8+
import io.swagger.models.ModelWithBooleanProperty;
89
import io.swagger.models.ModelWithModelPropertyOverrides;
910
import io.swagger.models.ModelWithPrimitiveArray;
1011
import io.swagger.models.ReadOnlyFields;
1112
import io.swagger.models.properties.ArrayProperty;
13+
import io.swagger.models.properties.BooleanProperty;
1214
import io.swagger.models.properties.IntegerProperty;
1315
import io.swagger.models.properties.LongProperty;
1416
import io.swagger.models.properties.Property;
@@ -107,6 +109,18 @@ public void modelAllowEmptyTest() {
107109
assertTrue(model.getProperties().get("id").getAllowEmptyValue());
108110
}
109111

112+
113+
@Test
114+
public void testIssue1743() {
115+
final Map<String, Model> models = ModelConverters.getInstance().readAll(ModelWithBooleanProperty.class);
116+
final Model model = models.get("ModelWithBooleanProperty");
117+
assertNotNull(model);
118+
119+
BooleanProperty bp = (BooleanProperty) model.getProperties().get("isGreat");
120+
assertTrue(bp.getEnum().size() == 1);
121+
assertEquals(bp.getEnum().get(0), Boolean.TRUE);
122+
}
123+
110124
class Family {
111125
public Date membersSince;
112126
public List<Person> members;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.swagger.models;
2+
3+
import io.swagger.annotations.ApiModelProperty;
4+
5+
public class ModelWithBooleanProperty {
6+
@ApiModelProperty(allowableValues = "true")
7+
public Boolean isGreat;
8+
9+
@ApiModelProperty(allowableValues = "1,2")
10+
public Integer intValue;
11+
}

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

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import io.swagger.models.Xml;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
public class BooleanProperty extends AbstractProperty implements Property {
69
public static final String TYPE = "boolean";
710
protected Boolean _default;
11+
protected List<Boolean> _enum;
812

913
public BooleanProperty() {
1014
super.type = TYPE;
@@ -14,6 +18,16 @@ public static boolean isType(String type, String format) {
1418
return TYPE.equals(type);
1519
}
1620

21+
public BooleanProperty _enum(Boolean value) {
22+
if (this._enum == null) {
23+
this._enum = new ArrayList<Boolean>();
24+
}
25+
if (!_enum.contains(value)) {
26+
_enum.add(value);
27+
}
28+
return this;
29+
}
30+
1731
public BooleanProperty xml(Xml xml) {
1832
this.setXml(xml);
1933
return this;
@@ -54,9 +68,39 @@ public BooleanProperty vendorExtension(String key, Object obj) {
5468
return this;
5569
}
5670

71+
@Override
72+
public boolean equals(Object o) {
73+
if (this == o) {
74+
return true;
75+
}
76+
if (!(o instanceof BooleanProperty)) {
77+
return false;
78+
}
79+
if (!super.equals(o)) {
80+
return false;
81+
}
82+
83+
BooleanProperty that = (BooleanProperty) o;
84+
85+
if (_default != null ? !_default.equals(that._default) : that._default != null) {
86+
return false;
87+
}
88+
return _enum != null ? _enum.equals(that._enum) : that._enum == null;
89+
90+
}
91+
92+
@Override
93+
public int hashCode() {
94+
int result = super.hashCode();
95+
result = 31 * result + (_default != null ? _default.hashCode() : 0);
96+
result = 31 * result + (_enum != null ? _enum.hashCode() : 0);
97+
return result;
98+
}
99+
57100
public BooleanProperty readOnly() {
58101
this.setReadOnly(Boolean.TRUE);
59102
return this;
103+
60104
}
61105

62106
public Boolean getDefault() {
@@ -71,30 +115,12 @@ public void setDefault(Boolean _default) {
71115
this._default = _default;
72116
}
73117

74-
@Override
75-
public int hashCode() {
76-
final int prime = 31;
77-
int result = super.hashCode();
78-
result = prime * result + ((_default == null) ? 0 : _default.hashCode());
79-
return result;
118+
public List<Boolean> getEnum() {
119+
return _enum;
80120
}
81121

82-
@Override
83-
public boolean equals(Object obj) {
84-
if (!super.equals(obj)) {
85-
return false;
86-
}
87-
if (!(obj instanceof BooleanProperty)) {
88-
return false;
89-
}
90-
BooleanProperty other = (BooleanProperty) obj;
91-
if (_default == null) {
92-
if (other._default != null) {
93-
return false;
94-
}
95-
} else if (!_default.equals(other._default)) {
96-
return false;
97-
}
98-
return true;
122+
public void setEnum(List<Boolean> _enum) {
123+
this._enum = _enum;
99124
}
125+
100126
}

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
@@ -783,6 +783,17 @@ public Property merge(Property property, Map<PropertyId, Object> args) {
783783
if(args.containsKey(PropertyId.ENUM)) {
784784
final List<String> values = PropertyId.ENUM.findValue(args);
785785
if(values != null) {
786+
if(property instanceof BooleanProperty) {
787+
BooleanProperty p = (BooleanProperty) property;
788+
for(String value : values) {
789+
try {
790+
p._enum(Boolean.parseBoolean(value));
791+
}
792+
catch(Exception e) {
793+
// continue
794+
}
795+
}
796+
}
786797
if(property instanceof IntegerProperty) {
787798
IntegerProperty p = (IntegerProperty) property;
788799
for(String value : values) {

0 commit comments

Comments
 (0)