diff --git a/pom.xml b/pom.xml index 7730e3c..bedef2a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.spirit21 javadoc2swagger-maven-plugin - 1.0 + 1.1 maven-plugin com.spirit21:javadoc2swagger-maven-plugin diff --git a/src/main/java/com/spirit21/swagger/converter/loader/JavaFileLoader.java b/src/main/java/com/spirit21/swagger/converter/loader/JavaFileLoader.java index bb52822..eb8d9f2 100644 --- a/src/main/java/com/spirit21/swagger/converter/loader/JavaFileLoader.java +++ b/src/main/java/com/spirit21/swagger/converter/loader/JavaFileLoader.java @@ -104,6 +104,7 @@ private List getInformationFromJavaFiles(List files) throws IOEx JavaFile javaFile = new JavaFile(); String packageName = getPackageNameFromFile(fileString); List imports = importLoader.getImportsFromFile(fileString); + imports.add(packageName); List methods = methodLoader.getMethodsFromJavaFile(fileString); String apiJavadoc = apiJavadocLoader.getApiJavadocFromJavaFile(fileString); List classAnnotations = classAnnotationLoader.getClassAnnotationsFromJavaFile(fileString); diff --git a/src/main/java/com/spirit21/swagger/converter/parsers/DefinitionParser.java b/src/main/java/com/spirit21/swagger/converter/parsers/DefinitionParser.java index ca5e713..da2f9ee 100644 --- a/src/main/java/com/spirit21/swagger/converter/parsers/DefinitionParser.java +++ b/src/main/java/com/spirit21/swagger/converter/parsers/DefinitionParser.java @@ -53,11 +53,13 @@ public Definition getDefinitionByClassName(String className) { * imports of the java file * @param fileName * file name + * @param packageName + * package name of current java file * @return class name * @throws ParserException * Error while the parsing process */ - public String createDefinitionIfNotExists(String input, List imports, String fileName) + public String createDefinitionIfNotExists(String input, List imports, String fileName, String packageName) throws ParserException { String className; if (input.startsWith("{")) { @@ -69,7 +71,7 @@ public String createDefinitionIfNotExists(String input, List imports, St } Definition definition = getDefinitionByClassName(className); if (definition == null) { - definitions.add(createDefinitionByClassName(className, imports, fileName, null)); + definitions.add(createDefinitionByClassName(className, imports, fileName, null, packageName)); } return className; } @@ -88,33 +90,45 @@ public String createDefinitionIfNotExists(String input, List imports, St * @param rootDefinition * definition where the recursion started to prevent an endless * loop + * @param filePackageName + * package name of current java file * @return new {@link Definition} object * @throws ParserException * Error while the parsing process */ public Definition createDefinitionByClassName(String className, List imports, String fileName, - Definition rootDefinition) throws ParserException { + Definition rootDefinition, String filePackageName) throws ParserException { + log.info("new import sequence"); for (String imp : imports) { + log.info("import: " + imp); String regex = ".*[.]" + className; if (imp.matches(regex)) { String classWithPackage = imp; try { - Class cls = loader.loadClass(classWithPackage); - Field[] fields = cls.getDeclaredFields(); - Definition definition = new Definition(); - definition.setClassName(className); - if (rootDefinition == null) { - rootDefinition = definition; - } - List properties = processFields(fields, definition, rootDefinition); - definition.setProperties(properties); - return definition; + return getDefinitionByClass(loader.loadClass(classWithPackage), className, rootDefinition); } catch (Exception e) { throw new ParserException("Error loading class '" + classWithPackage + "'", e); } } } - throw new ParserException("Class not found in imports: " + className + "; file: " + fileName); + try { + return getDefinitionByClass(loader.loadClass(filePackageName + "." + className), className, rootDefinition); + } catch (Exception e) { + throw new ParserException("Class not found in imports: " + className + "; file: " + fileName); + } + } + + private Definition getDefinitionByClass(Class cls, String className, Definition rootDefinition) + throws ParserException { + Field[] fields = cls.getDeclaredFields(); + Definition definition = new Definition(); + definition.setClassName(className); + if (rootDefinition == null) { + rootDefinition = definition; + } + List properties = processFields(fields, definition, rootDefinition); + definition.setProperties(properties); + return definition; } /** @@ -247,7 +261,7 @@ public void createDefinitionBySchemaAndPackageIfNotExists(String schema, String if (definition == null) { List imports = new ArrayList<>(); imports.add(packageName); - definitions.add(createDefinitionByClassName(className, imports, "", rootDefinition)); + definitions.add(createDefinitionByClassName(className, imports, "", rootDefinition, "")); } } } diff --git a/src/main/java/com/spirit21/swagger/converter/parsers/OperationParser.java b/src/main/java/com/spirit21/swagger/converter/parsers/OperationParser.java index 4a5a27f..267a405 100644 --- a/src/main/java/com/spirit21/swagger/converter/parsers/OperationParser.java +++ b/src/main/java/com/spirit21/swagger/converter/parsers/OperationParser.java @@ -72,12 +72,13 @@ public List findOperationsInJavaFile(JavaFile javaFile, String path, operation.setTags(tagParser.generateTags(path)); operation.setOperationId(met.getHttpMethod() + path.replace("/", "")); List parameters = parameterParser - .findParametersInMethodHeader(met.getHeader(), imports, met.getJavadoc(), javaFile.getFileName()); + .findParametersInMethodHeader(met.getHeader(), imports, met.getJavadoc(), javaFile.getFileName(), + javaFile.getPackageName()); if (parameters != null && !parameters.isEmpty()) { operation.setParameters(parameters); } List responses = responseParser.findResponsesInJavadocSection(section, imports, - javaFile.getFileName()); + javaFile.getFileName(), javaFile.getPackageName()); if (!responses.isEmpty()) { operation.setResponses(responses); } else { diff --git a/src/main/java/com/spirit21/swagger/converter/parsers/ParameterParser.java b/src/main/java/com/spirit21/swagger/converter/parsers/ParameterParser.java index b8b4a1c..c06228c 100644 --- a/src/main/java/com/spirit21/swagger/converter/parsers/ParameterParser.java +++ b/src/main/java/com/spirit21/swagger/converter/parsers/ParameterParser.java @@ -39,12 +39,14 @@ public ParameterParser(Log log, ClassLoader loader, List tags, List findParametersInMethodHeader(String header, List imports, String javadoc, - String fileName) throws ParserException { + String fileName, String packageName) throws ParserException { descriptionMap = getParameterDescriptionMap(javadoc); String regex = "\\((?s:.)*\\)"; Pattern pattern = Pattern.compile(regex); @@ -56,7 +58,7 @@ public List findParametersInMethodHeader(String header, List List retParameters = new ArrayList<>(); for (String param : parameters) { if (!param.contains("/* @swagger:ignore */")) { - Parameter parameter = getParameter(param, imports, fileName); + Parameter parameter = getParameter(param, imports, fileName, packageName); if (parameter != null) { retParameters.add(parameter); } @@ -86,7 +88,7 @@ public List findParametersInMethodHeader(String header, List * @throws ParserException * Error while the parsing process */ - private Parameter getParameter(String paramUnformatted, List imports, String fileName) + private Parameter getParameter(String paramUnformatted, List imports, String fileName, String packageName) throws ParserException { DataTypeFactory typeHandler = new DataTypeFactory(); DefinitionParser definitionParser = new DefinitionParser(log, loader, tags, definitions); @@ -118,7 +120,7 @@ private Parameter getParameter(String paramUnformatted, List imports, St if (type.startsWith("#") && !isQueryParam) { // reference -> body parameter parameter.setLocation("body"); - String title = definitionParser.createDefinitionIfNotExists(type, imports, fileName); + String title = definitionParser.createDefinitionIfNotExists(type, imports, fileName, packageName); if (title != null) { Definition definition = definitionParser.getDefinitionByClassName(title); parameter.setDefinition(definition); diff --git a/src/main/java/com/spirit21/swagger/converter/parsers/ResponseParser.java b/src/main/java/com/spirit21/swagger/converter/parsers/ResponseParser.java index 4792013..56d7863 100644 --- a/src/main/java/com/spirit21/swagger/converter/parsers/ResponseParser.java +++ b/src/main/java/com/spirit21/swagger/converter/parsers/ResponseParser.java @@ -33,19 +33,21 @@ public ResponseParser(Log log, ClassLoader loader, List tags, List findResponsesInJavadocSection(String section, List imports, String fileName) - throws ParserException { + public List findResponsesInJavadocSection(String section, List imports, String fileName, + String packageName) throws ParserException { List responses = new ArrayList<>(); String regex = "@responseCode [0-9]{1,3}([\\s]|@responseSchema \\{@link \\w+\\}|@responseType [a-zA-Z]+|@responseMessage [^@]+)*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(section); while (matcher.find()) { String responseSection = section.substring(matcher.start(), matcher.end()); - responses.add(generateResponseFromSection(responseSection, imports, fileName)); + responses.add(generateResponseFromSection(responseSection, imports, fileName, packageName)); } return responses; } @@ -64,13 +66,13 @@ public List findResponsesInJavadocSection(String section, List * @throws ParserException * Error while the parsing process */ - private Response generateResponseFromSection(String section, List imports, String fileName) - throws ParserException { + private Response generateResponseFromSection(String section, List imports, String fileName, + String packageName) throws ParserException { DefinitionParser definitionParser = new DefinitionParser(log, loader, tags, definitions); Response response = new Response(); String schema = findStringInSectionByRegex("@responseSchema \\{@link \\w+\\}", 16, section); if (schema != null) { - String title = definitionParser.createDefinitionIfNotExists(schema, imports, fileName); + String title = definitionParser.createDefinitionIfNotExists(schema, imports, fileName, packageName); Definition definition = definitionParser.getDefinitionByClassName(title); response.setDefinition(definition); }