Added test cases for WSDL and OpenAPI, OpenAPI is currently failing

This commit is contained in:
Ziver Koc 2021-02-28 22:50:15 +01:00
parent 5622a3b489
commit 02e91a38e9
7 changed files with 293 additions and 40 deletions

View file

@ -181,16 +181,16 @@ public class WSMethodDef {
} else {
// Specific request type was not provided, try to figure it out by the method name
if (name.startsWith("get"))
this.requestType = WSInterface.RequestType.GET;
if (name.startsWith("post"))
this.requestType = WSInterface.RequestType.POST;
if (name.startsWith("put"))
else if (name.startsWith("put"))
this.requestType = WSInterface.RequestType.PUT;
if (name.startsWith("delete"))
else if (name.startsWith("delete"))
this.requestType = WSInterface.RequestType.DELETE;
if (name.startsWith("patch"))
else if (name.startsWith("patch"))
this.requestType = WSInterface.RequestType.PATCH;
else
this.requestType = WSInterface.RequestType.GET;
}
// Handle endpoint path
@ -201,8 +201,8 @@ public class WSMethodDef {
else
path = this.name;
if (path.startsWith("/"))
path = path.substring(1);
if (!path.startsWith("/"))
path = '/' + path;
}
/**

View file

@ -12,7 +12,9 @@ import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
/**
@ -58,12 +60,14 @@ public class OpenAPIWriter {
public String write() {
if (cache == null) {
Map<String, List<WSParameterDef>> schemas = new HashMap<>();
DataNode root = new DataNode(DataNode.DataType.Map);
root.set("openapi", OPENAPI_VERSION);
root.set("info", generateInfo());
root.set("servers", generateServers());
root.set("paths", generatePaths());
root.set("components", generateComponents());
root.set("paths", generatePaths(schemas));
root.set("components", generateComponents(schemas));
this.cache = JSONWriter.toString(root);
}
@ -74,12 +78,12 @@ public class OpenAPIWriter {
DataNode infoRoot = new DataNode(DataNode.DataType.Map);
infoRoot.set("title", ws.getName());
infoRoot.set("description", ws.getDocumentation());
infoRoot.set("version", "");
// Not implemented properties
// "termsOfService": xxx,
// "contact": {"name": xxx,"url": xxx,"email": xxx},
// "license": {"name": xxx, "url": xxx},
// "version": xxx
return infoRoot;
}
@ -87,30 +91,28 @@ public class OpenAPIWriter {
DataNode serversRoot = new DataNode(DataNode.DataType.List);
for (ServerData data : servers) {
DataNode serverNode = new DataNode(DataNode.DataType.Map);
DataNode serverNode = serversRoot.add(DataNode.DataType.Map);
serverNode.set("url", data.url);
serverNode.set("description", data.description);
serversRoot.add(serverNode);
}
return serversRoot;
}
private DataNode generatePaths() {
private DataNode generatePaths(Map<String, List<WSParameterDef>> schemas) {
DataNode pathsRoot = new DataNode(DataNode.DataType.Map);
for (WSMethodDef methodDef : ws.getMethods()) {
DataNode pathNode = new DataNode(DataNode.DataType.Map);
DataNode pathNode = pathsRoot.set(methodDef.getPath(), DataNode.DataType.Map);
DataNode typeNode = new DataNode(DataNode.DataType.Map);
DataNode typeNode = pathNode.set(methodDef.getRequestType().toString().toLowerCase(), DataNode.DataType.Map);
typeNode.set("description", methodDef.getDocumentation());
pathNode.set(methodDef.getRequestType().toString().toLowerCase(), typeNode);
// --------------------------------------------
// Inputs
// --------------------------------------------
DataNode parameterNode = new DataNode(DataNode.DataType.Map);
DataNode parameterNode = typeNode.set("parameters", DataNode.DataType.Map);
for (WSParameterDef parameterDef : methodDef.getInputs()) {
parameterNode.set("name", parameterDef.getName());
parameterNode.set("description", parameterDef.getDocumentation());
@ -119,29 +121,27 @@ public class OpenAPIWriter {
parameterNode.set("schema", "");
}
typeNode.set("parameters", parameterNode);
// --------------------------------------------
// Outputs
// --------------------------------------------
DataNode responseNode = new DataNode(DataNode.DataType.Map);
for (WSParameterDef parameterDef : methodDef.getOutputs()) {
parameterNode.set("name", parameterDef.getName());
parameterNode.set("description", parameterDef.getDocumentation());
parameterNode.set("in", "query");
parameterNode.set("required", parameterDef.isOptional());
DataNode responseNode = typeNode.set("responses", DataNode.DataType.Map);
DataNode schemaNode = responseNode.set("200", DataNode.DataType.Map)
.set("content", DataNode.DataType.Map)
.set("application/json", DataNode.DataType.Map)
.set("schema", DataNode.DataType.Map);
parameterNode.set("schema", "");
}
typeNode.set("responses", responseNode);
String retName = methodDef.getName() + "Return";
schemas.put("retName", methodDef.getOutputs());
schemaNode.set("$ref", "#/components/schemas/" + retName);
}
return pathsRoot;
}
private DataNode generateComponents() {
private DataNode generateComponents(Map<String, List<WSParameterDef>> schemas) {
DataNode componentsRoot = new DataNode(DataNode.DataType.Map);
DataNode schemasNode = new DataNode(DataNode.DataType.Map);
componentsRoot.set("schemas", schemasNode);

View file

@ -313,7 +313,9 @@ public class WSDLWriter {
empty.addAttribute("name", "empty");
empty.addElement("xsd:sequence");
for (Class<?> c : types) {
for (int i=0; i<types.size(); i++) {
Class<?> c = types.get(i);
// Generate Array type
if (c.isArray()) {
Class<?> ctmp = ClassUtil.getArrayClass(c);
@ -344,26 +346,26 @@ public class WSDLWriter {
Element sequence = type.addElement("xsd:sequence");
Field[] fields = c.getFields();
for (int i = 0; i < fields.length; i++) {
WSInterface.WSParamName tmp = fields[i].getAnnotation(WSInterface.WSParamName.class);
for (int j=0; j<fields.length; j++) {
WSInterface.WSParamName tmp = fields[j].getAnnotation(WSInterface.WSParamName.class);
String name;
if (tmp != null)
name = tmp.value();
else
name = "field" + i;
name = "field" + j;
Element element = sequence.addElement("xsd:element");
element.addAttribute("name", name);
// Check if the object is an SOAPObject
Class<?> cTmp = ClassUtil.getArrayClass(fields[i].getType());
Class<?> cTmp = ClassUtil.getArrayClass(fields[j].getType());
if (WSReturnObject.class.isAssignableFrom(cTmp)) {
element.addAttribute("type", "tns:" + SOAPHttpPage.getSOAPClassName(cTmp));
if (!types.contains(cTmp))
types.add(cTmp);
} else {
element.addAttribute("type", "xsd:" + SOAPHttpPage.getSOAPClassName(fields[i].getType()));
element.addAttribute("type", "xsd:" + SOAPHttpPage.getSOAPClassName(fields[j].getType()));
}
// Is the Field optional

View file

@ -182,7 +182,19 @@ public class DataNode implements Iterable<DataNode> {
}
/**
* Adds a node to the Map
* Creates a new node and adds it to the list.
*
* @param type the type of the new DataNode
* @return a newly created DataNode
*/
public DataNode add(DataType type) {
DataNode newNode = new DataNode(type);
list.add(newNode);
return newNode;
}
/**
* Adds a node to the Map with the provided value
*/
public void set(String key, DataNode node) {
map.put(key, node);
@ -208,6 +220,20 @@ public class DataNode implements Iterable<DataNode> {
map.put(key, new DataNode(value));
}
/**
* Creates a new node and adds it to the specified key.
*
* @param key the key where the new DataNode should be assigned
* @param type the type of the new DataNode
* @return a newly created DataNode
*/
public DataNode set(String key, DataType type) {
DataNode newNode = new DataNode(type);
map.put(key, newNode);
return newNode;
}
/**
* Sets the value of the node
*