-
Notifications
You must be signed in to change notification settings - Fork 144
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
[Feature] 自定义注解,Yapi上加入到Header参数上 #1174
Comments
试试配一下: method.additional.header |
我尝试使用method.additional.header去写到.easy.api.config配置文件上,我想运行一段groovy代码去动态添加header
import java.util.logging.Logger
import com.xxxx.xxxx.xxxx.HeaderCheck
def log = Logger.getLogger("HeaderGenerator")
def headers = []
try {
if (method.parameters && method.parameters.size() > 0) {
def parameter = method.parameters[0]
def paramValue = parameter.value
if (paramValue) {
def fields = paramValue.class.getDeclaredFields()
fields.each { field ->
field.setAccessible(true)
def annotation = field.getAnnotation(HeaderCheck)
if (annotation) {
def fieldValue = field.get(paramValue)
if (fieldValue != null) {
headers.add([
name: annotation.value(),
value: fieldValue,
required: true
])
} else {
log.warning("Field value is null for field: ${field.name}")
}
} else {
log.fine("No HeaderCheck annotation found on field: ${field.name}")
}
}
} else {
log.warning("Parameter value is null")
}
} else {
log.warning("Method has no parameters or not enough parameters")
}
} catch (Exception e) {
log.severe("Error generating headers: ${e.message}")
e.printStackTrace()
}
return headers 但是我不知道如何让他生效,我的能力不是很够,看日志不知道如何定位问题,看了文档并没有找到相应的答案, |
试试这样: method.additional.header[groovy:it.containingClass().name().startsWith("xxxx.xxxx.xxxx.controller")]=groovy:```
logger.info("it:"+it)
for (arg in it.args()) {
logger.info("arg:"+arg)
logger.info("type:"+arg.type())
logger.info("fields:"+arg.type().fields())
for (field in arg.type().fields()) {
logger.info("field: "+field+", annotated with: xxxx.xxxx.xxxx.Annotation :"+field.hasAnn("xxxx.xxxx.xxxx.Annotation"))
if (field.hasAnn("xxxx.xxxx.xxxx.Annotation")) {
logger.info("Annotation found on:"+field)
return tool.toJson([
name: "xxx-xxx-xxx",
value: "{{xxx-xxx-xxx}}",
desc: "xxxx",
required: true,
example: ""
])
}
}
}
``` |
or: method.additional.header[groovy:it.containingClass().name().startsWith("xxxx.xxxx.xxxx.controller")]=groovy:```
logger.info("it:"+it)
for (arg in it.args()) {
logger.info("arg:"+arg)
logger.info("type:"+arg.type())
logger.info("fields:"+arg.type().fields())
for (field in arg.type().fields()) {
def header = field.ann("xxxx.xxxx.xxxx.Header")
logger.info("field: "+field+", annotated with: xxxx.xxxx.xxxx.Annotation, required:"+header)
if (header!=null) {
logger.info("Annotation found on:"+field)
return tool.toJson([
name: header,
value: "{{xxx-xxx-xxx}}",
desc: "xxxx",
required: true,
example: ""
])
}
}
}
``` |
它成功导出了,但是有一个小的问题,就是假如我的入参类型字段存在一个泛型,是无法获取它的type()的吗?我需要获取入参类型的data字段里面,去获取它是否存在我的HeaderCheck注解
我的这个日志是[INFO] Data field type: ,说明这个type根本拿不出来,被泛型擦出了,我尝试用gpt帮我修改,但是并不生效,尝试很多办法都不能生效。我应该怎么去处理这个问题呢?十分感谢帮我进行解答 @tangcent |
@Noel443 应该是这里 |
这里还有一个问题,我发现header只能add一个,于是乎我用数组接住json,后面一并返回,新修改的代码是这样的
但是它的日志出现是:[ERROR] error to parse additional header: [{"name":"header1","required":true}, {"name":"header2","required":true}],这个问题目前有办法处理吗?我自己也尝试找很多办法,但是找不到过多的参考价值,如果有时间,可以帮我看一下吗?十分感谢 @tangcent |
目前
这里是没办法用 |
尝试处理了类型擦除的问题, 可以试试. easy-yapi-2.7.3.212.0.zip |
尝试处理了类型擦除的问题, 可以试试. easy-yapi-2.7.3.212.0.zip |
我的项目需要通过参数去校验请求头有没有这个header,如果没有则报参数不足异常。而能够满足我的功能的是@RequestHeader("xxx"),这个注解在Yapi是能出现的,而我现在需要在我的java bean的某一字段加上我的自定义注解,使其能在YApi上展示出我的header是必须参数,我翻阅了文档,并没有找到相应的说明,是我没找到还是目前暂时不支持?
目前我的自定义注解
The text was updated successfully, but these errors were encountered: