Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#1175): OpenAPI connector enhancements for simulator random messa… #1177

Closed
wants to merge 8 commits into from
Closed
11 changes: 10 additions & 1 deletion connectors/citrus-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.apicurio</groupId>
<artifactId>apicurio-data-models</artifactId>
</dependency>
<dependency>
<groupId>com.atlassian.oai</groupId>
<artifactId>swagger-request-validator-core</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.17.0</version>
</dependency>

<!-- Test scoped dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright the original author or authors.
*
* Licensed 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.citrusframework.openapi;

public abstract class OpenApiConstants {

public static final String TYPE_ARRAY = "array";
public static final String TYPE_BOOLEAN = "boolean";
public static final String TYPE_INTEGER = "integer";
public static final String TYPE_NUMBER = "number";
public static final String TYPE_OBJECT = "object";
public static final String TYPE_STRING = "string";

public static final String FORMAT_INT32 = "int32";
public static final String FORMAT_INT64 = "int64";
public static final String FORMAT_FLOAT = "float";
public static final String FORMAT_DOUBLE = "double";
public static final String FORMAT_DATE = "date";
public static final String FORMAT_DATE_TIME = "date-time";
public static final String FORMAT_UUID = "uuid";

/**
* Prevent instantiation.
*/
private OpenApiConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright the original author or authors.
*
* Licensed 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.citrusframework.openapi;

import org.citrusframework.exceptions.CitrusRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import static java.lang.String.format;

/**
* A registry to store objects by OpenApi paths. The registry uses a digital tree data structure
* that performs path matching with variable placeholders. Variable
* placeholders must be enclosed in curly braces '{}', e.g., '/api/v1/pet/{id}'. This data structure
* is optimized for matching paths efficiently, handling both static and dynamic segments.
* <p>
* This class is currently not in use but may serve scenarios where a path needs to be mapped to an
* OasOperation without explicit knowledge of the API to which the path belongs.
* It could be utilized, for instance, in implementing an OAS message validator based on
* {@link org.citrusframework.validation.AbstractMessageValidator}.
*/
public class OpenApiPathRegistry<T> {

private static final Logger logger = LoggerFactory.getLogger(OpenApiPathRegistry.class);

private final RegistryNode root = new RegistryNode();

private final Map<String, T> allPaths = new ConcurrentHashMap<>();

public T search(String path) {
RegistryNode trieNode = internalSearch(path);
return trieNode != null ? trieNode.value : null;
}

RegistryNode internalSearch(String path) {
String[] segments = path.split("/");
return searchHelper(root, segments, 0);
}

public boolean insert(String path, T value) {
return insertInternal(path, value) != null;
}

RegistryNode insertInternal(String path, T value) {

if (path == null || value == null) {
return null;
}

String[] segments = path.split("/");
RegistryNode node = root;

if (!allPaths.isEmpty() && (isPathAlreadyContainedWithDifferentValue(path, value)
|| isPathMatchedByOtherPath(path, value))) {
return null;
}

allPaths.put(path, value);
StringBuilder builder = new StringBuilder();
for (String segment : segments) {
if (builder.isEmpty() || builder.charAt(builder.length() - 1) != '/') {
builder.append("/");
}
builder.append(segment);

if (!node.children.containsKey(segment)) {
RegistryNode trieNode = new RegistryNode();
trieNode.path = builder.toString();
node.children.put(segment, trieNode);
}
node = node.children.get(segment);
}

// Sanity check to disallow overwrite of existing values
if (node.value != null && !node.value.equals(value)) {
throw new CitrusRuntimeException(format(
"Illegal attempt to overwrite an existing node value. This is probably a bug. path=%s value=%s",
node.path, node.value));
}
node.value = value;

return node;
}

/**
* Tests if the path is either matching an existing path or any existing path matches the given
* patch.
* <p>
* For example '/a/b' does not match '/{a}/{b}', but '/{a}/{b}' matches '/a/b'.
*/
private boolean isPathMatchedByOtherPath(String path, T value) {

// Does the given path match any existing
RegistryNode currentValue = internalSearch(path);
if (currentValue != null && !Objects.equals(path, currentValue.path)) {
logger.error(
"Attempt to insert an equivalent path potentially overwriting an existing value. Value for path is ignored: path={}, value={} currentValue={} ",
path, currentValue, value);
return true;
}

// Does any existing match the path.
OpenApiPathRegistry<T> tmpTrie = new OpenApiPathRegistry<>();
tmpTrie.insert(path, value);

List<String> allMatching = allPaths.keySet().stream()
.filter(existingPath -> {
RegistryNode trieNode = tmpTrie.internalSearch(existingPath);
return trieNode != null && !existingPath.equals(trieNode.path);
}).map(existingPath -> "'" + existingPath + "'").toList();
if (!allMatching.isEmpty() && logger.isErrorEnabled()) {
logger.error(
"Attempt to insert an equivalent path overwritten by existing paths. Value for path is ignored: path={}, value={} existingPaths=[{}]",
path, currentValue, String.join(",", allMatching));

}

return !allMatching.isEmpty();
}

private boolean isPathAlreadyContainedWithDifferentValue(String path, T value) {
T currentValue = allPaths.get(path);
if (currentValue != null) {
if (value.equals(currentValue)) {
return false;
}
logger.error(
"Attempt to overwrite value for path is ignored: path={}, value={} currentValue={} ",
path, currentValue, value);
return true;
}
return false;
}

private RegistryNode searchHelper(RegistryNode node, String[] segments, int index) {
if (node == null) {
return null;
}
if (index == segments.length) {
return node;
}

String segment = segments[index];

// Exact match
if (node.children.containsKey(segment)) {
RegistryNode foundNode = searchHelper(node.children.get(segment), segments, index + 1);
if (foundNode != null && foundNode.value != null) {
return foundNode;
}
}

// Variable match
for (String key : node.children.keySet()) {
if (key.startsWith("{") && key.endsWith("}")) {
RegistryNode foundNode = searchHelper(node.children.get(key), segments, index + 1);
if (foundNode != null && foundNode.value != null) {
return foundNode;
}
}
}

return null;
}

class RegistryNode {
Map<String, RegistryNode> children = new HashMap<>();
String path;
T value = null;
}
}
Loading