com.reprezen.genflow.openapi.diagram.swagger.SwaggerDiagramData Maven / Gradle / Ivy
/**
* i * Copyright © 2013, 2016 Modelsolv, Inc.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains the property
* of ModelSolv, Inc. See the file license.html in the root directory of
* this project for further information.
*/
package com.reprezen.genflow.openapi.diagram.swagger;
import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.reprezen.genflow.common.services.MethodServices;
import com.reprezen.genflow.openapi.diagram.swagger.Anchors;
import com.reprezen.genflow.openapi.diagram.swagger.StatusCodeComparator;
import com.reprezen.genflow.openapi.diagram.swagger.SwaggerDiagramTypes;
import com.reprezen.genflow.openapi.diagram.swagger.XtendHelper;
import io.swagger.models.HttpMethod;
import io.swagger.models.Info;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.FormParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.apache.commons.text.StringEscapeUtils;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
@SuppressWarnings("all")
public class SwaggerDiagramData {
private static final StatusCodeComparator STATUS_CODE_COMPARATOR = new StatusCodeComparator();
private static final int DEFAULT_RESPONSE_CODE = (-1);
@Extension
private XtendHelper _xtendHelper = new XtendHelper();
@Extension
private Anchors _anchors = new Anchors();
private final SwaggerDiagramTypes typesProvider;
private final Swagger swagger;
public SwaggerDiagramData(final Swagger model) {
this.swagger = model;
SwaggerDiagramTypes _swaggerDiagramTypes = new SwaggerDiagramTypes(model);
this.typesProvider = _swaggerDiagramTypes;
}
public String generateDiagramData() {
final String result = this.generateJSON(this.swagger);
return result;
}
public String generateJSON(final Swagger swagger) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append("\t");
_builder.append("\"ResourceAPI\": {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\"objecttype\": \"ResourceAPI\",");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\"name\": \"");
Info _info = swagger.getInfo();
String _title = null;
if (_info!=null) {
_title=_info.getTitle();
}
String _escapeEcmaScript = StringEscapeUtils.escapeEcmaScript(_title);
_builder.append(_escapeEcmaScript, "\t\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\"anchorId\": \"");
String _htmlLink = this._anchors.htmlLink(swagger);
_builder.append(_htmlLink, "\t\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\"baseURI\": \"");
String _host = swagger.getHost();
String _basePath = swagger.getBasePath();
String _plus = (_host + _basePath);
String _escapeEcmaScript_1 = StringEscapeUtils.escapeEcmaScript(_plus);
_builder.append(_escapeEcmaScript_1, "\t\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("\"resources\": [");
_builder.newLine();
_builder.append("\t\t");
final Function2, Integer, String> _function = (Map.Entry e, Integer idx) -> {
return this.generateResource(e.getKey(), e.getValue(), (idx).intValue());
};
String _joinedMap = this._xtendHelper.>joinedMap(this._xtendHelper.safe(swagger.getPaths()).entrySet(), ",", _function);
_builder.append(_joinedMap, "\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
public String generateResource(final String uri, final Path path, final int pathIdx) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"ObjectResource\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"name\": \"");
_builder.append(uri, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"anchorId\": \"");
String _htmlLink = this._anchors.htmlLink(path);
_builder.append(_htmlLink, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"id\": \"");
_builder.append(uri, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"URI\": {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\"name\": [ ");
_builder.newLine();
_builder.append("\t\t");
_builder.append("{");
_builder.newLine();
_builder.append("\t");
_builder.append("\"objecttype\": \"URISegment\", ");
_builder.newLine();
_builder.append("\t");
_builder.append("\"label\": \"\", ");
_builder.newLine();
_builder.append("\t");
_builder.append("\"id\": \"");
_builder.append(uri, "\t");
_builder.append(".URI.");
_builder.append(pathIdx, "\t");
_builder.append("\"");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("],");
_builder.newLine();
_builder.append("\t");
_builder.append("\"parameters\": [");
_builder.newLine();
_builder.append("\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t");
_builder.append("},");
_builder.newLine();
_builder.append("\t");
_builder.append("\"methods\": [");
_builder.newLine();
{
Set> _entrySet = this._xtendHelper.sorted(this._xtendHelper.safe(path.getOperationMap())).entrySet();
boolean _hasElements = false;
for(final Map.Entry entry : _entrySet) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", "\t");
}
_builder.append("\t");
String _generateMethod = this.generateMethod(path, entry.getKey(), entry.getValue());
_builder.append(_generateMethod, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("],");
_builder.newLine();
_builder.append("\t");
_builder.append("\"mediaTypes\" : [");
_builder.newLine();
_builder.append("\t");
_builder.append("]");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
public String generateMethod(final Path path, final HttpMethod httpMethod, final Operation operation) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append("\t");
_builder.append("\"objecttype\": \"Method\",");
_builder.newLine();
_builder.append("\t");
_builder.append("\"name\": \"");
String _escapeEcmaScript = StringEscapeUtils.escapeEcmaScript(operation.getOperationId());
_builder.append(_escapeEcmaScript, "\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\"anchorId\": \"");
String _htmlLink = this._anchors.htmlLink(operation);
_builder.append(_htmlLink, "\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\"type\": \"");
_builder.append(httpMethod, "\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t");
CharSequence _generateRequestWithTrailingComma = this.generateRequestWithTrailingComma(path, operation);
_builder.append(_generateRequestWithTrailingComma, "\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\"responses\": [");
_builder.newLine();
{
Set> _entrySet = this._xtendHelper.sortedWith(this._xtendHelper.safe(operation.getResponses()), SwaggerDiagramData.STATUS_CODE_COMPARATOR).entrySet();
boolean _hasElements = false;
for(final Map.Entry entry : _entrySet) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", "\t");
}
_builder.append("\t");
CharSequence _generateResponse = this.generateResponse(entry.getKey(), entry.getValue());
_builder.append(_generateResponse, "\t");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("]");
_builder.newLine();
_builder.append("}");
_builder.newLine();
return _builder.toString();
}
public CharSequence generateRequestWithTrailingComma(final Path path, final Operation operation) {
CharSequence _xblockexpression = null;
{
final HashMap name2param = new HashMap();
final Consumer _function = (Parameter it) -> {
name2param.put(it.getName(), it);
};
this._xtendHelper.safe(operation.getParameters()).forEach(_function);
final Consumer _function_1 = (Parameter it) -> {
name2param.put(it.getName(), it);
};
this._xtendHelper.safe(path.getParameters()).forEach(_function_1);
StringConcatenation _builder = new StringConcatenation();
_builder.append("\"request\": {");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"Request\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"resource_type\": \"");
String _requestType = this.getRequestType(operation);
_builder.append(_requestType, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"name\": \"(empty request)\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"parameters\": [");
_builder.newLine();
{
Collection _values = name2param.values();
boolean _hasElements = false;
for(final Parameter next : _values) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", " ");
}
_builder.append(" ");
CharSequence _generateRequestParameter = this.generateRequestParameter(next);
_builder.append(_generateRequestParameter, " ");
_builder.newLineIfNotEmpty();
}
}
_builder.append(" ");
_builder.append("]");
_builder.newLine();
_builder.append("},");
_builder.newLine();
_xblockexpression = _builder;
}
return _xblockexpression;
}
public CharSequence generateRequestParameter(final Parameter parameter) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"");
String _escapeEcmaScript = StringEscapeUtils.escapeEcmaScript(this.getParameterObjectType(parameter));
_builder.append(_escapeEcmaScript, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"name\": \"");
String _escapeEcmaScript_1 = StringEscapeUtils.escapeEcmaScript(this.getParameterLabel(parameter));
_builder.append(_escapeEcmaScript_1, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"isProperty\": false,");
_builder.newLine();
_builder.append(" ");
_builder.append("\"propertyId\": \"\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"required\": ");
boolean _required = parameter.getRequired();
_builder.append(_required, " ");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence generateResponseHeader(final String headerName, final Property header) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"HeaderParameter\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"name\": \"");
String _escapeEcmaScript = StringEscapeUtils.escapeEcmaScript(headerName);
_builder.append(_escapeEcmaScript, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"isProperty\": false,");
_builder.newLine();
_builder.append(" ");
_builder.append("\"propertyId\": \"\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"required\": ");
boolean _required = header.getRequired();
_builder.append(_required, " ");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence generateResponse(final String code, final Response response) {
CharSequence _xblockexpression = null;
{
final int intCode = StatusCodeComparator.safeParse(code, SwaggerDiagramData.DEFAULT_RESPONSE_CODE);
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"Response\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"resource_type\": \"");
String _responseType = this.getResponseType(response);
_builder.append(_responseType, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"statusCode\": \"");
String _responseStatusCode = SwaggerDiagramData.getResponseStatusCode(intCode);
_builder.append(_responseStatusCode, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"statusCodeGroup\": \"");
String _responseStatusCodeGroup = SwaggerDiagramData.getResponseStatusCodeGroup(intCode);
_builder.append(_responseStatusCodeGroup, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"parameters\": [");
_builder.newLine();
{
Set> _entrySet = this._xtendHelper.sorted(this._xtendHelper.safe(response.getHeaders())).entrySet();
boolean _hasElements = false;
for(final Map.Entry entry : _entrySet) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", " ");
}
_builder.append(" ");
CharSequence _generateResponseHeader = this.generateResponseHeader(entry.getKey(), entry.getValue());
_builder.append(_generateResponseHeader, " ");
_builder.newLineIfNotEmpty();
}
}
_builder.append(" ");
_builder.append("]");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_xblockexpression = _builder;
}
return _xblockexpression;
}
private static String getResponseStatusCode(final int statusCode) {
String _xblockexpression = null;
{
if ((statusCode == SwaggerDiagramData.DEFAULT_RESPONSE_CODE)) {
return "(default response)";
}
final String desc = MethodServices.getResponseStatusCodeDescription(statusCode);
String _xifexpression = null;
boolean _isEmpty = desc.isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
_xifexpression = (" - " + desc);
} else {
_xifexpression = "";
}
_xblockexpression = (Integer.valueOf(statusCode) + _xifexpression);
}
return _xblockexpression;
}
private static String getResponseStatusCodeGroup(final int code) {
String _switchResult = null;
boolean _matched = false;
if (Objects.equal(code, SwaggerDiagramData.DEFAULT_RESPONSE_CODE)) {
_matched=true;
_switchResult = "Default";
}
if (!_matched) {
if (((code >= 100) && (code < 200))) {
_matched=true;
_switchResult = "Informational";
}
}
if (!_matched) {
if (((code >= 200) && (code < 300))) {
_matched=true;
_switchResult = "Success";
}
}
if (!_matched) {
if (((code >= 300) && (code < 400))) {
_matched=true;
_switchResult = "Redirection";
}
}
if (!_matched) {
if (((code >= 400) && (code < 500))) {
_matched=true;
_switchResult = "Client Error";
}
}
if (!_matched) {
if (((code >= 500) && (code < 600))) {
_matched=true;
_switchResult = "Server Error";
}
}
if (!_matched) {
_switchResult = "";
}
return _switchResult;
}
public String getParameterObjectType(final Parameter parameter) {
String _switchResult = null;
String _elvis = null;
String _in = parameter.getIn();
if (_in != null) {
_elvis = _in;
} else {
_elvis = "";
}
if (_elvis != null) {
switch (_elvis) {
case "query":
return "QueryParameter";
case "header":
return "HeaderParameter";
case "path":
return "RequestParameter";
case "formData":
return "Request";
case "body":
return "Request";
default:
_switchResult = "Request";
break;
}
} else {
_switchResult = "Request";
}
return _switchResult;
}
public String getParameterLabel(final Parameter parameter) {
return this.typesProvider.parameterLabel(parameter);
}
public String getResponseType(final Response response) {
final Model schema = response.getResponseSchema();
return this.typesProvider.propertyTypeLabel("Response", schema);
}
public String getRequestType(final Operation operation) {
String _xblockexpression = null;
{
Iterable bodyParams = Iterables.filter(operation.getParameters(), BodyParameter.class);
boolean _isEmpty = IterableExtensions.isEmpty(bodyParams);
boolean _not = (!_isEmpty);
if (_not) {
return this.typesProvider.computeType(IterableExtensions.head(bodyParams));
}
Iterable formParams = Iterables.filter(operation.getParameters(), FormParameter.class);
boolean _isEmpty_1 = IterableExtensions.isEmpty(formParams);
boolean _not_1 = (!_isEmpty_1);
if (_not_1) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Form Data");
return _builder.toString();
}
StringConcatenation _builder_1 = new StringConcatenation();
_xblockexpression = _builder_1.toString();
}
return _xblockexpression;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy