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

Added examples for body parameter #6228

Merged
merged 1 commit into from
Aug 19, 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
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 @@ -2274,6 +2274,10 @@ public CodegenOperation fromOperation(String path,
} else if (param instanceof BodyParameter) {
bodyParam = p;
bodyParams.add(p.copy());
if(definitions != null)
{
Copy link
Contributor

@wing328 wing328 Aug 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ToreJohansson Please update to if (definitions != null) { instead to conform to Java style guide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed via d6c9522

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 "";
}
}
}