added fix script for loops in carta api specs + poc alternative endpoint
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
package SuMoC.backend.carta;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import sumoc.backend.carta.client.api.CartaApi;
|
||||
import sumoc.backend.carta.client.api.MapApi;
|
||||
import sumoc.backend.carta.client.invoker.ApiException;
|
||||
import sumoc.backend.carta.client.model.Direction;
|
||||
|
||||
@Service
|
||||
public class CartaClient {
|
||||
|
||||
private final MapApi mapApi;
|
||||
private final CartaApi cartaApi;
|
||||
|
||||
public CartaClient(MapApi mapApi) {
|
||||
public CartaClient(MapApi mapApi, CartaApi cartaApi) {
|
||||
this.cartaApi = cartaApi;
|
||||
this.mapApi = mapApi;
|
||||
}
|
||||
|
||||
@@ -47,7 +49,7 @@ public class CartaClient {
|
||||
) {
|
||||
try {
|
||||
String uid = null; // Optional user identifier
|
||||
return mapApi.getDirection(
|
||||
return cartaApi.getDirection(
|
||||
origin,
|
||||
destination,
|
||||
mode,
|
||||
@@ -63,7 +65,7 @@ public class CartaClient {
|
||||
location,
|
||||
uid
|
||||
);
|
||||
} catch (ApiException e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package SuMoC.backend.carta;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import sumoc.backend.carta.client.api.CartaApi;
|
||||
import sumoc.backend.carta.client.api.MapApi;
|
||||
import sumoc.backend.carta.client.invoker.ApiClient;
|
||||
|
||||
@@ -22,4 +23,9 @@ public class CartaClientConfig {
|
||||
public MapApi mapApi(ApiClient cartaApiClient) {
|
||||
return new MapApi(cartaApiClient);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CartaApi cartaApi(ApiClient cartaApiClient) {
|
||||
return new CartaApi(cartaApiClient);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package SuMoC.backend.routing;
|
||||
|
||||
import SuMoC.backend.routing.dtos.RouteAlternativeDTO;
|
||||
import SuMoC.backend.routing.dtos.RouteAlternativeRequestDTO;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/routing/alternative")
|
||||
public class RoutingAlternativeController {
|
||||
private final RoutingAlternativeService routingAlternativeService;
|
||||
|
||||
@Autowired
|
||||
public RoutingAlternativeController(RoutingAlternativeService routingAlternativeService) {
|
||||
this.routingAlternativeService = routingAlternativeService;
|
||||
}
|
||||
|
||||
// TODO operation description + how to handle preferences (optional, if not provided use saved users prefs or default
|
||||
@PostMapping("/request")
|
||||
public ResponseEntity<List<RouteAlternativeDTO>> requestAlternativeRoutes(@Valid @RequestBody RouteAlternativeRequestDTO routeAlternativeRequestDTO) {
|
||||
// Implementation goes here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package SuMoC.backend.routing;
|
||||
|
||||
import SuMoC.backend.carta.CartaClient;
|
||||
import SuMoC.backend.routing.dtos.RouteAlternativeDTO;
|
||||
import SuMoC.backend.routing.dtos.RouteAlternativeRequestDTO;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/routing/alternative")
|
||||
public class RoutingController {
|
||||
private final RoutingService routingService;
|
||||
private final CartaClient cartaClient;
|
||||
|
||||
@Autowired
|
||||
public RoutingController(RoutingService routingService, CartaClient cartaClient) {
|
||||
this.routingService = routingService;
|
||||
this.cartaClient = cartaClient;
|
||||
}
|
||||
|
||||
// TODO operation description + how to handle preferences (optional, if not provided use saved users prefs or default
|
||||
@PostMapping("/request")
|
||||
public ResponseEntity<List<RouteAlternativeDTO>> requestAlternativeRoutes(@Valid @RequestBody RouteAlternativeRequestDTO routeAlternativeRequestDTO) {
|
||||
var direction = cartaClient.getRoute(
|
||||
routeAlternativeRequestDTO.getOrigin(),
|
||||
routeAlternativeRequestDTO.getDestination(),
|
||||
"walking", // mode
|
||||
null, // arrivalTime
|
||||
null, // departureTime
|
||||
1, // alternatives
|
||||
null, // walkingSpeed
|
||||
null, // transferTime
|
||||
null, // accessible
|
||||
null, // parking
|
||||
null, // type
|
||||
null, // range
|
||||
null // location
|
||||
);
|
||||
var dto = new RouteAlternativeDTO();
|
||||
dto.setDirection(direction);
|
||||
return ResponseEntity.ok(List.of(dto));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -3,5 +3,5 @@ package SuMoC.backend.routing;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RoutingAlternativeService {
|
||||
public class RoutingService {
|
||||
}
|
||||
@@ -1,4 +1,16 @@
|
||||
package SuMoC.backend.routing.dtos;
|
||||
|
||||
import sumoc.backend.carta.client.model.Direction;
|
||||
|
||||
public class RouteAlternativeDTO {
|
||||
private Direction direction;
|
||||
|
||||
// Getters and setters
|
||||
public Direction getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public void setDirection(Direction direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,23 @@
|
||||
package SuMoC.backend.routing.dtos;
|
||||
|
||||
public class RouteAlternativeRequestDTO {
|
||||
private String origin;
|
||||
private String destination;
|
||||
|
||||
// Getters and setters
|
||||
public String getOrigin() {
|
||||
return origin;
|
||||
}
|
||||
|
||||
public void setOrigin(String origin) {
|
||||
this.origin = origin;
|
||||
}
|
||||
|
||||
public String getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
public void setDestination(String destination) {
|
||||
this.destination = destination;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ public class RoutingPreferencesDTO {
|
||||
private boolean preferPhysicalActivity = false;
|
||||
private List<Modality> excludedModalities = List.of();
|
||||
|
||||
public RoutingPreferencesDTO() {}
|
||||
public RoutingPreferencesDTO() {
|
||||
}
|
||||
|
||||
public RoutingPreferencesDTO(boolean preferPhysicalActivity, List<Modality> excludedModalities) {
|
||||
this.preferPhysicalActivity = preferPhysicalActivity;
|
||||
|
||||
@@ -89,6 +89,7 @@ public class SecurityConfiguration {
|
||||
//.authorizeHttpRequests(auth -> auth.anyRequest().permitAll()) //allows Access To ALL APIs, Security is off
|
||||
.authorizeHttpRequests(auth -> {
|
||||
auth.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll();
|
||||
auth.requestMatchers("/routing/alternative/**").permitAll();
|
||||
auth.requestMatchers("/auth/user/**").permitAll();
|
||||
auth.requestMatchers("/admin/**").hasRole("ADMIN"); //looks for "ROLE_ADMIN" //alternative: hasAuthoritiy("ADMIN")
|
||||
auth.requestMatchers("/user/**").hasAnyRole("ADMIN", "USER");
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package SuMoC.backend.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
|
||||
public class OpenApiSpecFixer {
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static final Set<String> SCHEMAS_TO_BREAK = Set.of(
|
||||
"GeoJsonGeometry",
|
||||
"GeoJsonGeometryCollection",
|
||||
"Step",
|
||||
"CyclingStep",
|
||||
"StreetStep"
|
||||
// Add other cyclic schemas here
|
||||
);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File inputFile = new File("src/main/resources/carta_orig.json");
|
||||
File outputFile = new File("src/main/resources/carta_fixed.json");
|
||||
|
||||
JsonNode root = mapper.readTree(inputFile);
|
||||
ObjectNode schemas = (ObjectNode) root.at("/components/schemas");
|
||||
|
||||
for (String schemaName : SCHEMAS_TO_BREAK) {
|
||||
if (schemas.has(schemaName)) {
|
||||
ObjectNode schema = (ObjectNode) schemas.get(schemaName);
|
||||
// Remove problematic recursive $ref, keep as generic object
|
||||
schema.removeAll();
|
||||
schema.put("type", "object");
|
||||
schema.put("description", "Flattened to break circular reference");
|
||||
System.out.println("Fixed: " + schemaName);
|
||||
}
|
||||
}
|
||||
|
||||
mapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, root);
|
||||
System.out.println("Fixed spec written to: " + outputFile.getPath());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user