@Override public void validate(ServiceDefinition definition) { Multimap<String, String> pathToEndpoints = ArrayListMultimap.create(); definition.getEndpoints().stream().forEach(entry -> { String methodPath = entry.getHttpMethod().get() + " " + entry.getHttpPath().get(); // normalize all path parameter variables and regular expressions because all path args are treated // as identical for comparisons (paths cannot differ only in the name/regular expression of a path // variable) methodPath = PATHVAR_PATTERN.matcher(methodPath).replaceAll("{arg}"); pathToEndpoints.put(methodPath, entry.getEndpointName().get()); }); pathToEndpoints.keySet().stream().sorted().forEachOrdered(key -> { Collection<String> endpoints = pathToEndpoints.get(key); Preconditions.checkState(endpoints.size() <= 1, "Endpoint \"%s\" is defined by multiple endpoints: %s", key, endpoints.toString()); }); } }
@Override public void validate(EndpointDefinition definition) { Set<ArgumentName> pathParamIds = new HashSet<>(); definition.getArgs().stream() .filter(entry -> entry.getParamType().accept(ParameterTypeVisitor.IS_PATH)) .forEach(entry -> { boolean added = pathParamIds.add(entry.getArgName()); Preconditions.checkState(added, "Path parameter with identifier \"%s\" is defined multiple times for endpoint", entry.getArgName().get()); }); Set<ArgumentName> pathArgs = HttpPathValidator.pathArgs(definition.getHttpPath().get()); Set<ArgumentName> extraParams = Sets.difference(pathParamIds, pathArgs); Preconditions.checkState(extraParams.isEmpty(), "Path parameters defined in endpoint but not present in path template: %s. " + "Note that the `param-id` is no longer supported and the path template name is always " + "used instead. So make sure the path template name matches the path parameter defined " + "in endpoint.", extraParams); Set<ArgumentName> missingParams = Sets.difference(pathArgs, pathParamIds); Preconditions.checkState(missingParams.isEmpty(), "Path parameters defined path template but not present in endpoint: %s", missingParams); } }
@Override public void validate(EndpointDefinition definition) { HttpMethod method = definition.getHttpMethod(); if (method.equals(HttpMethod.GET)) { boolean hasBody = definition.getArgs() .stream() .anyMatch(entry -> entry.getParamType().accept(ParameterTypeVisitor.IS_BODY)); Preconditions.checkState(!hasBody, "Endpoint cannot be a GET and contain a body: method: %s, path: %s", definition.getHttpMethod(), definition.getHttpPath()); } } }