我有这样的活动记录:
case class Person(name: String) extends ActiveRecord
object Person extends ActiveRecordCompanion[Person]
我添加了 Swagger 注释:
@ApiModel(value = "Person", description = "A person.")
case class Person(
@ApiModelProperty(value = "A person's name")
name: String) extends ActiveRecord
object Person extends ActiveRecordCompanion[Person]
生成的模式包含很多垃圾,使其无法使用:
"Person": {
"id": "Person",
"description": "A person.",
"properties": {
"name": {
"type": "string"
},
"valid": {
"type": "boolean"
},
"errors": {
"$ref": "Errors"
},
"id": {
"type": "integer",
"format": "int64"
},
"persisted": {
"type": "boolean"
},
"_companion": {
"$ref": "com.github.aselab.activerecord.inner.ProductModelCompanion<com.github.aselab.activerecord.inner.ProductModel>"
},
"_isNewRecord": {
"type": "boolean"
},
"newRecord": {
"type": "boolean"
},
"com$github$aselab$activerecord$ActiveRecordBase$$_isDeleted": {
"type": "boolean"
},
"recordCompanion": {
"$ref": "com.github.aselab.activerecord.ActiveRecordBaseCompanion<java.lang.Object, com.github.aselab.activerecord.ActiveRecordBase>"
},
"com$github$aselab$activerecord$validations$Validatable$$_validated": {
"type": "boolean"
},
"deleted": {
"type": "boolean"
}
}
}
我查看了源代码swagger-core
。有ModelPropertyParser
一个方法parseRecursive
包含:
for (field <- hostClass.getDeclaredFields) {
if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers()) && !ignoredProperties.contains(field.getName) && !field.isSynthetic()) {
if (ignoredProperties.contains(field.getName)) {
LOGGER.debug("ignoring property " + field.getName)
} else {
parseField(field)
}
}
}
Option(hostClass.getSuperclass).map(parseRecursive(_))
当我运行时:
> classOf[Person].getDeclaredFields
Array[java.lang.reflect.Field] = Array(private final java.lang.String sk.essentialdata.ress.search.db.Person.name)
这是正确的,因为我只声明了一个字段。
当我运行时:
classOf[Person].getSuperclass.getDeclaredFields
res6: Array[java.lang.reflect.Field] = Array(private final long com.github.aselab.activerecord.ActiveRecord.id, private boolean com.github.aselab.activerecord.ActiveRecord.com$github$aselab$activerecord$ActiveRecordBase$$_isDeleted, private final com.github.aselab.activerecord.ActiveRecordBaseCompanion com.github.aselab.activerecord.ActiveRecord.recordCompanion, private final com.github.aselab.activerecord.validations.Errors com.github.aselab.activerecord.ActiveRecord.errors, private boolean com.github.aselab.activerecord.ActiveRecord.com$github$aselab$activerecord$validations$Validatable$$_validated, private final com.github.aselab.activerecord.inner.ProductModelCompanion com.github.aselab.activerecord.ActiveRecord._companion, private boolean com.github.aselab.activerecord.ActiveRecord...
ActiveRecord 有很多内部字段。有没有办法摆脱这种垃圾?我只需要fieldInfo
在招摇描述中包含以下字段:
> Person.fieldInfo
scala> res0: Map[String,com.github.aselab.activerecord.reflections.FieldInfo] = Map(name -> FieldInfo(name,class java.lang.String,false,false,WrappedArray()), id -> FieldInfo(id,long,false,false,WrappedArray()))