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

Handle different types and nested structures when deserializing an example. #183

Merged
merged 3 commits into from
Jan 24, 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
15 changes: 15 additions & 0 deletions src/main/java/io/swagger/inflector/examples/ExampleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.swagger.inflector.examples.models.LongExample;
import io.swagger.inflector.examples.models.ObjectExample;
import io.swagger.inflector.examples.models.StringExample;
import io.swagger.models.ArrayModel;
import io.swagger.models.ComposedModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
Expand Down Expand Up @@ -523,6 +524,20 @@ else if(model instanceof ComposedModel) {
mergeTo(ex, innerExamples);
output = ex;
}
else if(model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
ObjectExample ex = new ObjectExample();

Property inner = am.getItems();
if (inner != null) {
Example innerExample = fromProperty(inner, definitions, processedModels);
if (innerExample != null) {
ArrayExample an = new ArrayExample();
an.add(innerExample);
output = an;
}
}
}
if (output != null) {
if (attribute != null) {
output.setAttribute(attribute);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.swagger.inflector.examples.models;

import java.math.BigInteger;

public class BigIntegerExample extends AbstractExample {
private BigInteger value;

public BigIntegerExample() {
super.setTypeName("biginteger");
}

public BigIntegerExample(BigInteger value) {
this();
this.value = value;
}

public String asString() {
return value.toString();
}

public BigInteger getValue() {
return value != null ? value : new BigInteger("1180591620717411303424");
}

public void setValue(BigInteger value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.FloatNode;
import com.fasterxml.jackson.databind.node.IntNode;
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.databind.node.ShortNode;
import io.swagger.inflector.examples.models.ArrayExample;
import io.swagger.inflector.examples.models.BigIntegerExample;
import io.swagger.inflector.examples.models.BooleanExample;
import io.swagger.inflector.examples.models.DecimalExample;
import io.swagger.inflector.examples.models.DoubleExample;
import io.swagger.inflector.examples.models.Example;
import io.swagger.inflector.examples.models.FloatExample;
import io.swagger.inflector.examples.models.IntegerExample;
import io.swagger.inflector.examples.models.LongExample;
import io.swagger.inflector.examples.models.ObjectExample;
import io.swagger.inflector.examples.models.StringExample;

Expand All @@ -36,23 +53,44 @@ public class JsonExampleDeserializer extends JsonDeserializer<Example> {
@Override
public Example deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
Example output = null;
JsonNode node = jp.getCodec().readTree(jp);
return createExample(node);
}

private Example createExample(JsonNode node) {
if (node instanceof ObjectNode) {
ObjectExample obj = new ObjectExample();
ObjectNode on = (ObjectNode) node;

for (Iterator<Entry<String, JsonNode>> x = on.fields(); x.hasNext(); ) {
Entry<String, JsonNode> i = x.next();
String key = i.getKey();
JsonNode value = i.getValue();

obj.put(key, new StringExample(value.asText()));
output = obj;
obj.put(key, createExample(value));
}
return obj;
} else if (node instanceof ArrayNode) {
ArrayExample arr = new ArrayExample();
ArrayNode an = (ArrayNode) node;
for (JsonNode childNode : an) {
arr.add(createExample(childNode));
}
} else if (node instanceof TextNode) {
output = new StringExample(((TextNode) node).asText());
return arr;
} else if (node instanceof DoubleNode) {
return new DoubleExample(node.doubleValue());
} else if (node instanceof IntNode || node instanceof ShortNode) {
return new IntegerExample(node.intValue());
} else if (node instanceof FloatNode) {
return new FloatExample(node.floatValue());
} else if (node instanceof BigIntegerNode) {
return new BigIntegerExample(node.bigIntegerValue());
} else if (node instanceof DecimalNode) {
return new DecimalExample(node.decimalValue());
} else if (node instanceof LongNode) {
return new LongExample(node.longValue());
} else if (node instanceof BooleanNode) {
return new BooleanExample(node.booleanValue());
} else {
return new StringExample(node.asText());
}
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.swagger.inflector.examples.models.ArrayExample;
import io.swagger.inflector.examples.models.BigIntegerExample;
import io.swagger.inflector.examples.models.BooleanExample;
import io.swagger.inflector.examples.models.DecimalExample;
import io.swagger.inflector.examples.models.DoubleExample;
Expand Down Expand Up @@ -110,6 +111,14 @@ public void writeValue(JsonGenerator jgen, String field, Example o) throws IOExc
} else {
jgen.writeNumber(obj.getValue());
}
} else if (o instanceof BigIntegerExample) {
BigIntegerExample obj = (BigIntegerExample) o;
if (field != null) {
jgen.writeFieldName(field);
jgen.writeNumber(obj.getValue());
} else {
jgen.writeNumber(obj.getValue());
}
} else if (o instanceof DoubleExample) {
DoubleExample obj = (DoubleExample) o;
if (field != null) {
Expand Down
51 changes: 50 additions & 1 deletion src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,57 @@ public void testIssue1261InlineSchemaExample() throws Exception {

String output = Json.pretty(example);
assertEqualsIgnoreLineEnding(output, "{\n" +
" \"id\" : \"42\",\n" +
" \"id\" : 42,\n" +
" \"name\" : \"Arthur Dent\"\n" +
"}");
}

@Test
public void testIssue1177RefArrayExample() throws Exception {
Swagger swagger = new SwaggerParser().read("src/test/swagger/issue-1177.yaml");

Response response = swagger.getPath("/array").getGet().getResponses().get("200");
Example example = ExampleBuilder.fromProperty(response.getSchema(), swagger.getDefinitions());

String output = Json.pretty(example);
assertEqualsIgnoreLineEnding(output, "[ \"string\" ]");
}

@Test
public void testIssue1263SchemaExampleNestedObjects() throws Exception {
Swagger swagger = new SwaggerParser().read("src/test/swagger/issue-1263.yaml");

Response response = swagger.getPath("/nested_object").getGet().getResponses().get("200");
Example example = ExampleBuilder.fromProperty(response.getSchema(), swagger.getDefinitions());

String output = Json.pretty(example);
assertEqualsIgnoreLineEnding(output, "{\n" +
" \"nested_object\" : {\n" +
" \"foo\" : \"bar\"\n" +
" }\n" +
"}");
}

@Test
public void testDifferentExampleTypes() throws Exception {
Swagger swagger = new SwaggerParser().read("src/test/swagger/example-types.yaml");

Response response = swagger.getPath("/user").getGet().getResponses().get("200");
Example example = ExampleBuilder.fromProperty(response.getSchema(), swagger.getDefinitions());

String output = Json.pretty(example);
assertEqualsIgnoreLineEnding(output, "{\n" +
" \"obj\" : {\n" +
" \"b\" : \"ho\",\n" +
" \"a\" : \"hey\"\n" +
" },\n" +
" \"arr\" : [ \"hey\", \"ho\" ],\n" +
" \"double\" : 1.2,\n" +
" \"int\" : 42,\n" +
" \"biginteger\" : 118059162071741130342442,\n" +
" \"long\" : 1099511627776,\n" +
" \"boolean\" : true,\n" +
" \"string\" : \"Arthur Dent\"\n" +
"}");
}
}
31 changes: 31 additions & 0 deletions src/test/swagger/example-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
swagger: '2.0'
info:
version: 1.0.0
title: Different types in the example

paths:
/user:
get:
produces:
- application/json
responses:
200:
description: OK
schema:
type: object
properties:
id:
type: integer
format: int32
name:
type: string
required: [id, name]
example:
obj: {b: ho, a: hey}
arr: [hey, ho]
double: 1.2
int: 42
biginteger: 118059162071741130342442
long: 1099511627776
boolean: yes
string: Arthur Dent
22 changes: 22 additions & 0 deletions src/test/swagger/issue-1177.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
swagger: '2.0'
info:
version: 1.0.0
title: Array Mocking Test

produces:
- application/json

paths:
/array:
get:
responses:
200:
description: OK
schema:
$ref: "#/definitions/AnArray"

definitions:
AnArray:
type: array
items:
type: string
31 changes: 31 additions & 0 deletions src/test/swagger/issue-1263.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
swagger: '2.0'
info:
version: 1.0.0
title: Nested object example

paths:
/nested_object:
get:
produces:
- application/json
responses:
200:
description: OK
schema:
$ref: '#/definitions/ComplexObject'

definitions:
ComplexObject:
type: object
properties:
nested_object:
type: object
properties:
foo:
type: string
required: [foo]
required: [nested_object]
# If you remove this example, the generated response structure is correct
example:
nested_object:
foo: bar