0

我有这样的活动记录:

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()))
4

1 回答 1

1

如果你看这里ebean,你会看到一些对子类的特殊处理。您可以轻松添加相同的ActiveRecordCompanion内容,如下所示:

  • 使用要跳过的包扩展ModelConverter和覆盖propertyPackagesToSkip
  • 使用以下命令将新的 ModelConverter 添加到转换器链中:

    ModelConverters.addConverter(yourConverter, true) // 把它放在链的首位

您应该看到模型处理避免遍历您正在跳过的类。

于 2015-02-25T14:59:55.963 回答