-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from leandreck/9-feature/path-params
[RFR] 9-feature/path-params
- Loading branch information
Showing
15 changed files
with
622 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
|
||
import org.leandreck.endpoints.annotations.TypeScriptIgnore; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.Modifier; | ||
|
@@ -27,7 +29,9 @@ | |
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.Optional; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
/** | ||
* Created by Mathias Kowalzik ([email protected]) on 28.08.2016. | ||
|
@@ -51,21 +55,41 @@ public MethodNode createMethodNode(final ExecutableElement methodElement) { | |
return new MethodNode(name, "", true, null, null); | ||
} | ||
final String url = defineUrl(requestMapping); | ||
|
||
final List<String> httpMethods = defineHttpMethods(requestMapping); | ||
final TypeNode returnType = defineReturnType(methodElement); | ||
|
||
final List<? extends VariableElement> parameters = methodElement.getParameters(); | ||
final TypeNode requestBodyType = defineRequestBodyType(parameters); | ||
final List<TypeNode> pathVariables = definePathVariableTypes(parameters); | ||
|
||
return new MethodNode(name, url, false, httpMethods, returnType, requestBodyType, pathVariables); | ||
} | ||
|
||
private TypeNode defineReturnType(final ExecutableElement methodElement) { | ||
final TypeMirror returnMirror = methodElement.getReturnType(); | ||
final TypeNode returnType = typeNodeFactory.createTypeNode(returnMirror); | ||
return typeNodeFactory.createTypeNode(returnMirror); | ||
} | ||
|
||
private List<TypeNode> definePathVariableTypes(final List<? extends VariableElement> parameters) { | ||
return parameters.stream() | ||
.filter(p -> p.getAnnotation(PathVariable.class) != null) | ||
.map(p -> typeNodeFactory.createTypeNode(p)) | ||
.collect(toList()); | ||
} | ||
|
||
final TypeNode paramType; | ||
if (methodElement.getParameters().isEmpty()) { | ||
paramType = null; | ||
private TypeNode defineRequestBodyType(final List<? extends VariableElement> parameters) { | ||
final Optional<? extends VariableElement> optional = parameters.stream() | ||
.filter(p -> p.getAnnotation(RequestBody.class) != null) | ||
.findFirst(); | ||
final TypeNode requestBodyType; | ||
if (optional.isPresent()) { | ||
final VariableElement paramElement = optional.get(); | ||
requestBodyType = typeNodeFactory.createTypeNode(paramElement); | ||
} else { | ||
final VariableElement paramElement = methodElement.getParameters().get(0); | ||
paramType = typeNodeFactory.createTypeNode(paramElement); | ||
requestBodyType = null; | ||
} | ||
|
||
return new MethodNode(name, url, false, httpMethods, returnType, paramType); | ||
return requestBodyType; | ||
} | ||
|
||
private static List<String> defineHttpMethods(final RequestMapping requestMapping) { | ||
|
@@ -74,7 +98,7 @@ private static List<String> defineHttpMethods(final RequestMapping requestMappin | |
if (requestMapping != null) { | ||
return Arrays.stream(requestMapping.method()) | ||
.map(requestMethod -> requestMethod.toString().toLowerCase()) | ||
.collect(Collectors.toList()); | ||
.collect(toList()); | ||
} | ||
|
||
return methods; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.