package com.perforce.hws.swagger.asciidoc.models;
import com.google.common.base.CaseFormat;
import io.swagger.models.HttpMethod;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Response;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.RefProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* The API is essentially a list of routes.
* <p>
* Each route can be represented in different method calls in APIs.
*/
public class Route implements LinkHelpers {
private String method;
private String subPath;
private Operation operation;
private Path path;
private List<ParamHelper> paramHelpers;
public Route(HttpMethod method, Operation operation, Path path, String subPath) {
this.method = method.name().toLowerCase();
this.operation = operation;
this.path = path;
this.subPath = subPath;
if (operation.getParameters() != null) {
this.paramHelpers = operation.getParameters().stream()
.map(p -> new ParamHelper(p))
.collect(Collectors.toList());
}
}
public boolean isAlpha() {
return operation.getTags() != null && operation.getTags().contains("alpha");
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Operation getOperation() {
return operation;
}
public void setOperation(Operation operation) {
this.operation = operation;
}
public List<ParamHelper> getParamHelpers() {
return paramHelpers;
}
public void setParamHelpers(List<ParamHelper> paramHelpers) {
this.paramHelpers = paramHelpers;
}
public List<ParamHelper> getRubyRequiredParamHelpers() {
return getParamHelpers().stream()
.filter(p -> !p.isQueryParam())
.collect(Collectors.toList());
}
public List<ParamHelper> getPythonRequiredParamHelpers() {
return getRubyRequiredParamHelpers();
}
public List<ParamHelper> getPathParamHelpers() {
return paramHelpers.stream()
.filter(ParamHelper::isPathParam)
.collect(Collectors.toList());
}
public List<ParamHelper> getQueryParamHelpers() {
return paramHelpers.stream()
.filter(ParamHelper::isQueryParam)
.collect(Collectors.toList());
}
public ParamHelper getBodyParamHelper() {
return paramHelpers.stream()
.filter(ParamHelper::isBodyParam)
.findFirst()
.orElse(null);
}
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public String getSubPath() {
return subPath;
}
public void setSubPath(String subPath) {
this.subPath = subPath;
}
public boolean isGetMethod() {
return method.toLowerCase().equals("get");
}
public boolean isPutMethod() {
return method.toLowerCase().equals("put");
}
public boolean isPostMethod() {
return method.toLowerCase().equals("post");
}
public boolean isDeleteMethod() {
return method.toLowerCase().equals("delete");
}
public boolean isPatchMethod() {
return method.toLowerCase().equals("patch");
}
public String getJavaClassShortName() {
if (isAlpha()) {
return "AlphaApi";
} else {
return "DefaultApi";
}
}
public String getJavascriptClassShortName() {
return getJavaClassShortName();
}
public String getJavaClassReferenceId() {
return CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN).convert(getJavaClassShortName());
}
public String getJavascriptClassReferenceId() {
return getJavaClassReferenceId();
}
public String getRubyClassReferenceId() {
return getJavaClassReferenceId();
}
public String getRubyClassShortName() {
if (isAlpha()) {
return "AlphaApi";
} else {
return "DefaultApi";
}
}
public String getRubyClassName() {
if (isAlpha()) {
return "HelixWebServices::AlphaApi";
} else {
return "HelixWebServices::DefaultApi";
}
}
public String getPhpClassName() {
if (isAlpha()) {
return "HelixWebServices\\Api\\AlphaApi";
} else {
return "HelixWebServices\\Api\\DefaultApi";
}
}
public String getPhpClassShortName() {
if (isAlpha()) {
return "AlphaApi";
} else {
return "DefaultApi";
}
}
public String getPhpClassReferenceId() {
return getJavaClassReferenceId();
}
public String getPythonClassShortName() {
if (isAlpha()) {
return "AlphaApi";
} else {
return "DefaultApi";
}
}
public String getPythonClassName() {
if (isAlpha()) {
return "helix_web_services_client.apis.AlphaApi";
} else {
return "helix_web_services_client.apis.DefaultApi";
}
}
public String getPythonClassReferenceId() {
return getJavaClassReferenceId();
}
public String getJavaMethodName() {
List<String> words = getSubpathWordComponents();
words.add(method);
List<String> bits = new ArrayList<>();
bits.add(words.get(0));
words.remove(0);
bits.addAll(words.stream().map(WordUtils::capitalize).collect(Collectors.toList()));
return bits.stream().collect(Collectors.joining());
}
public String getJavaMethodReferenceId() {
return CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN).convert(getJavaMethodName());
}
public String getJavascriptMethodReferenceId() {
return getJavaMethodReferenceId();
}
public String getJavascriptMethodName() {
return getJavaMethodName();
}
public String getRubyMethodReferenceId() {
List<String> words = getSubpathWordComponents();
words.add(method);
return words.stream()
.map(s -> s.toLowerCase())
.collect(Collectors.joining("-"));
}
public String getRubyMethodName() {
List<String> words = getSubpathWordComponents();
words.add(method);
return words.stream()
.map(s -> s.toLowerCase())
.collect(Collectors.joining("_"));
}
public String getRubyParamSignatureList() {
// TODO: figure out a better model mechanism to generate this in the StringTemplate layer.
StringBuilder sb = new StringBuilder();
List<String> params = getParamHelpers().stream()
.filter(p -> !p.isQueryParam())
.map(p -> p.getName())
.collect(Collectors.toCollection(() -> new ArrayList<>()));
if (getParamHelpers().stream().anyMatch(p -> p.isQueryParam())) {
params.add("opts");
}
return params.stream().collect(Collectors.joining(", "));
}
public String getPhpMethodName() {
return getJavaMethodName();
}
public String getPhpMethodReferenceId() {
return getJavaMethodReferenceId();
}
public String getPythonMethodReferenceId() {
return getJavaMethodReferenceId();
}
public String getPythonMethodName() {
return getRubyMethodName();
}
public String getPythonParamSignatureList() {
StringBuilder sb = new StringBuilder();
List<String> params = getParamHelpers().stream()
.filter(p -> !p.isQueryParam())
.map(p -> p.getName())
.collect(Collectors.toCollection(() -> new ArrayList<>()));
if (getParamHelpers().stream().anyMatch(p -> p.isQueryParam())) {
params.add("**kwargs");
}
return params.stream().collect(Collectors.joining(", "));
}
public List<String> getSubpathWordComponents() {
List<String> list = new ArrayList<>();
Arrays.stream(subPath.split("/"))
.filter(s -> !s.isEmpty())
.map(s -> StringUtils.strip(s, "{}"))
.collect(Collectors.toList())
.forEach(s -> {
Arrays.stream(s.split("-"))
.forEach(t -> list.add(t));
});
return list;
}
public String getSuccessResponseJavaType() {
String responseType = getSuccessResponseType();
if (responseType.startsWith("Array(")) {
String itemType = StringUtils.strip(responseType.replace("Array(", ""), ")");
return "List<" + itemType + ">";
}
return responseType;
}
public String getSuccessResponseJavaTypeRef() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return javaModelRef(refProperty.getSimpleRef());
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "List< " + javaModelRef(refProperty.getSimpleRef()) + " >";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
throw new IllegalStateException("Can't find success response for " + operation);
}
public String getSuccessResponseJavascriptType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
} else if (response.getSchema() instanceof ArrayProperty) {
return "Array";
}
}
throw new IllegalStateException("Unknown Javascript type reference");
}
public String getSuccessResponseJavascriptTypeRef() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return javascriptModelRef(refProperty.getSimpleRef());
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "Array of " + javascriptModelRef(refProperty.getSimpleRef());
}
}
throw new IllegalStateException("Unknown Javascript type reference");
}
public String getSuccessResponseRubyType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
} else if (response.getSchema() instanceof ArrayProperty) {
return "Array";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponseRubyTypeRef() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return rubyModelRef(refProperty.getSimpleRef());
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "Array of " + rubyModelRef(refProperty.getSimpleRef());
}
}
throw new IllegalStateException("Unknown Ruby type reference");
}
public String getSuccessResponsePhpType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
} else if (response.getSchema() instanceof ArrayProperty) {
return "array";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponsePhpTypeRef() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return phpModelRef(refProperty.getSimpleRef());
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "array of " + phpModelRef(refProperty.getSimpleRef());
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponsePythonType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
} else if (response.getSchema() instanceof ArrayProperty) {
return "list";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponsePythonTypeRef() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return pythonModelRef(refProperty.getSimpleRef());
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "list[" + pythonModelRef(refProperty.getSimpleRef()) + "]";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponseType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
} else if (response.getSchema() instanceof ArrayProperty) {
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return "Array(" + refProperty.getSimpleRef() + ")";
}
throw new IllegalStateException("Don't know how to convert response type:" + response);
}
return "void";
}
public String getSuccessResponseDescription() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
return response.getDescription();
}
return "";
}
public boolean hasSuccessData() {
return operation.getResponses().containsKey("200");
}
public boolean isSuccessArrayType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
return response.getSchema() instanceof ArrayProperty;
}
return false;
}
public String getSuccessJsSimpleRefType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
if (response.getSchema() instanceof RefProperty) {
RefProperty refProperty = (RefProperty) response.getSchema();
if (refProperty.getSimpleRef() != null) {
return refProperty.getSimpleRef();
}
}
}
throw new IllegalStateException("Invalid call to get simple ref type");
}
public String getSuccessJsArrayItemType() {
if (operation.getResponses().containsKey("200")) {
Response response = operation.getResponses().get("200");
ArrayProperty arrayProperty = (ArrayProperty) response.getSchema();
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
return refProperty.getSimpleRef();
}
throw new IllegalStateException("Invalid call to get item type on array");
}
}
# |
Change |
User |
Description |
Committed |
|
#1
|
20014 |
Paul Allen |
Moved swagger gneration inside project. |
|
|
//guest/paul_allen/hws/dev/swagger/src/main/java/com/perforce/hws/swagger/asciidoc/models/Route.java |
#1
|
19966 |
Paul Allen |
Refactor... |
|
|