Skip to content

Commit 8d48765

Browse files
[maven-tool] 对于默认的参数值,若为空,不添加该值 (#355)
* [maven-tool] 对于默认的参数值,若为空,不添加该值 * [maven-tool] 修改检视意见
1 parent 37f726d commit 8d48765

File tree

7 files changed

+114
-23
lines changed

7 files changed

+114
-23
lines changed

framework/fel/java/maven-plugins/tool-maven-plugin/src/main/java/modelengine/fel/maven/complie/parser/ByteBuddySchemaParser.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
import modelengine.fel.tool.info.entity.ReturnPropertyEntity;
1515
import modelengine.fel.tool.info.entity.SchemaEntity;
1616
import modelengine.fitframework.annotation.Property;
17+
import modelengine.fitframework.annotation.util.PropertyHelper;
18+
import modelengine.fitframework.util.StringUtils;
1719

1820
import net.bytebuddy.description.annotation.AnnotationDescription;
1921
import net.bytebuddy.description.method.MethodDescription;
2022
import net.bytebuddy.description.method.ParameterDescription;
2123

2224
import static modelengine.fitframework.inspection.Validation.notNull;
2325

24-
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.LinkedHashMap;
@@ -79,7 +80,9 @@ private static Map<String, Object> getStringObjectMap(ReturnPropertyEntity retur
7980
if (returnPropertyEntity.getConvertor() != null) {
8081
returnProperty.put("convertor", returnPropertyEntity.getConvertor());
8182
}
82-
returnProperty.put("examples", returnPropertyEntity.getExamples());
83+
if (returnPropertyEntity.getExamples() != null) {
84+
returnProperty.put("examples", returnPropertyEntity.getExamples());
85+
}
8386
return returnProperty;
8487
}
8588

@@ -113,8 +116,14 @@ private static PropertyEntity parseProperty(ParameterDescription parameterDescri
113116
Property property = paramAnnotation.load();
114117
entity.setDescription(property.description());
115118
entity.setNeed(property.required());
116-
entity.setDefaultValue(property.defaultValue());
117-
entity.setExamples(Collections.singletonList(property.example()));
119+
String defaultValue = property.defaultValue();
120+
if (defaultValue != null && PropertyHelper.isCustomValue(defaultValue)) {
121+
entity.setDefaultValue(defaultValue);
122+
}
123+
String example = property.example();
124+
if (StringUtils.isNotEmpty(example)) {
125+
entity.setExamples(Collections.singletonList(example));
126+
}
118127
}
119128
return entity;
120129
}
@@ -127,7 +136,10 @@ private static ReturnPropertyEntity parseReturnProperty(MethodDescription method
127136
Property property = returnAnnotation.load();
128137
returnPropertyEntity.setName(property.name());
129138
returnPropertyEntity.setDescription(property.description());
130-
returnPropertyEntity.setExamples(Collections.singletonList(property.example()));
139+
String example = property.example();
140+
if (StringUtils.isNotEmpty(example)) {
141+
returnPropertyEntity.setExamples(Collections.singletonList(example));
142+
}
131143
}
132144
notNull(methodDescription.getReturnType(), "The return type cannot be null.");
133145
JsonNode jsonNode = JacksonTypeParser.getParameterSchema(methodDescription.getReturnType());

framework/fel/java/maven-plugins/tool-maven-plugin/src/test/java/modelengine/fel/maven/compile/parser/weather/Rain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public interface Rain {
3232
*/
3333
@ToolMethod(name = "rain_today", description = "该方法获取今天的下雨信息")
3434
@Genericable("genericable_weather_rain_today")
35-
String today(@Property(description = "查询地点", required = true, example = "Hangzhou") String location,
36-
@Property(description = "查询日期", required = true) Date date,
35+
String today(@Property(description = "查询地点", required = true, defaultValue = "Shanghai", example = "Hangzhou")
36+
String location, @Property(description = "查询日期", required = true) Date date,
3737
@Property(description = "下雨的经纬度") RainPosition rainPosition,
3838
@Property(description = "其他信息") Object info);
3939

framework/fel/java/maven-plugins/tool-maven-plugin/src/test/resources/weather-tools.json

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
"name" : "location",
1818
"type" : "string",
1919
"examples" : [ "Hangzhou" ],
20-
"default" : ""
20+
"default" : "Shanghai"
2121
},
2222
"date" : {
2323
"description" : "查询日期",
2424
"name" : "date",
25-
"type" : "string",
26-
"examples" : [ "" ],
27-
"default" : ""
25+
"type" : "string"
2826
},
2927
"rainPosition" : {
3028
"description" : "下雨的经纬度",
@@ -38,17 +36,13 @@
3836
"type" : "string"
3937
}
4038
},
41-
"examples" : [ "" ],
42-
"required" : [ "latitude", "longitude" ],
43-
"default" : ""
39+
"required" : [ "latitude", "longitude" ]
4440
},
4541
"info" : {
4642
"description" : "其他信息",
4743
"name" : "info",
4844
"type" : "object",
49-
"properties" : { },
50-
"examples" : [ "" ],
51-
"default" : ""
45+
"properties" : { }
5246
}
5347
},
5448
"required" : [ "location", "date" ]
@@ -136,8 +130,7 @@
136130
"name" : "",
137131
"description" : "获取今日下雨信息的结果",
138132
"type" : "string",
139-
"convertor" : "",
140-
"examples" : [ "" ]
133+
"convertor" : ""
141134
}
142135
},
143136
"runnables" : {
@@ -174,8 +167,7 @@
174167
"name" : "",
175168
"description" : "获取明日下雨信息的结果",
176169
"type" : "string",
177-
"convertor" : "",
178-
"examples" : [ "" ]
170+
"convertor" : ""
179171
}
180172
},
181173
"runnables" : {

framework/fel/java/services/tool-service/src/main/java/modelengine/fel/tool/support/MethodToolMetadata.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import modelengine.fel.tool.annotation.Group;
1414
import modelengine.fel.tool.annotation.ToolMethod;
1515
import modelengine.fitframework.annotation.Property;
16+
import modelengine.fitframework.annotation.util.PropertyHelper;
1617
import modelengine.fitframework.json.schema.JsonSchemaManager;
1718
import modelengine.fitframework.util.MapBuilder;
1819
import modelengine.fitframework.util.StringUtils;
@@ -67,7 +68,10 @@ public Object parameterDefaultValue(String name) {
6768
// TODO 需要考虑注解值的类型匹配与基础类型的默认值问题
6869
Property annotation = parameter.getDeclaredAnnotation(Property.class);
6970
if (annotation != null) {
70-
return annotation.defaultValue();
71+
String defaultValue = annotation.defaultValue();
72+
if (defaultValue != null && PropertyHelper.isCustomValue(defaultValue)) {
73+
return defaultValue;
74+
}
7175
}
7276
return null;
7377
}).orElse(null);

framework/fel/java/services/tool-service/src/test/java/modelengine/fel/tool/support/MethodToolMetadataTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,42 @@ void shouldReturnParameterIndex() {
185185
assertThat(actual).isEqualTo(0);
186186
}
187187

188+
@Test
189+
@DisplayName("当参数名称不存在时,返回 -1 序号")
190+
void shouldReturnParameterIndexNegativeWhenNameNotExists() {
191+
int actual = this.toolMetadata.parameterIndex("NotExists");
192+
assertThat(actual).isEqualTo(-1);
193+
}
194+
188195
@Test
189196
@DisplayName("返回正确的必须参数名字列表")
190197
void shouldReturnRequired() {
191198
Set<String> parameterNames = this.toolMetadata.requiredParameters();
192199
assertThat(parameterNames).containsExactly("P1");
193200
}
194201

202+
@Test
203+
@DisplayName("当参数为基础类型时,必须参数应包含该参数")
204+
void shouldReturnRequiredWhenPrimitiveParam() throws NoSuchMethodException {
205+
Method primitiveMethod = PrimitiveInterface.class.getDeclaredMethod("primitiveMethod", int.class);
206+
Tool.Metadata primitiveMetadata = Tool.Metadata.fromMethod(primitiveMethod);
207+
Set<String> required = primitiveMetadata.requiredParameters();
208+
assertThat(required).containsExactly("i");
209+
}
210+
195211
@Test
196212
@DisplayName("返回正确的返回值类型")
197213
void shouldReturnReturnType() {
198214
Map<String, Object> type = this.toolMetadata.returnType();
199215
assertThat(type).isEqualTo(JsonSchemaManager.create().createSchema(String.class).toJsonObject());
200216
}
201217

218+
@Test
219+
@DisplayName("返回转换器字符串为空")
220+
void shouldReturnEmptyReturnConverter() {
221+
assertThat(this.toolMetadata.returnConverter()).isEqualTo("");
222+
}
223+
202224
@Test
203225
@DisplayName("返回正确的格式规范描述")
204226
void shouldReturnSchema() {
@@ -217,6 +239,23 @@ void shouldReturnDefaultValueNull() {
217239
assertThat(definitionGroupName).isNotNull();
218240
}
219241

242+
@Test
243+
@DisplayName("参数默认值非自定义占位时应为 null")
244+
void shouldReturnNullParameterDefaultValueWhenNotCustom() {
245+
Object defaultValue = this.toolMetadata.parameterDefaultValue("P1");
246+
assertThat(defaultValue).isEqualTo("default_value");
247+
}
248+
249+
@Test
250+
@DisplayName("返回方法、名称、分组与描述信息正确")
251+
void shouldReturnMethodAndDefinitions() {
252+
assertThat(this.toolMetadata.getMethod()).isPresent();
253+
assertThat(this.toolMetadata.getMethod().orElse(null)).isEqualTo(this.testMethod);
254+
assertThat(this.toolMetadata.definitionName()).isEqualTo("test_tool_def_name");
255+
assertThat(this.toolMetadata.definitionGroupName()).isEqualTo("test_def_group_name");
256+
assertThat(this.toolMetadata.description()).isEqualTo("测试方法的描述信息");
257+
}
258+
220259
@Group(name = "test_def_group_name")
221260
interface TestInterface {
222261
/**
@@ -229,4 +268,10 @@ interface TestInterface {
229268
@Genericable(id = "t1", description = "desc")
230269
String testMethod(@Property(name = "P1", required = true, defaultValue = "default_value") String p1);
231270
}
271+
272+
@Group(name = "primitive_group")
273+
interface PrimitiveInterface {
274+
@ToolMethod(name = "primitive_tool", description = "primitive")
275+
int primitiveMethod(@Property(name = "i") int i);
276+
}
232277
}

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/annotation/Property.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
@Retention(RetentionPolicy.RUNTIME)
2525
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
2626
public @interface Property {
27+
/**
28+
* 表示未设置默认值的标记常量。
29+
*/
30+
String UNSET_DEFAULT_VALUE = "$$Fit$UnsetDefaultValue$$";
31+
2732
/**
2833
* 获取参数的名字。
2934
*
@@ -50,7 +55,7 @@
5055
*
5156
* @return 表示参数的默认值的 {@link String}。
5257
*/
53-
String defaultValue() default StringUtils.EMPTY;
58+
String defaultValue() default UNSET_DEFAULT_VALUE;
5459

5560
/**
5661
* 获取参数的样例值。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
package modelengine.fitframework.annotation.util;
8+
9+
import modelengine.fitframework.annotation.Property;
10+
11+
/**
12+
* 为 Property 注解提供工具类。
13+
*
14+
* @author 杭潇
15+
* @since 2025-10-29
16+
*/
17+
public final class PropertyHelper {
18+
/**
19+
* 隐藏默认构造方法,避免工具类被实例化。
20+
*/
21+
private PropertyHelper() {}
22+
23+
/**
24+
* 判断给定的默认值是否为设置状态。
25+
*
26+
* @param defaultValue 要检查的默认值。
27+
* @return 如果自定义参数值则返回 {@code true},否则返回 {@code false}。
28+
*/
29+
public static boolean isCustomValue(String defaultValue) {
30+
return !Property.UNSET_DEFAULT_VALUE.equals(defaultValue);
31+
}
32+
}
33+

0 commit comments

Comments
 (0)