davsclaus commented on code in PR #28183: URL: https://github.com/apache/camel/pull/28183#discussion_r4333294055
########## components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/OpenApiExternalReferences.java: ########## @@ -0,0 +1,194 @@ +/* + * 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.camel.component.rest.openapi; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import io.swagger.v3.core.util.Json; +import io.swagger.v3.core.util.Yaml; +import io.swagger.v3.parser.models.RefFormat; +import io.swagger.v3.parser.util.RefUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Checks the references ({@code $ref}) of an OpenAPI specification against the {@code externalReferences} option before + * the parser resolves them. The parser loads every external reference it meets: from the working directory and the + * classpath, or over http(s) when the reference is a URL or the specification itself comes from a URL. + * <p> + * camel-openapi-java has a copy of this class for the specification it serves as api-doc; keep the two in sync. + */ +final class OpenApiExternalReferences { + + static final String ALL = "all"; + static final String LOCAL = "local"; + static final String NONE = "none"; + + private static final Logger LOG = LoggerFactory.getLogger(OpenApiExternalReferences.class); + + private OpenApiExternalReferences() { + } + + /** + * The value of the option to apply: {@link #ALL} when none is set. + * + * @throws IllegalArgumentException for a value other than all, local or none + */ + static String policy(String externalReferences) { + if (externalReferences == null || externalReferences.isBlank()) { + return ALL; + } + String policy = externalReferences.trim().toLowerCase(Locale.ROOT); + if (ALL.equals(policy) || LOCAL.equals(policy) || NONE.equals(policy)) { + return policy; + } + throw new IllegalArgumentException( + "Unknown externalReferences value: " + externalReferences + ", expected all, local or none"); + } + + /** + * Checks every {@code $ref} of the specification, and of the local documents it references, against the option. + * + * @param externalReferences the option: all, local or none + * @param specification the specification, as JSON or YAML text + * @param specificationUri where the specification comes from, for the error message + * @param remote whether the specification comes from a URL: the parser then resolves its + * external references against that URL + * @throws IllegalArgumentException for the first reference the option does not allow + */ + static void check(String externalReferences, String specification, String specificationUri, boolean remote) { + String policy = policy(externalReferences); + if (ALL.equals(policy)) { + return; + } + checkDocument(policy, specification, specificationUri, null, remote, new HashSet<>()); + } + + private static void checkDocument( + String policy, String content, String specificationUri, String document, boolean remote, + Set<String> checked) { + DocumentCheck check = new DocumentCheck(policy, specificationUri, document, remote); + check.scan(readTree(content, specificationUri, document), ""); + // a local document can reference further documents, which the parser loads as well + for (String file : check.localDocuments) { + if (checked.add(file)) { Review Comment: When nested refs are resolved against their containing document (point 1), `checked` and the `document` passed down should hold the resolved location, not the ref as written. Otherwise `common.yaml` referenced from `a/x.yaml` and from `b/y.yaml` counts as one document, and only the first one is checked. Same in the camel-openapi-java copy. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
