|
| 1 | +/* |
| 2 | + * |
| 3 | + * * |
| 4 | + * * * |
| 5 | + * * * * |
| 6 | + * * * * * Copyright 2019-2022 the original author or authors. |
| 7 | + * * * * * |
| 8 | + * * * * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * * * * * you may not use this file except in compliance with the License. |
| 10 | + * * * * * You may obtain a copy of the License at |
| 11 | + * * * * * |
| 12 | + * * * * * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * * * * * |
| 14 | + * * * * * Unless required by applicable law or agreed to in writing, software |
| 15 | + * * * * * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * * * * * See the License for the specific language governing permissions and |
| 18 | + * * * * * limitations under the License. |
| 19 | + * * * * |
| 20 | + * * * |
| 21 | + * * |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +package org.springdoc.core.customizers; |
| 26 | + |
| 27 | +import java.util.Comparator; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.Map.Entry; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import io.swagger.v3.oas.models.OpenAPI; |
| 35 | +import io.swagger.v3.oas.models.PathItem; |
| 36 | +import io.swagger.v3.oas.models.Paths; |
| 37 | + |
| 38 | +import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; |
| 39 | + |
| 40 | +/** |
| 41 | + * The type Actuator open api customiser. |
| 42 | + * @author bnasslahsen |
| 43 | + * @Deprecated as not anymore required, use your own {@link org.springdoc.core.customizers.GlobalOpenApiCustomizer} instead |
| 44 | + */ |
| 45 | +public class ActuatorOpenApiCustomizer implements GlobalOpenApiCustomizer { |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * The Web endpoint properties. |
| 50 | + */ |
| 51 | + private final WebEndpointProperties webEndpointProperties; |
| 52 | + |
| 53 | + /** |
| 54 | + * Instantiates a new Actuator open api customizer. |
| 55 | + * |
| 56 | + * @param webEndpointProperties the web endpoint properties |
| 57 | + */ |
| 58 | + public ActuatorOpenApiCustomizer(WebEndpointProperties webEndpointProperties) { |
| 59 | + this.webEndpointProperties = webEndpointProperties; |
| 60 | + } |
| 61 | + |
| 62 | + private Stream<Entry<String, PathItem>> actuatorPathEntryStream(OpenAPI openApi, String relativeSubPath) { |
| 63 | + String pathPrefix = webEndpointProperties.getBasePath() + Optional.ofNullable(relativeSubPath).orElse(""); |
| 64 | + return Optional.ofNullable(openApi.getPaths()) |
| 65 | + .map(Paths::entrySet) |
| 66 | + .map(Set::stream) |
| 67 | + .map(s -> s.filter(entry -> entry.getKey().startsWith(pathPrefix))) |
| 68 | + .orElse(Stream.empty()); |
| 69 | + } |
| 70 | + |
| 71 | + private void handleActuatorOperationIdUniqueness(OpenAPI openApi) { |
| 72 | + Set<String> usedOperationIds = new HashSet<>(); |
| 73 | + actuatorPathEntryStream(openApi, null) |
| 74 | + .sorted(Comparator.comparing(Entry::getKey)) |
| 75 | + .forEachOrdered(stringPathItemEntry -> { |
| 76 | + stringPathItemEntry.getValue().readOperations().forEach(operation -> { |
| 77 | + String initialOperationId = operation.getOperationId(); |
| 78 | + String uniqueOperationId = operation.getOperationId(); |
| 79 | + int counter = 1; |
| 80 | + while (!usedOperationIds.add(uniqueOperationId)) { |
| 81 | + uniqueOperationId = initialOperationId + "_" + ++counter; |
| 82 | + } |
| 83 | + operation.setOperationId(uniqueOperationId); |
| 84 | + }); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void customise(OpenAPI openApi) { |
| 90 | + handleActuatorOperationIdUniqueness(openApi); |
| 91 | + } |
| 92 | +} |
0 commit comments