com.deepoove.swagger.diff.output.MarkdownRender Maven / Gradle / Ivy
package com.deepoove.swagger.diff.output;
import static com.deepoove.swagger.diff.output.MarkdownRenderUtils.buildParentPhrase;
import static com.deepoove.swagger.diff.output.MarkdownRenderUtils.prefix;
import static com.deepoove.swagger.diff.output.MarkdownRenderUtils.sort;
import static com.deepoove.swagger.diff.output.MarkdownRenderUtils.sortedPrefixJoin;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.tuple.Pair;
import com.deepoove.swagger.diff.SwaggerDiff;
import com.deepoove.swagger.diff.model.ChangedEndpoint;
import com.deepoove.swagger.diff.model.ChangedExtensionGroup;
import com.deepoove.swagger.diff.model.ChangedOperation;
import com.deepoove.swagger.diff.model.ChangedParameter;
import com.deepoove.swagger.diff.model.ElProperty;
import com.deepoove.swagger.diff.model.Endpoint;
import io.swagger.models.HttpMethod;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
;
public class MarkdownRender implements Render {
final String H6 = "###### ";
final String H3 = "### ";
final String H2 = "## ";
final String BLOCKQUOTE = "> ";
final String CODE = "`";
final String PRE_CODE = " ";
final String PRE_LI = " ";
final String LI = "* ";
final String HR = "---\n\n";
final String ST = "~~";
// Change strings
final String DELETE = "Removed ";
final String INSERT = "Added ";
final String MODIFY = "Changed ";
String IT = "_";
String BD = "__";
String RIGHT_ARROW = " → ";
public MarkdownRender() {}
public String render(SwaggerDiff diff) {
List newEndpoints = diff.getNewEndpoints();
String ol_newEndpoint = ol_newEndpoint(newEndpoints);
List missingEndpoints = diff.getMissingEndpoints();
String ol_missingEndpoint = ol_missingEndpoint(missingEndpoints);
String ol_change = ol_changeSummary(diff).replace("\n\n", "\n");
return renderMarkdown(diff.getOldVersion(), diff.getNewVersion(), ol_newEndpoint, ol_missingEndpoint, ol_change);
}
public String renderBasic(SwaggerDiff diff) {
MarkdownRender renderer = new MarkdownRender();
renderer.IT = "";
renderer.BD = "";
renderer.RIGHT_ARROW = "->";
return renderer.render(diff);
}
private String renderMarkdown(String oldVersion, String newVersion, String ol_new, String ol_miss,
String ol_changed) {
StringBuffer sb = new StringBuffer();
sb.append(H2).append("Version " + oldVersion + " to " + newVersion + "\n").append(HR);
sb.append(H3).append("What's New\n").append(HR)
.append(ol_new).append("\n").append(H3)
.append("What's Deprecated\n").append(HR)
.append(ol_miss).append("\n").append(H3)
.append("What's Changed\n").append(HR)
.append(ol_changed);
return sb.toString();
}
private String ol_newEndpoint(List endpoints) {
if (null == endpoints) return "";
StringBuffer sb = new StringBuffer();
for (Endpoint endpoint : endpoints) {
sb.append(li_newEndpoint(endpoint.getMethod().toString(),
endpoint.getPathUrl(), endpoint.getSummary()));
}
return sb.toString();
}
private String li_newEndpoint(String method, String path, String desc) {
StringBuffer sb = new StringBuffer();
sb.append(LI).append(BD + CODE).append(method).append(CODE + BD)
.append(" " + path).append(" " + desc + "\n");
return sb.toString();
}
private String ol_missingEndpoint(List endpoints) {
if (null == endpoints) return "";
StringBuffer sb = new StringBuffer();
for (Endpoint endpoint : endpoints) {
sb.append(li_newEndpoint(endpoint.getMethod().toString(),
endpoint.getPathUrl(), endpoint.getSummary()));
}
return sb.toString();
}
private String ol_changeSummary(SwaggerDiff diff) {
StringBuffer sb = new StringBuffer();
ChangedExtensionGroup topLevelExts = diff.getChangedVendorExtensions();
sb.append(ul_changedVendorExtsDeep(topLevelExts, "")).append("\n");
List changedEndpoints = diff.getChangedEndpoints();
String ol_changed = ol_changed(changedEndpoints);
return sb.append(ol_changed).toString();
}
private String ol_changed(List changedEndpoints) {
if (null == changedEndpoints) return "";
String detailPrefix = PRE_LI;
String detailTitlePrefix = detailPrefix + LI;
String operationPrefix = LI + BD + CODE;
StringBuffer sb = new StringBuffer();
for (ChangedEndpoint changedEndpoint : changedEndpoints) {
String pathUrl = changedEndpoint.getPathUrl();
Map changedOperations = changedEndpoint
.getChangedOperations();
if (changedEndpoint.vendorExtensionsAreDiff()) {
sb.append(LI).append(pathUrl).append("\n")
.append(sortedPrefixJoin(ul_changedVendorExts(changedEndpoint), PRE_LI + LI));
}
for (Entry entry : changedOperations.entrySet()) {
String method = entry.getKey().toString();
ChangedOperation changedOperation = entry.getValue();
String desc = changedOperation.getSummary() != null
? " - " + changedOperation.getSummary()
: "";
StringBuffer ul_detail = new StringBuffer();
if (changedOperation.vendorExtensionsAreDiff()) {
ul_detail.append(sortedPrefixJoin(ul_changedVendorExts(changedOperation), detailPrefix + LI));
}
if (changedOperation.isDiffParam()) {
ul_detail.append(ul_param(changedOperation));
}
if (changedOperation.isDiffProp()) {
ul_detail.append(detailTitlePrefix)
.append(IT).append("Return Type").append(IT)
.append(ul_response(changedOperation));
}
if (changedOperation.hasSubGroup("responses")) {
ChangedExtensionGroup group = changedOperation.getSubGroup("responses");
if (group.vendorExtensionsAreDiff()) {
ul_detail.append(detailTitlePrefix)
.append(IT).append("Responses").append(IT).append("\n");
ul_detail.append(ul_changedVendorExtsDeep(group, PRE_LI + PRE_LI));
}
}
sb.append(operationPrefix).append(method).append(CODE + BD)
.append(" " + pathUrl + desc + " \n")
.append(ul_detail);
}
}
return sb.toString();
}
private String ul_changedVendorExtsDeep(ChangedExtensionGroup group, String pre) {
StringBuffer sb = new StringBuffer();
if (group.vendorExtensionsAreDiffShallow()) {
List changedVendorExts = sort(ul_changedVendorExts(group));
sb.append(sortedPrefixJoin(changedVendorExts, pre + LI));
}
for (Entry entry : group.getChangedSubGroups().entrySet()) {
String key = entry.getKey();
ChangedExtensionGroup subgroup = entry.getValue();
if (subgroup.vendorExtensionsAreDiff()) {
sb.append("\n").append(prefix(key, pre + LI)).append("\n");
sb.append(ul_changedVendorExtsDeep(subgroup, pre + PRE_LI));
}
}
return sb.toString();
}
private List ul_changedVendorExts(ChangedExtensionGroup group) {
ArrayList lines = new ArrayList();
for (String key : group.getIncreasedVendorExtensions().keySet()) {
lines.add(li_addVendorExt(key));
}
for (String key : group.getMissingVendorExtensions().keySet()) {
lines.add(li_missingVendorExt(key));
}
for (String key : group.getChangedVendorExtensions().keySet()) {
Pair
© 2015 - 2025 Weber Informatics LLC | Privacy Policy