Skip to content

Commit

Permalink
Merge pull request #1 from SPIRIT-21/develop
Browse files Browse the repository at this point in the history
fixed a bug where classes in the same package were not found
  • Loading branch information
danielsimon1 authored Mar 2, 2017
2 parents f26af56 + 4295feb commit 1dda19d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.spirit21</groupId>
<artifactId>javadoc2swagger-maven-plugin</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>maven-plugin</packaging>

<name>com.spirit21:javadoc2swagger-maven-plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private List<JavaFile> getInformationFromJavaFiles(List<Path> files) throws IOEx
JavaFile javaFile = new JavaFile();
String packageName = getPackageNameFromFile(fileString);
List<String> imports = importLoader.getImportsFromFile(fileString);
imports.add(packageName);
List<Method> methods = methodLoader.getMethodsFromJavaFile(fileString);
String apiJavadoc = apiJavadocLoader.getApiJavadocFromJavaFile(fileString);
List<String> classAnnotations = classAnnotationLoader.getClassAnnotationsFromJavaFile(fileString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> imports, String fileName)
public String createDefinitionIfNotExists(String input, List<String> imports, String fileName, String packageName)
throws ParserException {
String className;
if (input.startsWith("{")) {
Expand All @@ -69,7 +71,7 @@ public String createDefinitionIfNotExists(String input, List<String> 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;
}
Expand All @@ -88,33 +90,45 @@ public String createDefinitionIfNotExists(String input, List<String> 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<String> 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<Property> 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<Property> properties = processFields(fields, definition, rootDefinition);
definition.setProperties(properties);
return definition;
}

/**
Expand Down Expand Up @@ -247,7 +261,7 @@ public void createDefinitionBySchemaAndPackageIfNotExists(String schema, String
if (definition == null) {
List<String> imports = new ArrayList<>();
imports.add(packageName);
definitions.add(createDefinitionByClassName(className, imports, "", rootDefinition));
definitions.add(createDefinitionByClassName(className, imports, "", rootDefinition, ""));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ public List<Operation> findOperationsInJavaFile(JavaFile javaFile, String path,
operation.setTags(tagParser.generateTags(path));
operation.setOperationId(met.getHttpMethod() + path.replace("/", ""));
List<com.spirit21.swagger.converter.models.Parameter> 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<Response> responses = responseParser.findResponsesInJavadocSection(section, imports,
javaFile.getFileName());
javaFile.getFileName(), javaFile.getPackageName());
if (!responses.isEmpty()) {
operation.setResponses(responses);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public ParameterParser(Log log, ClassLoader loader, List<Tag> tags, List<Definit
* javadoc section
* @param fileName
* java file name
* @param packageName
* package name of current java file
* @return parameters
* @throws ParserException
* Error while the parsing process
*/
public List<Parameter> findParametersInMethodHeader(String header, List<String> imports, String javadoc,
String fileName) throws ParserException {
String fileName, String packageName) throws ParserException {
descriptionMap = getParameterDescriptionMap(javadoc);
String regex = "\\((?s:.)*\\)";
Pattern pattern = Pattern.compile(regex);
Expand All @@ -56,7 +58,7 @@ public List<Parameter> findParametersInMethodHeader(String header, List<String>
List<Parameter> 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);
}
Expand Down Expand Up @@ -86,7 +88,7 @@ public List<Parameter> findParametersInMethodHeader(String header, List<String>
* @throws ParserException
* Error while the parsing process
*/
private Parameter getParameter(String paramUnformatted, List<String> imports, String fileName)
private Parameter getParameter(String paramUnformatted, List<String> imports, String fileName, String packageName)
throws ParserException {
DataTypeFactory typeHandler = new DataTypeFactory();
DefinitionParser definitionParser = new DefinitionParser(log, loader, tags, definitions);
Expand Down Expand Up @@ -118,7 +120,7 @@ private Parameter getParameter(String paramUnformatted, List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ public ResponseParser(Log log, ClassLoader loader, List<Tag> tags, List<Definiti
* imports of a java file
* @param fileName
* java file name
* @param packageName
* package name of current java file
* @return List of {@link Resource}
* @throws ParserException
* Error while the parsing process
*/
public List<Response> findResponsesInJavadocSection(String section, List<String> imports, String fileName)
throws ParserException {
public List<Response> findResponsesInJavadocSection(String section, List<String> imports, String fileName,
String packageName) throws ParserException {
List<Response> 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;
}
Expand All @@ -64,13 +66,13 @@ public List<Response> findResponsesInJavadocSection(String section, List<String>
* @throws ParserException
* Error while the parsing process
*/
private Response generateResponseFromSection(String section, List<String> imports, String fileName)
throws ParserException {
private Response generateResponseFromSection(String section, List<String> 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);
}
Expand Down

0 comments on commit 1dda19d

Please sign in to comment.