fix: cast to correct types

This commit is contained in:
zaaarf 2024-01-23 11:47:34 +01:00
parent b7ea0fdd81
commit 1e5646a7fd
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
2 changed files with 25 additions and 25 deletions

View file

@ -1,6 +1,5 @@
package foo.zaaarf.routecompass;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
/**
@ -18,16 +17,16 @@ public class Route {
public final String method;
/**
* The {@link MediaType} produced by the endpoint.
* The media types produced by the endpoint.
* May be null if not specified.
*/
public final String produces;
public final String[] produces;
/**
* The {@link MediaType} consumed by the endpoint.
* The media types consumed by the endpoint.
* May be null if not specified.
*/
public final String consumes;
public final String[] consumes;
/**
* Whether the endpoint is deprecated.
@ -53,14 +52,14 @@ public class Route {
* The one and only constructor.
* @param path the path of the endpoint
* @param methods the {@link RequestMethod}s accepted by the endpoint
* @param consumes the {@link MediaType} consumed by the endpoint, may be null
* @param produces the {@link MediaType} produced by the endpoint, may be null
* @param consumes the media types consumed by the endpoint, may be null
* @param produces the media types produced by the endpoint, may be null
* @param deprecated whether the endpoint is deprecated
* @param returnType the DTO for the response type, may be null
* @param inputType the DTO for the request type, may be null
* @param params {@link Param}s of the endpoint, may be null
*/
public Route(String path, RequestMethod[] methods, MediaType consumes, MediaType produces,
public Route(String path, RequestMethod[] methods, String[] consumes, String[] produces,
boolean deprecated, DTO returnType, DTO inputType, Param... params) {
this.path = path;
@ -74,11 +73,11 @@ public class Route {
.append("]");
this.method = methodStringBuilder.toString();
if(produces != null) this.produces = produces.toString();
else this.produces = null;
if(produces != null) this.produces = produces;
else this.produces = new String[0];
if(consumes != null) this.consumes = consumes.toString();
else this.consumes = null;
if(consumes != null) this.consumes = consumes;
else this.consumes = new String[0];
this.deprecated = deprecated;

View file

@ -1,6 +1,5 @@
package foo.zaaarf.routecompass;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.processing.AbstractProcessor;
@ -100,8 +99,10 @@ public class RouteCompass extends AbstractProcessor {
out.print("\t- ");
if(r.deprecated) out.print("[DEPRECATED] ");
out.print(r.method + " " + r.path);
if(r.consumes != null) out.print("(expects: " + r.consumes + ")");
if(r.produces != null) out.print("(returns: " + r.produces + ")");
if(r.consumes != null) {
out.print("(expects: " + Arrays.toString(r.consumes) + ")");
}
if(r.produces != null) out.print("(returns: " + Arrays.toString(r.produces) + ")");
out.println();
BiConsumer<String, Route.Param[]> printParam = (name, params) -> {
@ -140,13 +141,13 @@ public class RouteCompass extends AbstractProcessor {
* @return the full route of the endpoint
*/
private String getFullRoute(TypeElement annotationType, Element element) {
try {
String route = this.getAnnotationFieldsValue(annotationType, element, "path", "value");
return this.getParentOrFallback(element, route, (a, e) -> {
try { //TODO support multiple routes
String[] routes = this.getAnnotationFieldsValue(annotationType, element, "path", "value");
return this.getParentOrFallback(element, routes[0], (a, e) -> {
String parent = this.getFullRoute(a, e);
StringBuilder sb = new StringBuilder(parent);
if(!parent.endsWith("/")) sb.append("/");
sb.append(route);
sb.append(routes[0]);
return sb.toString();
});
} catch (ReflectiveOperationException ex) {
@ -173,11 +174,11 @@ public class RouteCompass extends AbstractProcessor {
* Finds the media type consumed by an endpoint.
* @param annotationType the {@link TypeElement} with the annotation we are processing
* @param element the {@link Element} currently being examined
* @return the {@link MediaType} consumed by the endpoint
* @return the media type consumed by the endpoint
*/
private MediaType getConsumedType(TypeElement annotationType, Element element) {
private String[] getConsumedType(TypeElement annotationType, Element element) {
try {
MediaType res = this.getAnnotationFieldsValue(annotationType, element, "consumes");
String[] res = this.getAnnotationFieldsValue(annotationType, element, "consumes");
return res == null
? this.getParentOrFallback(element, res, this::getConsumedType)
: res;
@ -190,11 +191,11 @@ public class RouteCompass extends AbstractProcessor {
* Finds the media type consumed by an endpoint.
* @param annotationType the {@link TypeElement} with the annotation we are processing
* @param element the {@link Element} currently being examined
* @return the {@link MediaType} consumed by the endpoint
* @return the media type consumed by the endpoint
*/
private MediaType getProducedType(TypeElement annotationType, Element element) {
private String[] getProducedType(TypeElement annotationType, Element element) {
try {
MediaType res = this.getAnnotationFieldsValue(annotationType, element, "produces");
String[] res = this.getAnnotationFieldsValue(annotationType, element, "produces");
return res == null
? this.getParentOrFallback(element, res, this::getProducedType)
: res;