Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce UntypedProperty #2508

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import io.swagger.models.Swagger;
import io.swagger.models.properties.ObjectProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.UntypedProperty;
import io.swagger.models.properties.Property;
import io.swagger.util.Json;
import io.swagger.util.ResourceUtils;
Expand Down Expand Up @@ -98,4 +100,21 @@ public void testNestedObjectProperty() throws IOException {
final Map<String, Property> secondLevelProperties = ((ObjectProperty) property3).getProperties();
assertEquals(secondLevelProperties.size(), 1);
}

@Test(description = "it should deserialize untyped additionalProperties")
public void testNestedUntypedProperty() throws IOException {
final String json = "{\n" +
" \"type\":\"object\",\n" +
" \"description\":\"top level object\",\n" +
" \"additionalProperties\":{\n" +
" \"description\":\"map value\"\n" +
" }\n" +
"}";
final Property result = m.readValue(json, Property.class);
assertTrue(result instanceof MapProperty);

final Property additionalProperties = ((MapProperty) result).getAdditionalProperties();
assertTrue(additionalProperties instanceof UntypedProperty);
assertEquals(additionalProperties.getDescription(), "map value");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public ObjectProperty xml(Xml xml) {
return this;
}

public ObjectProperty example(String example) {
public ObjectProperty example(Object example) {
this.setExample(example);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,17 @@ protected ObjectProperty create() {
return new ObjectProperty();
}
},
UNTYPED(UntypedProperty.class) {
@Override
protected boolean isType(String type, String format) {
return UntypedProperty.isType(type, format);
}

@Override
protected UntypedProperty create() {
return new UntypedProperty();
}
},
ARRAY(ArrayProperty.class) {
@Override
protected boolean isType(String type, String format) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.swagger.models.properties;

import io.swagger.models.Xml;

import java.util.ArrayList;
import java.util.List;

public class UntypedProperty extends AbstractProperty implements Property {
public static final String TYPE = null;

public UntypedProperty() {
super.type = TYPE;
}

public UntypedProperty vendorExtension(String key, Object obj) {
this.setVendorExtension(key, obj);
return this;
}

public static boolean isType(String type) {
return TYPE == type;
}

public static boolean isType(String type, String format) {
return isType(type);
}

public UntypedProperty access(String access) {
this.setAccess(access);
return this;
}

public UntypedProperty description(String description) {
this.setDescription(description);
return this;
}

public UntypedProperty name(String name) {
this.setName(name);
return this;
}

public UntypedProperty title(String title) {
this.setTitle(title);
return this;
}

public UntypedProperty _default(String _default) {
this.setDefault(_default);
return this;
}

public UntypedProperty readOnly(boolean readOnly) {
this.setReadOnly(readOnly);
return this;
}

public UntypedProperty required(boolean required) {
this.setRequired(required);
return this;
}

public UntypedProperty readOnly() {
this.setReadOnly(Boolean.TRUE);
return this;
}

public UntypedProperty xml(Xml xml) {
this.setXml(xml);
return this;
}

public UntypedProperty example(Object example) {
this.setExample(example);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Iterator<Object[]> createPredefinedProperties() {
new BaseIntegerProperty(), new IntegerProperty(), new LongProperty(), new StringProperty(),
new UUIDProperty(), new BooleanProperty(), new ByteArrayProperty(), new ArrayProperty(),
new ObjectProperty(), new DateTimeProperty(), new DateProperty(), new RefProperty(),
new EmailProperty(),
new EmailProperty(), new UntypedProperty()
// new MapProperty() // MapProperty can't be distinguished from
// ObjectProperty
};
Expand Down Expand Up @@ -87,12 +87,14 @@ public Object[][] createCustomAndPlainData() {
// these are the types without formats (as long as not already in the
// table in the spec), and a "custom" format for each of the types.
// we expect to get the same Property class back in both cases.
return new Object[][]{{"integer", null, BaseIntegerProperty.class},
{"integer", "custom", BaseIntegerProperty.class}, {"number", null, DecimalProperty.class},
{"number", "custom", DecimalProperty.class}, {"string", "custom", StringProperty.class},
{"boolean", "custom", BooleanProperty.class}, {"object", null, ObjectProperty.class},
{"object", "custom", ObjectProperty.class}, {"array", null, ArrayProperty.class},
{"array", "custom", ArrayProperty.class}};
return new Object[][]{
{"integer", null, BaseIntegerProperty.class}, {"integer", "custom", BaseIntegerProperty.class},
{"number", null, DecimalProperty.class}, {"number", "custom", DecimalProperty.class},
{"string", "custom", StringProperty.class}, {"boolean", "custom", BooleanProperty.class},
{"object", null, ObjectProperty.class}, {"object", "custom", ObjectProperty.class},
{"array", null, ArrayProperty.class}, {"array", "custom", ArrayProperty.class},
{null, null, UntypedProperty.class}, {null, "custom", UntypedProperty.class}
};
}

@Test(dataProvider = BY_IMPLEMENTATION)
Expand Down