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

deserialize as arrayModel, any model that has items and no specific type defined #4109

Merged
merged 2 commits into from
Feb 2, 2022
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 @@ -51,8 +51,9 @@ public Model deserialize(JsonParser jp, DeserializationContext ctxt)
return model;
} else {
sub = node.get("type");
JsonNode items = node.get("items");
Model model = null;
if (sub != null && "array".equals(((TextNode) sub).textValue())) {
if ((sub != null && "array".equals(((TextNode) sub).textValue())) || (items != null)){
model = Json.mapper().convertValue(node, ArrayModel.class);
} else {
model = Json.mapper().convertValue(node, ModelImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.AssertJUnit.assertNotNull;

import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.Swagger;
import io.swagger.models.properties.ObjectProperty;
import io.swagger.models.properties.MapProperty;
Expand All @@ -12,6 +15,7 @@
import io.swagger.util.ResourceUtils;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.util.Yaml;
import org.testng.annotations.Test;

import java.io.IOException;
Expand Down Expand Up @@ -117,4 +121,19 @@ public void testNestedUntypedProperty() throws IOException {
assertTrue(additionalProperties instanceof UntypedProperty);
assertEquals(additionalProperties.getDescription(), "map value");
}

@Test(description = "it should deserialize an array untyped")
public void testArrayUntypedModel() throws IOException {
final String json = "{\n" +
" \"description\":\"top level object\",\n" +
" \"items\":{}" +
"}";
final Model result = m.readValue(json, Model.class);
assertTrue(result instanceof ArrayModel);

final ArrayModel array = (ArrayModel) result;
assertEquals(array.getType(),"array");
assertEquals(array.getDescription(), "top level object");
assertNotNull(array.getItems());
}
}