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 inlined array example in example builder (Swagger2) #239

Merged
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 @@ -325,7 +325,12 @@ public static Example fromProperty(Property property, Map<String, Model> definit
}
} else if (property instanceof ArrayProperty) {
if (example != null) {
output = new ArrayExample();
try {
output = Json.mapper().readValue(example.toString(), ArrayExample.class);
} catch (IOException e) {
LOGGER.error("unable to convert `" + example + "` to JsonNode");
output = new ArrayExample();
}
}
else {
ArrayProperty ap = (ArrayProperty) property;
Expand Down Expand Up @@ -527,7 +532,6 @@ else if(model instanceof ComposedModel) {
}
else if(model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
ObjectExample ex = new ObjectExample();

Property inner = am.getItems();
if (inner != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package io.swagger.inflector.examples.models;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.inflector.processors.JsonExampleDeserializer;

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

@JsonDeserialize(using = JsonExampleDeserializer.class)
public class ArrayExample extends AbstractExample {
List<Example> values = null;

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,20 @@ public void testIssue1177RefArrayExample() throws Exception {
assertEqualsIgnoreLineEnding(output, "[ \"string\" ]");
}

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

String output = getExampleForPath(swagger, "/");
assertEqualsIgnoreLineEnding(output, "[ {\n" +
" \"id\" : 1,\n" +
" \"name\" : \"Arthur Dent\"\n" +
"}, {\n" +
" \"id\" : 2,\n" +
" \"name\" : \"Ford Prefect\"\n" +
"} ]");
}

@Test
public void testIssue1263SchemaExampleNestedObjects() throws Exception {
Swagger swagger = new SwaggerParser().read("src/test/swagger/issue-1263.yaml");
Expand Down
32 changes: 32 additions & 0 deletions src/test/swagger/array-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
swagger: '2.0'
info:
version: '1.0'
title: Array example

paths:
/:
get:
produces:
- application/json
responses:
200:
description: completed successfully
schema:
type: array
items:
$ref: '#/definitions/User'
example:
- id: 1
name: Arthur Dent
- id: 2
name: Ford Prefect
definitions:
User:
type: object
properties:
id:
type: integer
example: 0
name:
type: string
example: Marvin