diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index 25828cd7..17f72416 100644 --- a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java @@ -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; @@ -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); diff --git a/src/main/java/io/swagger/inflector/examples/models/BigIntegerExample.java b/src/main/java/io/swagger/inflector/examples/models/BigIntegerExample.java new file mode 100644 index 00000000..42681ddf --- /dev/null +++ b/src/main/java/io/swagger/inflector/examples/models/BigIntegerExample.java @@ -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; + } +} diff --git a/src/main/java/io/swagger/inflector/processors/JsonExampleDeserializer.java b/src/main/java/io/swagger/inflector/processors/JsonExampleDeserializer.java index 75d1eab3..b398b21e 100644 --- a/src/main/java/io/swagger/inflector/processors/JsonExampleDeserializer.java +++ b/src/main/java/io/swagger/inflector/processors/JsonExampleDeserializer.java @@ -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; @@ -36,23 +53,44 @@ public class JsonExampleDeserializer extends JsonDeserializer { @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> x = on.fields(); x.hasNext(); ) { Entry 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; } } diff --git a/src/main/java/io/swagger/inflector/processors/JsonNodeExampleSerializer.java b/src/main/java/io/swagger/inflector/processors/JsonNodeExampleSerializer.java index 908a754e..75ec6891 100644 --- a/src/main/java/io/swagger/inflector/processors/JsonNodeExampleSerializer.java +++ b/src/main/java/io/swagger/inflector/processors/JsonNodeExampleSerializer.java @@ -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; @@ -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) { diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index fb1de935..3bf455c8 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -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" + + "}"); + } } diff --git a/src/test/swagger/example-types.yaml b/src/test/swagger/example-types.yaml new file mode 100644 index 00000000..2ddf4b03 --- /dev/null +++ b/src/test/swagger/example-types.yaml @@ -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 diff --git a/src/test/swagger/issue-1177.yaml b/src/test/swagger/issue-1177.yaml new file mode 100644 index 00000000..cbde132f --- /dev/null +++ b/src/test/swagger/issue-1177.yaml @@ -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 \ No newline at end of file diff --git a/src/test/swagger/issue-1263.yaml b/src/test/swagger/issue-1263.yaml new file mode 100644 index 00000000..7387f4b7 --- /dev/null +++ b/src/test/swagger/issue-1263.yaml @@ -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 \ No newline at end of file