com.reprezen.genflow.openapi.diagram.openapi3.OpenApi3DiagramData Maven / Gradle / Ivy
/**
* 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.openapi3;
import com.google.common.base.Objects;
import com.reprezen.genflow.common.services.MethodServices;
import com.reprezen.genflow.openapi.diagram.openapi3.OpenApi3Anchors;
import com.reprezen.genflow.openapi.diagram.openapi3.OpenApi3DiagramTypes;
import com.reprezen.genflow.openapi.diagram.swagger.StatusCodeComparator;
import com.reprezen.genflow.openapi.diagram.swagger.XtendHelper;
import com.reprezen.jsonoverlay.IModelPart;
import com.reprezen.kaizen.oasparser.model3.Header;
import com.reprezen.kaizen.oasparser.model3.Info;
import com.reprezen.kaizen.oasparser.model3.MediaType;
import com.reprezen.kaizen.oasparser.model3.OpenApi3;
import com.reprezen.kaizen.oasparser.model3.Operation;
import com.reprezen.kaizen.oasparser.model3.Parameter;
import com.reprezen.kaizen.oasparser.model3.Path;
import com.reprezen.kaizen.oasparser.model3.RequestBody;
import com.reprezen.kaizen.oasparser.model3.Response;
import com.reprezen.kaizen.oasparser.model3.Schema;
import java.util.Collection;
import java.util.LinkedHashMap;
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.CollectionLiterals;
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 OpenApi3DiagramData {
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 OpenApi3Anchors _openApi3Anchors = new OpenApi3Anchors();
private OpenApi3DiagramTypes typesProvider;
private OpenApi3 swagger;
public OpenApi3DiagramData(final OpenApi3 model) {
this.swagger = model;
OpenApi3DiagramTypes _openApi3DiagramTypes = new OpenApi3DiagramTypes();
this.typesProvider = _openApi3DiagramTypes;
}
public String generateDiagramData() {
final String result = this.generateJSON(this.swagger);
return result;
}
public String generateJSON(final OpenApi3 swagger) {
String _xblockexpression = null;
{
swagger.getServers();
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._openApi3Anchors.htmlLink(swagger);
_builder.append(_htmlLink, "\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();
_xblockexpression = _builder.toString();
}
return _xblockexpression;
}
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._openApi3Anchors.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.getOperations())).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 String 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._openApi3Anchors.htmlLink(operation);
_builder.append(_htmlLink, "\t");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\"type\": \"");
String _upperCase = httpMethod.toUpperCase();
_builder.append(_upperCase, "\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()), OpenApi3DiagramData.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 LinkedHashMap> values = CollectionLiterals.>newLinkedHashMap();
final Consumer _function = (String k) -> {
values.put(k, operation.getRequestBody().getContentMediaTypes().get(k).getSchema());
};
IterableExtensions.take(operation.getRequestBody().getContentMediaTypes().keySet(), 1).forEach(_function);
final Consumer _function_1 = (Parameter it) -> {
values.put(it.getName(), it);
};
this._xtendHelper.andAlso(this._xtendHelper.safe(path.getParameters()), this._xtendHelper.safe(operation.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 = values.values();
boolean _hasElements = false;
for(final IModelPart next : _values) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", " ");
}
{
if ((next instanceof Parameter)) {
_builder.append(" ");
CharSequence _generateRequestParameter = this.generateRequestParameter(((Parameter) next));
_builder.append(_generateRequestParameter, " ");
_builder.newLineIfNotEmpty();
} else {
_builder.append(" ");
CharSequence _generateRequestParameter_1 = this.generateRequestParameter(operation.getRequestBody(), ((Schema) next));
_builder.append(_generateRequestParameter_1, " ");
_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 _isRequired = parameter.isRequired();
_builder.append(_isRequired, " ");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence generateRequestParameter(final RequestBody request, final Schema schema) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("{");
_builder.newLine();
_builder.append(" ");
_builder.append("\"objecttype\": \"Request\",");
_builder.newLine();
_builder.append(" ");
_builder.append("\"name\": \"");
String _escapeEcmaScript = StringEscapeUtils.escapeEcmaScript(this.getSchemaLabel(schema));
_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 _isRequired = request.isRequired();
_builder.append(_isRequired, " ");
_builder.newLineIfNotEmpty();
_builder.append("}");
_builder.newLine();
return _builder;
}
public CharSequence generateResponseHeader(final String headerName, final Header 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 _isRequired = header.isRequired();
_builder.append(_isRequired, " ");
_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, OpenApi3DiagramData.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 = OpenApi3DiagramData.getResponseStatusCode(intCode);
_builder.append(_responseStatusCode, " ");
_builder.append("\",");
_builder.newLineIfNotEmpty();
_builder.append(" ");
_builder.append("\"statusCodeGroup\": \"");
String _responseStatusCodeGroup = OpenApi3DiagramData.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 == OpenApi3DiagramData.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, OpenApi3DiagramData.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 _elvis = null;
String _in = parameter.getIn();
String _lowerCase = null;
if (_in!=null) {
_lowerCase=_in.toLowerCase();
}
if (_lowerCase != null) {
_elvis = _lowerCase;
} else {
_elvis = "";
}
if (_elvis != null) {
switch (_elvis) {
case "query":
return "QueryParameter";
case "header":
return "HeaderParameter";
case "path":
return "RequestParameter";
}
}
return "RequestParameter";
}
public String getParameterLabel(final Parameter parameter) {
return this.typesProvider.parameterLabel(parameter);
}
public String getSchemaLabel(final Schema schema) {
return this.typesProvider.propertyTypeLabel("Request", schema);
}
public String getResponseType(final Response response) {
MediaType _head = IterableExtensions.head(response.getContentMediaTypes().values());
Schema _schema = null;
if (_head!=null) {
_schema=_head.getSchema();
}
final Schema schema = _schema;
return this.typesProvider.propertyTypeLabel("Response", schema);
}
public String getRequestType(final Operation operation) {
String _xblockexpression = null;
{
RequestBody _requestBody = operation.getRequestBody();
boolean _tripleNotEquals = (_requestBody != null);
if (_tripleNotEquals) {
return this.typesProvider.computeType(operation.getRequestBody());
}
StringConcatenation _builder = new StringConcatenation();
_xblockexpression = _builder.toString();
}
return _xblockexpression;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy