valepakh commented on code in PR #6121: URL: https://github.com/apache/ignite-3/pull/6121#discussion_r2185175091
########## modules/compatibility-tests/src/testFixtures/java/org/apache/ignite/internal/OpenApiMatcher.java: ########## @@ -0,0 +1,434 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal; + +import static java.util.Optional.ofNullable; +import static java.util.stream.Collectors.toMap; +import static java.util.stream.Collectors.toSet; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.PathItem.HttpMethod; +import io.swagger.v3.oas.models.Paths; +import io.swagger.v3.oas.models.media.Content; +import io.swagger.v3.oas.models.media.MediaType; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.parameters.Parameter; +import io.swagger.v3.oas.models.parameters.RequestBody; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.StringDescription; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.jetbrains.annotations.Nullable; + +/** + * Matcher for checking OpenAPI compatibility between versions. Checks include missing paths, missing operations and others. + */ +@SuppressWarnings("rawtypes") // Mostly for the Schema class, which is parameterized but the API returns raw type. +public class OpenApiMatcher extends TypeSafeDiagnosingMatcher<OpenAPI> { + private final OpenAPI baseApi; + + private OpenApiMatcher(OpenAPI baseApi) { + this.baseApi = baseApi; + } + + public static Matcher<OpenAPI> isCompatibleWith(OpenAPI baseApi) { + return new OpenApiMatcher(baseApi); + } + + @Override + protected boolean matchesSafely(OpenAPI currentApi, Description mismatchDescription) { + return new OpenApiDiff(baseApi, currentApi, mismatchDescription).compare(); + } + + private static class OpenApiDiff { + private final OpenAPI baseApi; + private final OpenAPI currentApi; + private final Description mismatchDescription; + + // Current direction + private boolean isRequest; + + private OpenApiDiff(OpenAPI baseApi, OpenAPI currentApi, Description mismatchDescription) { + this.baseApi = baseApi; + this.currentApi = currentApi; + this.mismatchDescription = mismatchDescription; + } + + boolean compare() { + Paths basePaths = ofNullable(baseApi.getPaths()).orElse(new Paths()); + Paths currentPaths = ofNullable(currentApi.getPaths()).orElse(new Paths()); + return comparePaths(basePaths, currentPaths); + } + + private boolean comparePaths(Paths basePaths, Paths currentPaths) { + // Check for missing paths + Set<String> missingPaths = basePaths.keySet().stream() + .filter(path -> !currentPaths.containsKey(path)) + .collect(toSet()); + + if (!missingPaths.isEmpty()) { + mismatchDescription.appendText("has missing paths ").appendValueList("", ", ", "", missingPaths); + return false; + } + + for (Entry<String, PathItem> entry : basePaths.entrySet()) { + String path = entry.getKey(); + + Map<HttpMethod, Operation> baseOperations = entry.getValue().readOperationsMap(); + Map<HttpMethod, Operation> currentOperations = currentPaths.get(path).readOperationsMap(); + + if (!compareOperations(path, baseOperations, currentOperations)) { + return false; + } + } + + return true; + } + + private boolean compareOperations( + String path, + Map<HttpMethod, Operation> baseOperations, + Map<HttpMethod, Operation> currentOperations + ) { + Set<HttpMethod> missingMethods = baseOperations.keySet().stream() + .filter(method -> !currentOperations.containsKey(method)) + .collect(toSet()); + + if (!missingMethods.isEmpty()) { + mismatchDescription.appendText("path ").appendValue(path) + .appendText(" has missing operations ").appendValueList("", ", ", "", missingMethods); + return false; + } + + for (Entry<HttpMethod, Operation> operationEntry : baseOperations.entrySet()) { + Operation baseOperation = operationEntry.getValue(); + Operation currentOperation = currentOperations.get(operationEntry.getKey()); + + if (!compareOperation(path, operationEntry.getKey(), baseOperation, currentOperation)) { + return false; + } + } + + return true; + } + + private boolean compareOperation( + String path, + HttpMethod method, + Operation baseOperation, + Operation currentOperation + ) { + if (!Objects.equals(baseOperation.getOperationId(), currentOperation.getOperationId())) { + describeOperation(path, method) + .appendText(" has different operation id ").appendValue(currentOperation.getOperationId()); + return false; + } + + return compareRequestBody(path, method, baseOperation.getRequestBody(), currentOperation.getRequestBody()) + && compareParameters(path, method, baseOperation, currentOperation) + && compareResponses(path, method, baseOperation, currentOperation); + } + + private boolean compareRequestBody( + String path, + HttpMethod method, + RequestBody baseRequestBody, + RequestBody currentRequestBody + ) { + isRequest = true; + if (baseRequestBody != null) { + if (currentRequestBody == null) { + describeOperation(path, method).appendText(" has missing request body"); + return false; + } + + StringDescription contentMismatchDescription = new StringDescription(); + if (!compareContent(contentMismatchDescription, baseRequestBody.getContent(), currentRequestBody.getContent())) { + describeOperation(path, method) + .appendText(" has different request body content: ").appendText(contentMismatchDescription.toString()); + return false; + } + } + return true; + } + + private boolean compareParameters( + String path, + HttpMethod method, + Operation baseOperation, + Operation currentOperation + ) { + isRequest = true; + Map<String, Parameter> baseParameters = parametersByName(baseOperation.getParameters()); + Map<String, Parameter> currentParameters = parametersByName(currentOperation.getParameters()); + + for (Entry<String, Parameter> entry : baseParameters.entrySet()) { + Parameter baseParameter = entry.getValue(); + Parameter currentParameter = currentParameters.get(entry.getKey()); + if (currentParameter != null && !compareParameter(path, method, baseParameter, currentParameter)) { + return false; Review Comment: > could you add a description here? what is wrong? The `compareParameter` method adds information to the description. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org