/** * Get the 'specificity' score of the provided API path. This is used when selecting an API operation to validate against - * where an incoming request matches multiple paths the 'most specific' one should win. * <p> * Note: This score is essentially meaningless across different paths - it should only be used to differentiate paths * that could be equivalent. For example, '{@code /{id}}' and '{@code /{id}.json}' could both match an incoming request on path * '{@code /foo.json}'; in that case we should match on '{@code /{id}.json}' as it is the most 'specific' match. * * @return a score >= 0 that indicates how 'specific' the path definition is. Higher numbers indicate more specific * definitions (e.g. fewer path variables). */ private static int specificityScore(final ApiPath apiPath) { // Return the length of the path, with path vars counting as 1. return apiPath.normalised().replaceAll("\\{.+?}", "").length(); }
/** * Matches operations that contain the given regular expression in their API path. * <p> * The tested path does not have parameters materialized, but is taken from the API * definition, e.g. "/store/order/{orderId}". */ public static WhitelistRule pathContains(final String regexp) { return new PrintableWhitelistRule( "Api path contains: '" + regexp + "'", (message, operation, request, response) -> operation != null && regexpContain(operation.getApiPath().normalised(), regexp)); }