|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.infrastructure.openapi; |
| 20 | + |
| 21 | +import io.swagger.v3.jaxrs2.Reader; |
| 22 | +import io.swagger.v3.oas.annotations.Operation; |
| 23 | +import io.swagger.v3.oas.models.OpenAPI; |
| 24 | +import jakarta.ws.rs.HttpMethod; |
| 25 | +import jakarta.ws.rs.Path; |
| 26 | +import java.lang.annotation.Annotation; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +public final class FineractOperationIdReader extends Reader { |
| 35 | + |
| 36 | + // Check explicit @Operation ids first, then let Swagger build the spec. |
| 37 | + @Override |
| 38 | + public OpenAPI read(Set<Class<?>> classes, Map<String, Object> resources) { |
| 39 | + ExplicitOperationValidator.validate(classes); |
| 40 | + return super.read(classes, resources); |
| 41 | + } |
| 42 | + |
| 43 | + static final class ExplicitOperationValidator { |
| 44 | + |
| 45 | + static void validate(Set<Class<?>> classes) { |
| 46 | + Map<String, List<String>> operationIds = new LinkedHashMap<>(); |
| 47 | + for (Class<?> resourceClass : classes) { |
| 48 | + // No @Path means this class is not a JAX-RS resource. |
| 49 | + if (resourceClass.getAnnotation(Path.class) == null) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + for (Method method : resourceClass.getMethods()) { |
| 54 | + Operation operation = method.getAnnotation(Operation.class); |
| 55 | + String operationId = trimToNull(operation == null ? null : operation.operationId()); |
| 56 | + |
| 57 | + // We only care about actual endpoints that set an id explicitly. |
| 58 | + if (operationId == null || !hasHttpMethod(method)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + operationIds.computeIfAbsent(operationId, ignored -> new ArrayList<>()) |
| 63 | + .add(resourceClass.getSimpleName() + "#" + method.getName()); |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + List<String> duplicates = operationIds.entrySet().stream().filter(e -> e.getValue().size() > 1) |
| 69 | + .map(e -> e.getKey() + " -> " + String.join(", ", e.getValue())).sorted().toList(); |
| 70 | + if (!duplicates.isEmpty()) { |
| 71 | + throw new IllegalStateException( |
| 72 | + "Duplicate explicit OpenAPI operationIds detected:\n - " + String.join("\n - ", duplicates)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // GET, POST, etc. are meta-annotated with @HttpMethod. |
| 78 | + private static boolean hasHttpMethod(Method method) { |
| 79 | + for (Annotation annotation : method.getAnnotations()) { |
| 80 | + if (annotation.annotationType().getAnnotation(HttpMethod.class) != null) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + private static String trimToNull(String value) { |
| 88 | + if (value == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + String trimmed = value.trim(); |
| 92 | + return trimmed.isEmpty() ? null : trimmed; |
| 93 | + } |
| 94 | +} |
0 commit comments