Improve generated examples for arrays

This commit is contained in:
Cas Eliëns
2017-01-18 11:36:36 +01:00
committed by Robert Winkler
parent d5cf4ad9e5
commit 3a0b9a00a3
4 changed files with 28 additions and 4 deletions

View File

@@ -65,11 +65,32 @@ public final class PropertyAdapter {
} else {
if (logger.isDebugEnabled()) logger.debug("generateExample for ref not RefProperty");
}
case "array":
if (property instanceof ArrayProperty) {
return generateArrayExample((ArrayProperty) property, markupDocBuilder);
}
default:
return property.getType();
}
}
/**
* Generate example for an ArrayProperty
*
* @param property ArrayProperty to generate example for
* @param markupDocBuilder MarkupDocBuilder containing all associated settings
* @return String example
*/
private static Object generateArrayExample(ArrayProperty property, MarkupDocBuilder markupDocBuilder) {
// TODO: Fix example generation for arrays. See Issue #180
Property itemProperty = property.getItems();
List<Object> exampleArray = new ArrayList<>();
exampleArray.add(generateExample(itemProperty, markupDocBuilder));
// ExamplesUtil.generateExampleForRefModel(true, itemProperty.getName(), /* TODO */, /* TODO */, markupDocBuilder, /* TODO */);
return exampleArray;
}
/**
* Convert a string {@code value} to specified {@code type}.
*

View File

@@ -372,11 +372,13 @@ public class PathOperationComponent extends MarkupComponent<PathOperationCompone
JsonNode rootNode = parseExample(entry.getValue());
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
if (!fieldsIterator.hasNext()) {
// workaround for "array" example
//TODO: print $ref'd examples correctly instead of just "array"
String example = stripExampleQuotes(Json.pretty(entry.getValue()));
example = Json.pretty(example);
String example = stripExampleQuotes(Json.pretty(rootNode));
// example = Json.pretty(example);
markupDocBuilder.listingBlock(example, "json");
}
while (fieldsIterator.hasNext()) {

View File

@@ -63,6 +63,9 @@ public class ExamplesUtil {
String simpleRef = ((RefProperty) schema).getSimpleRef();
example = generateExampleForRefModel(generateMissingExamples, simpleRef, definitions, definitionDocumentResolver, markupDocBuilder, new HashMap<>());
}
if (example == null && schema instanceof ArrayProperty && generateMissingExamples) {
example = generateExampleForArrayProperty((ArrayProperty) schema, definitions, definitionDocumentResolver, markupDocBuilder, new HashMap<>());
}
if (example == null && generateMissingExamples) {
example = PropertyAdapter.generateExample(schema, markupDocBuilder);
}

View File

@@ -41,7 +41,6 @@ Returns the navigation as a tree
===== Response 123
[source,json]
----
{
[ {
"id" : 0.0,
"childs" : [ {
@@ -49,7 +48,6 @@ Returns the navigation as a tree
"childs" : [ "..." ]
} ]
} ]
}
----