Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Nov 19, 2017
1 parent 2a293a9 commit 962ecd4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void recordMethodComment(Javadoc javadoc, MethodDeclaration method) {

private List<MemberParameterTag> createMethodParameterTags(Javadoc javadoc, MethodDeclaration method) {
return javadoc.getBlockTags().stream()
.filter(t -> t.getType() == JavadocBlockTag.Type.PARAM)
.filter(t -> t.getType() == JavadocBlockTag.Type.PARAM || t.getTagName().equalsIgnoreCase("status"))
.map(t -> createMethodParameterTag(t, method))
.collect(Collectors.toList());
}
Expand All @@ -120,12 +120,17 @@ private MemberParameterTag createMethodParameterTag(JavadocBlockTag tag, MethodD
.map(NodeList::stream)
.orElseGet(Stream::empty);

return createMemberParamTag(tag.getContent(), annotations);
return createMemberParamTag(tag, annotations);
}

private MemberParameterTag createMemberParamTag(JavadocDescription description, Stream<AnnotationExpr> annotationStream) {
private MemberParameterTag createMemberParamTag(JavadocDescription javadocDescription, Stream<AnnotationExpr> annotationStream) {
Map<String, String> annotations = annotationStream.collect(Collectors.toMap(a -> a.getName().getIdentifier(), a -> a.asSingleMemberAnnotationExpr().getMemberValue().asStringLiteralExpr().asString()));
return new MemberParameterTag(description.toText(), annotations);
return new MemberParameterTag(javadocDescription.toText(),"", annotations);
}

private MemberParameterTag createMemberParamTag(JavadocBlockTag tag, Stream<AnnotationExpr> annotationStream) {
Map<String, String> annotations = annotationStream.collect(Collectors.toMap(a -> a.getName().getIdentifier(), a -> a.asSingleMemberAnnotationExpr().getMemberValue().asStringLiteralExpr().asString()));
return new MemberParameterTag(tag.getContent().toText(),tag.getTagName(), annotations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import com.sebastian_daschner.jaxrs_analyzer.model.results.ClassResult;
import com.sebastian_daschner.jaxrs_analyzer.model.results.MethodResult;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.sebastian_daschner.jaxrs_analyzer.analysis.results.JavaDocParameterResolver.*;

Expand Down Expand Up @@ -120,6 +122,9 @@ private ResourceMethod interpretResourceMethod(final MethodResult methodResult,

methodResult.getResponses().forEach(r -> interpretResponse(r, resourceMethod));

addJavadocResponses(methodResult, resourceMethod);


addMediaTypes(methodResult, classResult, resourceMethod);

if (methodResult.isDeprecated() || classResult.isDeprecated() || hasDeprecationTag(methodDoc))
Expand All @@ -128,6 +133,47 @@ private ResourceMethod interpretResourceMethod(final MethodResult methodResult,
return resourceMethod;
}

/**
* Reads @status javadoc annotation and creates Response status codes based on them.
*
* @param methodResult
* @param resourceMethod
*/
private void addJavadocResponses(MethodResult methodResult, ResourceMethod resourceMethod) {


methodResult.getMethodDoc().getParamTags();

List<MemberParameterTag> statusTag = methodResult.getMethodDoc().getParamTags().stream()
.filter(t -> t.getTagName().equalsIgnoreCase("status"))
.collect(Collectors.toList());

//Class level Javadoc tags NOT working cause below containing class coments are empty
methodResult.getMethodDoc().getContainingClassComment().getFieldComments().stream()
.filter(t -> t.getTagName().equalsIgnoreCase("status"))
.forEach(statusTag::add);

statusTag.stream()
.forEach(t -> resourceMethod.getResponses().putIfAbsent(toStatusCode(t),toResponse(t)));
}

private Integer toStatusCode(MemberParameterTag t) {
try {
return Integer.parseInt(t.getComment().trim().substring(0,3));
}catch (NumberFormatException nfe){
return null;
}

}

private Response toResponse(MemberParameterTag memberParameterTag) {
if(memberParameterTag.getComment() != null && memberParameterTag.getComment().trim().length() > 3) {
return new Response(null,memberParameterTag.getComment().trim().substring(3).trim());
} else {
return null;
}
}

private boolean hasDeprecationTag(MethodComment doc) {
if (doc == null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private JsonObjectBuilder buildResponses(final ResourceMethod method) {
e.getValue().getHeaders().stream().sorted().forEach(h -> headers.add(h, Json.createObjectBuilder().add("type", "string")));

final JsonObjectBuilder response = Json.createObjectBuilder()
.add("description", Optional.ofNullable(Response.Status.fromStatusCode(e.getKey())).map(Response.Status::getReasonPhrase).orElse(""))
.add("description", e.getValue().getDescription() != null ? e.getValue().getDescription() : Optional.ofNullable(Response.Status.fromStatusCode(e.getKey())).map(Response.Status::getReasonPhrase).orElse(""))
.add("headers", headers);

if (e.getValue().getResponseBody() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ClassComment extends MemberComment {
private List<MemberParameterTag> fieldComments = new ArrayList<>();

public ClassComment(String comment, boolean deprecated) {
super(comment, deprecated);
super(comment, deprecated);
}

public List<MemberParameterTag> getFieldComments() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@
public class MemberParameterTag {

private final String comment;
private final String tagName;

/**
* The annotations with their type and {@code values()};
*/
private final Map<String, String> annotations;

public MemberParameterTag(String comment, Map<String, String> annotations) {
public MemberParameterTag(String comment, String tagName, Map<String, String> annotations) {
this.comment = comment;
this.tagName = tagName;
this.annotations = Collections.unmodifiableMap(annotations);
}

public String getComment() {
return comment;
}

public String getTagName() {
return tagName;
}

public Map<String, String> getAnnotations() {
return annotations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ public class Response {

private final Set<String> headers = new HashSet<>();
private final TypeIdentifier responseBody;
private final String description;

public Response() {
this(null);
this(null,null);
}

public Response(final TypeIdentifier responseBody) {
this.responseBody = responseBody;
this.description = null;
}

public Response(TypeIdentifier responseBody, String description) {
this.responseBody = responseBody;
this.description = description;
}

public Set<String> getHeaders() {
Expand All @@ -45,6 +52,10 @@ public TypeIdentifier getResponseBody() {
return responseBody;
}

public String getDescription() {
return description;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down

0 comments on commit 962ecd4

Please sign in to comment.