Skip to content

fix diff method can not be added to RadixTree #15445

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

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void register0(RequestMapping mapping, HandlerMeta handler, AtomicIntege
try {
Registration registration = new Registration(mapping, handler);
for (PathExpression path : mapping.getPathCondition().getExpressions()) {
Registration exists = tree.addPath(path, registration);
Registration exists = tree.addPath(path, registration, Registration::isMappingOverlap);
if (exists == null) {
counter.incrementAndGet();
if (LOGGER.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;

/**
Expand Down Expand Up @@ -63,13 +65,25 @@ public RadixTree() {
this(true, '/');
}

/**
* This is a default implementation that does not check for equality.
*/
public T addPath(PathExpression path, T value) {
return addPath(path, value, (t, t2) -> Boolean.TRUE);
}

/**
* When the path is the same, the predicate is used to determine whether the values are considered equal.
* If the predicate returns true, the existing value is returned directly.
*/
public T addPath(PathExpression path, T value, BiFunction<T, T, Boolean> predicate) {
Objects.requireNonNull(predicate);
if (path.isDirect()) {
KeyString key = new KeyString(path.getPath(), caseSensitive);
List<Match<T>> matches = directPathMap.computeIfAbsent(key, k -> new ArrayList<>());
for (int i = 0, size = matches.size(); i < size; i++) {
Match<T> match = matches.get(i);
if (match.getValue().equals(value)) {
if (predicate.apply(match.getValue(), value)) {
return match.getValue();
}
}
Expand All @@ -85,7 +99,9 @@ public T addPath(PathExpression path, T value) {
List<Pair<PathExpression, T>> values = child.values;
for (int j = 0, size = values.size(); j < size; j++) {
if (values.get(j).getLeft().equals(path)) {
return values.get(j).getRight();
if (predicate.apply(values.get(j).getRight(), value)) {
return values.get(j).getRight();
}
}
}
values.add(Pair.of(path, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.HandlerMeta;

import java.util.Collections;
import java.util.Objects;

public final class Registration {

private final RequestMapping mapping;
Expand Down Expand Up @@ -47,6 +50,24 @@ public boolean equals(Object obj) {
return mapping.equals(((Registration) obj).mapping);
}

public boolean isMappingOverlap(Registration other) {
RequestMapping otherMapping = other.getMapping();
if (mapping == otherMapping) {
return true;
}
return (mapping.getMethodsCondition() == null
|| otherMapping.getMethodsCondition() == null
|| !Collections.disjoint(
mapping.getMethodsCondition().getMethods(),
otherMapping.getMethodsCondition().getMethods()))
&& Objects.equals(mapping.getParamsCondition(), otherMapping.getParamsCondition())
&& Objects.equals(mapping.getHeadersCondition(), otherMapping.getHeadersCondition())
&& Objects.equals(mapping.getConsumesCondition(), otherMapping.getConsumesCondition())
&& Objects.equals(mapping.getProducesCondition(), otherMapping.getProducesCondition())
&& Objects.equals(mapping.getCustomCondition(), otherMapping.getCustomCondition())
&& Objects.equals(mapping.getSig(), otherMapping.getSig());
}

@Override
public int hashCode() {
return mapping.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ public ProducesCondition getProducesCondition() {
return producesCondition;
}

public ConditionWrapper getCustomCondition() {
return customCondition;
}

public CorsMeta getCors() {
return cors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.dubbo.rpc.protocol.tri.rest.mapping

import org.apache.dubbo.rpc.protocol.tri.rest.mapping.condition.PathExpression

import spock.lang.Specification

class RadixTreeTest extends Specification {
Expand Down Expand Up @@ -59,6 +61,42 @@ class RadixTreeTest extends Specification {
'/a/b/c/d' | 0
}

def "test repeat add,no predicate function"() {
given:
def tree = new RadixTree<Boolean>()
Boolean val1 = tree.addPath('/a/*/*', false)
Boolean val2 = tree.addPath('/a/*/*', true)
expect:
val1 == null;
val2 == false;
}

def "test repeat add,use predicate function"() {
given:
def tree = new RadixTree<Boolean>()
Boolean val1 = tree.addPath(PathExpression.parse('/a/*/*'), false, { a, b -> a == b })
Boolean val2 = tree.addPath(PathExpression.parse('/a/*/*'), true, { a, b -> a == b })
Boolean val3 = tree.addPath(PathExpression.parse('/a/*/*'), true, { a, b -> a == b })

expect:
val1 == null;
val2 == null;
val3 == true;
}

def "test repeat add,use predicate function and Registration"() {
given:
def tree = new RadixTree<Boolean>()
Boolean val1 = tree.addPath(PathExpression.parse('/a/*/*'), false, { a, b -> a == b })
Boolean val2 = tree.addPath(PathExpression.parse('/a/*/*'), true, { a, b -> a == b })
Boolean val3 = tree.addPath(PathExpression.parse('/a/*/*'), true, { a, b -> a == b })

expect:
val1 == null;
val2 == null;
val3 == true;
}

def "test sub path match"() {
given:
def tree = new RadixTree<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.dubbo.rpc.protocol.tri.rest.mapping

import org.apache.dubbo.remoting.http12.HttpMethods
import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.HandlerMeta

import spock.lang.Specification

class RegistrationSpec extends Specification {

def "isMethodOverlap should return true when methods overlap"() {
given:
def mapping1 = new RequestMapping.Builder().method(HttpMethods.GET.name(), HttpMethods.PUT.name()).build()
def mapping2 = new RequestMapping.Builder().method(HttpMethods.PUT.name(), HttpMethods.POST.name()).build()
def meta = GroovyMock(HandlerMeta)
def reg1 = new Registration(mapping1, meta)
def reg2 = new Registration(mapping2, meta)

expect:
reg1.isMappingOverlap(reg2)
}

def "isMethodOverlap should return false when methods do not overlap"() {
given:
def mapping1 = new RequestMapping.Builder().method(HttpMethods.GET.name()).build()
def mapping2 = new RequestMapping.Builder().method(HttpMethods.PUT.name()).build()

def meta = GroovyMock(HandlerMeta)
def reg1 = new Registration(mapping1, meta)
def reg2 = new Registration(mapping2, meta)

expect:
!reg1.isMappingOverlap(reg2)
}

def "isMethodOverlap should return true if both MethodsCondition is null"() {
given:
def mapping1 = new RequestMapping.Builder().build()
def mapping2 = new RequestMapping.Builder().build()

def meta = GroovyMock(HandlerMeta)
def reg1 = new Registration(mapping1, meta)
def reg2 = new Registration(mapping2, meta)

expect:
reg1.isMappingOverlap(reg2)
}

def "isMethodOverlap should return false if only one MethodsCondition is null"() {
given:
def mapping1 = new RequestMapping.Builder().method(HttpMethods.GET.name()).build()
def mapping2 = new RequestMapping.Builder().method().build()

def meta = GroovyMock(HandlerMeta)
def reg1 = new Registration(mapping1, meta)
def reg2 = new Registration(mapping2, meta)

expect:
reg1.isMappingOverlap(reg2)
}

}
Loading