Skip to content

Commit

Permalink
Added examples for body parameter (#6228)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToreJohansson authored and wing328 committed Aug 19, 2017
1 parent 88e99f1 commit dabe6b5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CodegenOperation {
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
public Set<String> imports = new HashSet<String>();
public List<Map<String, String>> examples;
public List<Map<String, String>> requestBodyExamples;
public ExternalDocs externalDocs;
public Map<String, Object> vendorExtensions;
public String nickname; // legacy support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,10 @@ public CodegenOperation fromOperation(String path,
} else if (param instanceof BodyParameter) {
bodyParam = p;
bodyParams.add(p.copy());
if(definitions != null)
{
op.requestBodyExamples = new ExampleGenerator(definitions).generate(null, operation.getConsumes(), bodyParam.dataType);
}
} else if (param instanceof FormParameter) {
formParams.add(p.copy());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,53 @@ public List<Map<String, String>> generate(Map<String, Object> examples, List<Str
return output;
}

public List<Map<String, String>> generate(Map<String, Object> examples, List<String> mediaTypes, String modelName) {
List<Map<String, String>> output = new ArrayList<>();
Set<String> processedModels = new HashSet<>();
if (examples == null) {
if (mediaTypes == null) {
// assume application/json for this
mediaTypes = Collections.singletonList(MIME_TYPE_JSON); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
}
for (String mediaType : mediaTypes) {
Map<String, String> kv = new HashMap<>();
kv.put(CONTENT_TYPE, mediaType);
if (modelName != null && mediaType.startsWith(MIME_TYPE_JSON)) {
final Model model = this.examples.get(modelName);
if (model != null) {

String example = Json.pretty(resolveModelToExample(modelName, mediaType, model, processedModels));

if (example != null) {
kv.put(EXAMPLE, example);
output.add(kv);
}
}
} else if (modelName != null && mediaType.startsWith(MIME_TYPE_XML)) {
final Model model = this.examples.get(modelName);
String example = new XmlExampleGenerator(this.examples).toXml(model, 0, Collections.<String>emptySet());
if (example != null) {
kv.put(EXAMPLE, example);
output.add(kv);
}
}
}
} else {
for (Map.Entry<String, Object> entry : examples.entrySet()) {
final Map<String, String> kv = new HashMap<>();
kv.put(CONTENT_TYPE, entry.getKey());
kv.put(EXAMPLE, Json.pretty(entry.getValue()));
output.add(kv);
}
}
if (output.size() == 0) {
Map<String, String> kv = new HashMap<>();
kv.put(OUTPUT, NONE);
output.add(kv);
}
return output;
}

private Object resolvePropertyToExample(String propertyName, String mediaType, Property property, Set<String> processedModels) {
logger.debug("Resolving example for property {}...", property);
if (property.getExample() != null) {
Expand Down Expand Up @@ -209,4 +256,4 @@ private Object resolveModelToExample(String name, String mediaType, Model model,
}
return "";
}
}
}

0 comments on commit dabe6b5

Please sign in to comment.